Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: images are loaded while using eclipse but i get NullPointerException in jar

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default images are loaded while using eclipse but i get NullPointerException in jar

    As you can see I have tried various ways of loading images to my program,
    Image iiii = new ImageIcon("src/gra/grafika/ramka.png").getImage();
    //Image iiii = new ImageIcon(this.getClass().getResource("grafika/ramka.png")).getImage();
    //Image iiii = new ImageIcon(ClassLoader.getSystemResource("gra/grafika/ramka.png")).getImage();
     
    this.setIconImage(iiii);

    (I keep every image loaded by the same method so i can easily change the way of doing this.)

    The point is when i run it in eclipse there there is everything fine, but after making jar i get NullPointerException in line containing " Image iiii = ..." or just no single image appears (no nullpointerexception)

    Question is HOW should i load them because I am a little bit resigned after wasting like 12h (pathetic) of testing ways, changing adresses without any success : |
    Last edited by Teuthoidea; June 11th, 2012 at 11:52 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Are the images packaged into the jar as well (unpackage the jar to check and make sure they are there)?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    If the images are in the jar you need to access them as resources not files.
    You can use a zip utility to look at the contents of a jar file to verify that the path to the images is correct.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    images are in location gra > grafika in jar file

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Does the URL returned by getResource() look correct? Add a println() call that prints it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Quote Originally Posted by Teuthoidea View Post
    images are in location gra > grafika in jar file
    Based upon the path you mentioned above, you should load the image as a resource, passing the absolute path of the package the images reside in. If the image ranka is in /gra/grafika, then pass that path:

    Image iiii = new ImageIcon(this.getClass().getResource("/gra/grafika/ramka.png")).getImage();

    As Norm mentioned, check the paths to the images...preface the path with a '/' to indicate absolute path (relative to the root package within the jar)

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    System.out.println(this.getClass().getResource("/gra/grafika/ramka.png"));

    this line prints null just like there's no such file

    also tested line from copeg
    and that's the results of running it
    null <- println~
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    Last edited by Teuthoidea; June 11th, 2012 at 02:25 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Is the class in a package? Did you try it with the leading / on the path as copeg suggested?
    Check the spelling and the path for correctness.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    hierarchy of packages : Przechwytywanie.PNG

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    You need to have the path point to where there is a file. It is not something that can be done remotely on this forum. You need to try many different combinations until you find the one that works.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    i'll be glad if you give me an example with path point i've seen it earlier today but cant google it out now

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Sorry, what is "path point"? What I was talking about was the paths as you see them in a jar file using a tool like zip file tool.

    I see now. I'll reword it: The path should point to where there is a file.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    i have seen smth like using " new point () " don't remember arguments of it and whole thing was related to my problem but seemd to be hard at all

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Nothing to do with a point class.
    I'll reword it: The path should refer to where there is a file.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    strange thing - my friend said that it's working while i have tested it at my pc (win7) and then at winxp at virtualbox both failed, moreover he send me his game and i images there were not loading like at my game (i got screen that it's working at his pc)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    Are you saying the program in the jar file executes OK on one PC and has problems loading images on another PC?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: images are loaded while using eclipse but i get NullPointerException in jar

    problem solved - i thought that it'll load images from the inside of jar but it just has to be in proper folder to run thanks for help
    Last edited by Teuthoidea; June 12th, 2012 at 03:33 AM.

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM
  3. Beginners Eclipse Tutorial. How to run first java application on Eclipse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 13
    Last Post: June 24th, 2011, 12:26 AM
  4. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM