Nullpointerexception in java. Save image in bin folder for the software to run error-free?

Asked

Viewed 525 times

4

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

When I go into jLabel properties, I click icon and reset to default, jLabel runs without image and the program runs normally.

During my research I found people saying that my code was returning null in the image... Follow the code:

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

My problem is that the right path to image would be "/img/logoempregoinicial.png", however, when I insert the image, netbeans automatically creates the previous code address and results in null return, as it will not find the image.

Follow the image of the architecture of my project:

inserir a descrição da imagem aqui

Follows the stack of errors:

Exception in thread "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 read that to solve this problem, it is necessary to save the images in a netbeans "bin" folder, but I did not find this folder.

How to find the netbeans bin folder to save the image and this problem does not occur anymore 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?

  • Where is the stack of errors?

  • I forgot to post the print, I will edit the question with print @Renan

  • edited the question with the @diegofm bug stack

  • Have you tried to pass the path through code, not through the netbeans editor? And confirm there where line 47 of the Logoinitial class is.

  • I’ll run tests here for code. Line 47 is the same "jLabelImgLogoInicial.setIcon(new javax.swing.Imageicon(getClass().getResource("/view/Gui/img/logoempregoinicial.png")); // NOI18N"

  • 1

    Change the address to /main/java/view/gui/img/logoempregoinicial.png

  • Is using the build.xml generated by Netbeans to build the . jar, right? Have you made any modifications or are you "the way it is"? Just so you know, I think you can formulate an answer.

  • I’m doing tests @diegofm thanks for the tips.

  • @Renan am using the default netbeans option, but feel free to post your answer, maybe I’ll try it your way and fix my problem.

Show 4 more comments

2 answers

2

Come on!

Netbeans has two ways to create Java projects:

  1. One is by clicking on File, New Project, Category: Java -> Java application. This way I will call Project ANT.
  2. The second is by clicking on File, New Project, Category: Maven -> Java application. This way I will 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 file xml build. found at the root of the project.

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

  • They have a file pom.xml at the root of the project.
  • They have a specific folder structure:
    • The archives .java stay inside src/main/java
    • Resource files, be they text, icons, configuration, are in src/main/Resources
    • Java test classes (which are also .java) are in for src/test/java
    • And resource files used only in tests, are in src/test/Resources
  • When Maven compiles the classes, it joins both the src/main/java and the src/main/Resources, and plays the already compiled result for target/classes.

Now the most important:

  • If there is any file other than .java inside src/main/java, it will totally ignore them. 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 saw that it is a Maven project, and therefore the solution is to create a folder src/main/Resources/view/Gui/img and move the file logoempregoinicial.png into her.

  • None of the netbeans projects that I have here follow this structure that you mentioned there, they all have only the src folder followed by the Packages that I myself created in the project, within the IDE, including with files other than . java, which are recognized and compiled together with the project normally.

  • 1

    diegofm, so you’re using an ANT project, not a Maven, which is the case with Marcielli. By default Netbeans creates the projects using ANT itself.

  • Now it made more sense to me, looking again at the structure. As she did not quote anything, it implied that she had created.

-2

Dude was having the same problem with a project that I exported the zip package or even bringing it by sharing the github, which solved, me comparing it to a test project that I did, saw the difference between one and the other realized that the folder I created in the project’s file directory did not have the folder named images in the project’s src directory, what I did was copy the images folder and paste in src, closed the netbeans and ready, working without error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.