Opening a file produces a java.lang.NullPointerException -


i have jframe , on frame have jbutton, want when file clicked user can load file using java jfilechooser.

i declare filechooser this.

jfilechooser fc; 

and here code in action listener button.

jbutton btnload = new jbutton("load .txt");     btnload.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              int returnval = fc.showopendialog(openfile.this);              if (returnval == jfilechooser.approve_option) {                 file file = fc.getselectedfile();                 //this real application open file.                 system.out.println("opening: " + file.getname() + ".");             } else {                 system.out.println("open command cancelled user.");             }           }     }); 

the error producing me is

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ maple.netflix$2.actionperformed(netflix.java:73) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.awteventmulticaster.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)

here line 73.

int returnval = fc.showopendialog(netflix.this); 

just declaring jfilechooser variable not enough, need initialize reference variable, fc, valid object first before using it. same other reference variable.

jfilechooser fc = new jfilechooser(); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -