0

I have created a 14class java project. So far I have been using default-package. Today I have moved all my classes using: Refactor - Move, and I created a new package.

After doing that, I cannot access my resources. Running my MainForm class I get this error:

java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
        at com.anreas.mls.MainForm.<init>(MainForm.java:279)
        at com.anreas.mls.MainForm$1.run(MainForm.java:48)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

EDIT: Line 279 of MainForm:

        btnHelp.setIcon(new ImageIcon(MainForm.class.getResource("images/help.png")));

Folder Structure:

enter image description here

If anyone knows how I this I would really appreciate it!

Thank you in advance :)

  • How are you accessing these resources? Anyway you should probably read similar question: http://stackoverflow.com/questions/9864267/load-icon-image-exception – Pshemo Apr 30 '16 at 18:28
  • 1
    could you post **line no 279** of `MainForm` class of `package com.anreas.mls` – Blip Apr 30 '16 at 18:28
  • is your main class in the same package as others? could you post a snapshot of your directory structure? – Rahul Sharma Apr 30 '16 at 18:28
  • @Blip updated my question and added the line. – Andreas Neophytou Apr 30 '16 at 18:30
  • @RahulSharma updated question and added a snapshot, all classes are in my package. – Andreas Neophytou Apr 30 '16 at 18:32
  • 2
    Try with `.getResource("/images/help.png")` – Pshemo Apr 30 '16 at 18:33
  • does your package `com.anreas.mls` has a directory `images` that contains the file `help.png`? if not place the same and it will work fine – Blip Apr 30 '16 at 18:33
  • @Pshemo why did this work? I didn't have this issue with Default-Package. Can you please my kind sir explain to me why this was the solution? – Andreas Neophytou Apr 30 '16 at 18:37
  • https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String- In short if you start with `/` resource will be searched starting from root directory of your project, otherwise you will start from package which contains class on which you invoked `getResource`. – Pshemo Apr 30 '16 at 18:42
  • @Pshemo thank you so much man. made my day :) – Andreas Neophytou Apr 30 '16 at 18:48

1 Answers1

0

The answer for this is what Pshemo commented in my question.

.getResource("/images/help.png")

enter image description here