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

Threaded View

  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.

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