0

I'm using the desing view to add components to the JFrame, I create a package "res" and copied the images files and add the image to the JLabel with options, properties/icon/Image with Project/ selecting res package and the file .png, but i don't know why I'm getting this exception:

line 75 exception: jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons8customer32px1.png"))); // NOI18N

If I remove the icon it doesn't crash, if any have an idea why this happen? thanks

private void initComponents() {
  jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons8customer32px1.png")));        
}// </editor-fold>

I'm using this steps for handling images: https://netbeans.org/kb/73/java/gui-image-display.html

error netbeans

Pedro
  • 11
  • 1
  • 5
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Always copy/paste error and exception output! (As in, the ***entire*** stack trace!) 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 4) `AbsoluteLayout` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. .. – Andrew Thompson Apr 01 '20 at 08:36
  • 1
    .. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 01 '20 at 08:36
  • hi, it seems to be that the problem is that when set the icon to the label I get a null reference and that is why I receive the null pointer exception, but I dont know why, I created a new project adding only a JFrame and a label and following this instructions to Handling Images in a Java GUI Application, netbeans.org/kb/73/java/gui-image-display.html, and I get the same error. – Pedro Apr 02 '20 at 02:28
  • 1
    I'm glad to see you have progressed the problem, but you seem to have misunderstood some of the advice offered. When I advised to post the full stack trace, I meant as text, code formatted in the question itself. As to the code, I was hoping you'd realize that we need to be able to match the line numbers mentioned in the stack trace to the code seen in the question, so rather than shortening the code to be even more minimal, it had to be a complete class, with import statements etc. **Suggestion** Check the case used for the name of the icon. – Andrew Thompson Apr 02 '20 at 04:06
  • EIther `jLabel5` is null or the resource wasn't found. The stack trace tells you which. – user207421 Apr 02 '20 at 04:07
  • finally I figured out what the problem is, first my project is a java Maven project, so the example that I was doing is some old and I think is not maven, when I setIcon on JLabel using this: jlabel5.setIcon(new ImageIcon(getClass.getResource("/resource/image.png")); the path that throws using getClass.getResource is wrong, thats why the nullpointerException, I don't have clear yet but I think in maven projects I need to add the resource path of my files, but I don't know how yet, I avoid the error copying the file in ProjectName/target/classes/com/mycompany/projectname/image.png just for test – Pedro Apr 02 '20 at 20:56

0 Answers0