NullPointerException in java. Save image to bin folder so the software runs without errors?

3

I'm developing a project in java and putting images in jLabels , however, every time I run the program, it displays a message " Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException " and does not execute, loads up to 50 %.

When I go into properties of jLabel, I click icon and reset as default, jLabel is left without image and the program runs normally.

During my search I found people talking about how my code was returning null in the image ... The following code:

jLabelImgLogoInicial.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/gui/img/logoempregoinicial.png"))); // NOI18N

My problem is that the right path to the image would be " /img/logoempregoinicial.png ", but when I insert the image, netbeans automatically creates the address of the previous code and returns null because it will not find the image .

The architecture image of my project follows:

Thestackoferrorsfollows:

Exceptioninthread"AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at view.gui.LogoInicial.initComponents(LogoInicial.java:47)
    at view.gui.LogoInicial.<init>(LogoInicial.java:20)
    at view.gui.MainEmpreGO.<init>(MainEmpreGO.java:19)
    at view.gui.MainEmpreGO$6.run(MainEmpreGO.java:278)
    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)

I have read that in order to solve this problem, it is necessary to save the images in a netbeans "bin" folder, but I did not find this folder.

  

How do I find the netbeans bin folder to save the image and this   problem no longer occurs and why it is necessary to save   specifically in this folder so that the program does not put a path   different than what I need?

Source searched: Why does my icon handling code throw a NullPointerException?

    
asked by anonymous 01.09.2016 / 17:17

1 answer

1

Come on!

NetBeans has two ways to create Java projects:

  • One is by clicking File, New Project, Category: Java - > Java application. This way I'll call Project ANT .
  • The second is by clicking File, New Project, Category: Maven - > Java application. This way I'll call Maven Project .
  • ANT projects use a tool called ant to manage the application build process. This type of project is easy to recognize by the build.xml file found at the root of the project.

    Maven projects use a tool called Maven , also to manage the build process (and much more). You can recognize Java Maven projects in several ways:

    • They have a pom.xml file at the root of the project.
    • They have a specific folder structure:
      • .java files are within src / main / java
      • Resource files, whether text, icons, configuration, are in src / main / resources
      • Java test classes (which are also .java ) are in src / test / java
      • And resource files used only in tests, are in src / test / resources
    • When Maven compiles classes, it merges both src / main / java and src / main / resources , and throws the compiled result to

    Now the most important thing:

    • If there is any file other than .java within src / main / java , it will ignore them altogether. Even folders without Java files are ignored! If you want these files to be contained in your application, they should be within src / main / resources !!

    Looking at your folder structure, I have seen that it is a Maven project, so the solution is to create a src / main / resources / view / gui / img strong> and move the logoemplogoinicial.png file into it.

        
    27.10.2016 / 01:29