Here is my code. I want to display an image chosen by Jfilechooser on the content pane of the frame. Since i have designed frame in different class, i am enable to add image panel to the cintent pane. i have tried below code, but it is showing null pointer exception. Can anybody help me to know where is the error in the code & what should i do to make its working correct?
post up the log of error, maybe we can see where you erring. But you have to know than NullPointerException points to an object that is being loaded without receiving a value.
error that is occuring after selecting file from JFilechooser
c:\javac ImageEditor.java
c:\java ImageEditor
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at OpenDialog.<init>(ImageEditor.java:26)
at MenuHandler.actionPerformed(ImageEditor.java:163)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
13)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2336)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:405)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:260
)
at javax.swing.AbstractButton.doClick(AbstractButton.java:375)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1
238)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMen
uItemUI.java:1282)
at java.awt.Component.processMouseEvent(Component.java:6100)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3288)
at java.awt.Component.processEvent(Component.java:5865)
at java.awt.Container.processEvent(Container.java:2099)
at java.awt.Component.dispatchEventImpl(Component.java:4461)
at java.awt.Container.dispatchEventImpl(Container.java:2157)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4455
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4119)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4049)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Window.dispatchEventImpl(Window.java:2555)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:605)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:276)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)
Why post the entire code when the exception clearly shows where it is? It's located on line 26, in the OpenDialog constructor. My guess it's this line:
After all, where have you initialized frame? You haven't!
I don't see the purpose of the entire class though. It extends Component yet you never use it as a component. It's never added to any container. However, it isn't necessary either. All the entire class ever does is show the open dialog and process its selected file. You can better just copy the code in the constructor to the event handler, and drop the entire class completely (and SaveDialog as well):