0

I made a program in Netbeans using the GUI builder and there it was running fine but when I copied the source code from Netbeans and pasted in Eclipse I am getting the following error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at frame2.initComponents(frame2.java:100)
at frame2.<init>(frame2.java:17)
at frame2$5.run(frame2.java:671)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I searched it on net and found that I might had not initialized something which caused this exception. So here are the questions :-

1) If there is any exception why the program is running fine in Netbeans?

2) and how could I make this code run on Eclipse?

here is the link of my code:

http://pastebin.com/tVXjKZ9A

And I had putted the image on the right place.

1
  • The error indicates that you are doing something wrong in frame2.java line 100, you probably pass null to the ImageIcon constructor. Commented Mar 31, 2014 at 13:11

2 Answers 2

2

I'm not sure if I understand what you mean with "copy to java...". But anyway, the stacktrace indicates that there is a nullpointer on line 100 in your code.

jLabel33.setIcon(
      new javax.swing.ImageIcon(getClass().getResource("I:/Workspace/image.png"))
); // NOI18N

The problem with the line, is that you got a "train dot crash". There are several positions where the nullPointer could come from. But I guess that when you move your code outside of Netbeans, you don't have access to I:/Workspace anymore.

Even if that is not the problem you should change that, so that the path is relative to the project. And not hardcoded as an absolute path. That will make it much easier to move the code to another computer :-).

Sign up to request clarification or add additional context in comments.

3 Comments

sorry it was copied from netbeans to eclipse..and I had already tried changing the path. the current path is eclipse workspace.
@Vaibhav Is the file in the same folder as the java file?
@RD-Develop yes..I placed the file in the same folder....even tried changing the path...but no luck...it gave me the same exception...
0

I finally made my code run in eclipse. In the line 100 (as mentioned by stacktrace) there was null pointer exception. here is that line.

jLabel33.setIcon(
  new javax.swing.ImageIcon(getClass().getResource("I:/Workspace/image.png"))
); // NOI18N

I removed getClass().getResource and here is the new line

jLabel33.setIcon(
  new javax.swing.ImageIcon("I:/Workspace/image.png")
); // NOI18N

and it was working just fine. Everything is fine now. But I am wondering how getClass().getResource made my code stopped working in eclipse which was working just fine in netbeans.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.