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 8 of 8

Thread: Cannot load file from resource using getClass().getResource

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Cannot load file from resource using getClass().getResource

    For anyone that is having this issue the resolution was to (actually) add the folder to the class path.
    Contrary to what you will find via google, the path cannot be added via project properties (apache NetBeans 12.6). At least not under the file/project properties/libraries menu item.
    (I cant use the right click method to get to project properties due to NetBeans mishandling screen size and menu items).
    Right click 'Libraries' in the project browser and select 'Add Jar/Folder'.


    Hello:
    Using NetBeans on Ubuntu.
    I am trying to load an image into my class but am getting null pointer error.
    Exception in thread "main" java.lang.NullPointerException
    	at java.desktop/sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:692)
    	at java.desktop/sun.awt.SunToolkit.getImage(SunToolkit.java:728)
    	at oddcouplepoker.GameCanvas.<init>(GameCanvas.java:23)
    I can see the image in my project files list.
    h t t p s : //imgur.com/a/dIZpAAT

    This is the line that is throwing errors:
    this._backgroundImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/images/oddcouple.png"));
    From this constructor of a class that extends JPanel:
    public class GameCanvas extends JPanel {
        private Image _cardsTileset; 
        private Image _backgroundImage; 
     
        public GameCanvas(String cardsImagePath, String backgroundImagePath) {
            //this. _cardsTileset = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(cardsImagePath));
            System.out.println(backgroundImagePath);
            this._backgroundImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("resources/images/oddcouple.png"));
            System.out.println("past");
        }
    I have been searching all over and I cannot find the answer.
    Last edited by LeslieS; January 8th, 2022 at 02:24 PM.

  2. #2
    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: Cannot load file from resource using getClass().getResource

    What folders are on the classpath? Where is the resources folder? Is it in one of the folders on the classpath?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot load file from resource using getClass().getResource

    The folder resources/images is on the class path I think. I added the folder to Project properties - source package folders.

    If you look at the image I linked you can see the image is in the project under resources/images - <default package> - oddcouple.png
    I can double click on it there and view it in the source window.
    Also the image loads using this ImageIO instead:
    try {
                img = ImageIO.read(new File("resources/images/oddcouple.png"));
    Last edited by LeslieS; January 7th, 2022 at 10:36 PM.

  4. #4
    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: Cannot load file from resource using getClass().getResource

    the image loads using this ImageIO
    That says the resources folder is in the current directory when the program executes.
    How do you know what folders are on the classpath? Did you print it out?
    For example this statement will print out the classpath:
        System.out.println(System.getProperty("java.class.path"));  // show classpath

    If the resources folder is on the classpath, then the getResource method should look in the images folder
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Cannot load file from resource using getClass().getResource

    Quote Originally Posted by Norm View Post
    If the resources folder is on the classpath, then the getResource method should look in the images folder
    (Assuming this is on Windows) I like to use SysInternals Process Monitor to see where a program looks for its files. It can sometimes be surprising 😀
    [EDIT] Oops, I read this is on Ubuntu. Then use strace to the same effect.

  6. #6
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot load file from resource using getClass().getResource

    Thank you Norm, however, sadly the resource is still not being loaded.

    The folder was not in my class path.

    I added it in and now the output from
     System.out.println(System.getProperty("java.class.path"));  // show classpath
    is
    /home/noone/NetBeansProjects/OddCouplePoker/resources:/home/noone/NetBeansProjects/OddCouplePoker/resources/images:/home/noone/NetBeansProjects/OddCouplePoker/build/classes
    Exception in thread "main" java.lang.NullPointerException
    	at java.desktop/sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:692)
    	at java.desktop/sun.awt.SunToolkit.getImage(SunToolkit.java:728)
    	at oddcouplepoker.GameCanvas.<init>(GameCanvas.java:35)
    So the folder is now in the class path. I checked again to make sure the file was in that folder.
    But still this persistent error.

    Edit:
    After some experimentation and juggling paths, actually removing the path to the file seems to have worked:
            this._backgroundImage = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/pokertable.png"));
    I am not sure why the path cant be included but I am marking this as solved.
    Thanks a lot.
    Last edited by LeslieS; January 8th, 2022 at 02:26 PM.

  7. #7
    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: Cannot load file from resource using getClass().getResource

    The classpath needs to contain the folder that contains the path to the resource file.
    If the full path to the file is: :/home/noone/NetBeansProjects/OddCouplePoker/resources/images/pokertable.png
    Then the classpath and the path in the getResouce method together point to the file:
    cp=home/noone/NetBeansProjects/OddCouplePoker/resources               resPath=images/pokertable.png
    cp=home/noone/NetBeansProjects/OddCouplePoker/resources/images    resPath=pokertable.png
    cp=home/noone/NetBeansProjects/OddCouplePoker/                          resPath=resources/images/pokertable.png
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Cannot load file from resource using getClass().getResource

    In my case:
    cp=home/noone/NetBeansProjects/OddCouplePoker/resources               
    resPath=
    images/pokertable.png - error
    /images/pokertable.png - no error
    pokertable.png - error
    /pokertable.png - no error

Similar Threads

  1. Having issues loading files using getClass().getResource()
    By Technetium in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 13th, 2023, 01:17 PM
  2. [SOLVED] Null pointer exception when attempting to load file from inside jar file
    By teslaa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2018, 06:47 PM
  3. Problem creating text file using the using class.getResource()
    By Pin Head in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 14th, 2013, 01:27 AM
  4. [SOLVED] How to i load my txt file into jtable?
    By legend101z in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: April 7th, 2013, 07:39 PM
  5. Load ArrayList from file
    By dcdude in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: May 13th, 2012, 06:11 AM