Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default .jar file giving null pointer exception for JLabel iconimages....

    I created a program in Netbeans, and it runs fine within Netbeans. However when I create the .jar file and attempt to run it, I am experiencing a weird issue.

    My program uses JLabels and gives them Image Icons to display. Any of the Image Icons that are set BEFORE the program is run, show up fine when the jar is executed. However, any of the Image Icons that are set dynamically AFTER the program has been executed give me a null pointer exception that looks like:
    java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at com.sphereccgclient.game.GameFrame.displayBackgrou nd(GameFrame.java:770)

    Here is line 770 of GameFrame.java (this code is called dynamically after the program runs):
    backGround.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/backgroundtemplate1.jpg")));

    But THIS line of code works (it is set in the initComponents() method of the window):
    labelShadow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Symbols/shadowspherelabel.JPG"))); // NOI18N

    I don't understand why I am getting a null pointer exception, because when I extract the contents of the jar file, I can manually locate all of the images that are used in the program.

  2. #2
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: .jar file giving null pointer exception for JLabel iconimages....

    Hi,

    you have to use the classLoader, because with in a .jar, the structure of your packages isn't like we expected to be.
    the line
    getClass().getResource("/Images/backgroundtemplate1.jpg"))
    you have to change in:
    getClass().getClassLoader().getResource("/Images/backgroundtemplate1.jpg")

  3. #3
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default Re: .jar file giving null pointer exception for JLabel iconimages....

    Thank you for your response. I will modify the code as per your suggestion.

    It turned out though, that my particular problem was that when the jar attempts to find a file it is actually case-sensitive, whereas when I run the program in netbeans it is not case-sensitive.

    The reason the static code was working was because I manually browsed for the file on my harddrive, while for the dynamic code I simply typed in the name of the file. So the problem was that I was typing ".jpg" instead of ".JPG". Kinda weird, but that's what it was haha.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: .jar file giving null pointer exception for JLabel iconimages....

    Kinda weird
    It's a Windows gotcha that arises because that OS is casual about case when a file:// is being accessed, but Java is more strict when a url specifies an entry in a jar archive. It pays to use a standard case convention (like all lower case) for resources like images so that there are no surprises when everything is put into a jar file.

Similar Threads

  1. Storing an image file into DB(Null pointer exception)
    By mithcool in forum Java Servlet
    Replies: 5
    Last Post: 06-27-2011, 06:30 PM
  2. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 06:04 PM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 02:12 AM
  4. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  5. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •