0

I am in the processes of rebuilding a simple game program that I created to improve efficiency. I am running into trouble when I try to set an Icon to a JButton. The simpler version of the program runs perfectly with no issues in this area. I am using the same code to accomplish the same task here, but I am getting a lot of errors that I have not seen before. Could I get some help understanding the errors?

This is the path to the .jpg file: /Program/src/fire.jpg

This is the code I am using to add the icon to the button. The errors point to the line starting with new, specifically the work new when I space them out in Eclipse. The error does not lie in hand.[d], I have tested that thoroughly. Thanks for any help given.

SwingUtilities.invokeLater(new Runnable() {

    @Override 
    public void run() {GraphicGameBoard.hand[d].setIcon(new 
        javax.swing.ImageIcon(getClass().getResource("/Program/fire.jpg")));}});

Stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException   
at javax.swing.ImageIcon.<init>(Unknown Source)
    at Program.Main$2.run(Main.java:168)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sev
  • 883
  • 1
  • 14
  • 34
  • 5
    Check what does `getClass().getResource("/Program/fire.jpg")` actually returns. The stack trace states that the exception was thrown during `ImageIcon`'s initialization. – Fritz Sep 02 '13 at 19:04
  • @ Andrew Thompson Wow, thought this was a good site for a new programer to come for advice and to get pointed in the right direction. Guess it's not. – Sev Sep 02 '13 at 19:23
  • 1
    As in any human community, threre are different levels of tolerance when it comes to less experienced people. Please don't judge the whole based in one individual. Still, as a tip, whenever you see an exception try to read its JavaDoc. Also, if you're using an existing class, check the JavaDoc of the involved methods. The more you get to know what you're dealing with, the more you'll get around obstacles on your own. – Fritz Sep 02 '13 at 19:39
  • Great. Thanks for the advice Gamb, I am self taught as far as Java is concerned. I was unaware there where JavaDocs for specific exceptions. I tried strait up googling the exceptions and came up empty, only then did I post here. – Sev Sep 02 '13 at 19:56
  • @Nerves82 : Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), hope it helps, in providing some extra information :-) – nIcE cOw Sep 03 '13 at 15:25

2 Answers2

1

Okay, getResource looks into the classpath. If it cannot find the resource, it returns null. So if you create a jar, you might open it with 7zip/WinZip and find a path /Program/fire.jpg (case-sensitive!). Without jar, starting with the classes root directory.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

I found my solution here:

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException". I have no clue what I'm doing wrong

So the issue was not with the path to the image file as I thought. It was with my JButton[] array. I had only declared it and not initialized it properly within my GUI.

Community
  • 1
  • 1
Sev
  • 883
  • 1
  • 14
  • 34