views:

227

answers:

3

I have added an image for my button,but when I run that frame this exception will be thrown .why?please help me.

init:

deps-jar:
compile-single:
run-single:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
        at ClientGUI.IdAndPasswordFrame.initComponents(IdAndPasswordFrame.java:91)
        at ClientGUI.IdAndPasswordFrame.<init>(IdAndPasswordFrame.java:22)
        at ClientGUI.IdAndPasswordFrame$4.run(IdAndPasswordFrame.java:200)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
BUILD SUCCESSFUL (total time: 1 second)

line 138:

public ImageIcon (URL location) {
    this(location, location.toExternalForm());
}

line91:

 jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/yahoo_1.gif"))); // NOI18N

I use this poor checking (Peter Lang recommended)which is:System.out.println(getClass().getResource("/Images/yahoo_1.gif")); and it returns null,why? please help me.

+3  A: 

This means, that getClass().getResource("/Images/yahoo_1.gif") returns null.

JavaDoc states that this happens if

the resource could not be found or the invoker doesn't have adequate privileges to get the resource.

  1. Check if getResource really returns null:
    System.out.println(getClass().getResource("/Images/yahoo_1.gif"));

  2. Make sure that your path is correct and that it is in your classpath.

EDIT:

I just tried it with NetBeans. I created the following structure

Source Packages
  Images
    yahoo_1.gif

and your code worked fine. Is this your structure?

Try to right-click on your application and select Clean and Build.

Peter Lang
So what should i do?
Johanna
I do the first step which is:Check if getResource really returns null:System.out.println(getClass().getResource("/Images/yahoo_1.gif"));and it returns null,what should i do? please help me
Johanna
@Joachim Sauer: Thanks for editing, i used Oracle's `NULL` instead of Java's `null` :)
Peter Lang
Thanks a million I have used Clean and Build ,and the code worked fine,thanks.
Johanna
+1  A: 

It looks like getClass().getResource("/Images/yahoo_1.gif") returns null i.e. the .gif cannot be found on your classpath. (Images versus images maybe?)

rsp
+1  A: 

The URL being passed in is null from this line:

getClass().getResource("/Images/yahoo_1.gif")

From the JDK documentation:

[getResource(..) returns] A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource

Maybe you meant ("Images/yahoo_1.gif") - i.e. relative path not absolute?

Paolo
I use netbeans ,and I choose just icon menu for adding a photo.
Johanna