java - JFileChooser and Jbutton errors -


following question, error message jbutton , jfilechooser, want have jbutton browse file using jfilechooser. code have:

package main;  import java.awt.component; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.itemevent; import java.awt.event.itemlistener; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield;  public class main {  private static component frame; private static string fullpath;  public static void main(string args[]) throws filenotfoundexception, ioexception {     date start_time = new date();     try {          gridbagconstraints gbc = new gridbagconstraints();          jbutton inputbutton = new jbutton("browse input file");          final jfilechooser inputfile = new jfilechooser();         inputfile.setfileselectionmode(jfilechooser.files_and_directories);          jpanel mypanel = new jpanel(new gridbaglayout());          mypanel.add(inputbutton, gbc);          inputbutton.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 jfilechooser inputfile = new jfilechooser();                 inputfile.setfileselectionmode(jfilechooser.files_and_directories);                 if (inputfile.showopendialog(null) == jfilechooser.approve_option) {                     file file1 = inputfile.getselectedfile();                     string fullpathtemp = (string) file1.getabsolutepath();                     fullpathtemp = fullpath;                 }             }         });         date stop_time = new date();         double etime = (stop_time.gettime() - start_time.gettime()) / 1000.;         system.out.println("\nelapsed time = " + etime + " seconds\n");      } catch (exception e) {         system.err.println("error: " + e.getmessage());     } {     } } } 

the problem after clicking on button "browse input file" , choose file, click on ok, error message:

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ main.main$1.actionperformed(main.java:195) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) 

you've declared inputfile 3 times

once static class variable

private static jfilechooser inputfile; 

then in you're main method

    final jfilechooser inputfile = new jfilechooser();     // can't possible compile     inputfile.setfileselectionmode(jfilechooser.files_and_directories); 

and in actionlistener

    inputbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             jfilechooser inputfile = new jfilechooser();             inputfile.setfileselectionmode(jfilechooser.files_and_directories);             if (inputfile.showopendialog(null) == jfilechooser.approve_option) {                 file file1 = inputfile.getselectedfile();                 string fullpathtemp = (string) file1.getabsolutepath();                 fullpathtemp = fullpath;             }         }     }); 

it should possible of these interfere each other produce nullpointerexception can see, given fact code example won't compile can imagine we're not seeing everything


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -