Why does my icon handling code throw a NullPointerException?

I added an image for my button, but when I run this 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 bad check (recommended by Peter Lang): System.out.println (getClass (). GetResource ("/Images/yahoo_1.gif")); and it returns null, why? please help me.

+3
source share
6 answers

That means getClass().getResource("/Images/yahoo_1.gif")returns null.

Javadoc claims this happens if

invoker .

  • , getResource null:
    System.out.println(getClass().getResource("/Images/yahoo_1.gif"));

  • , .

NetBeans.

Source Packages
  Images
    yahoo_1.gif

. ?

Clean and Build.

+6

, bin, src.

null getClass(). getResource ( "image.png" ). null, , , , .class .

. , .

+4

, getClass().getResource("/Images/yahoo_1.gif") null, ...gif . (, ?)

+1

URL-, , null :

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

JDK:

[getResource (..) ] URL , null, invoker

, ( "Images/yahoo_1.gif" ) - ?

+1

. :

  • jar bin (, .class ) .
+1
private class HandlerClass implements ActionListener{
        public void actionperformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }

}
0

Source: https://habr.com/ru/post/1727604/


All Articles