Results 1 to 3 of 3
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] java.lang.NullPointerException when reading getClass().getResourceAsStream(.

    The code below is generating an error when trying to read a stream object (see code). The file is located within a subdirectory named 'config' in the sub-folder of the project.

    :confused:

    Java Code:
    package homenetwork.bkr.training;
    import java.awt.*;
    import java.io.*;
    import java.net.URL;
    import java.util.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ResourceTestFrame extends JFrame {
    	
    	public ResourceTestFrame() {
    			setTitle("Resource Test");
    			setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    			URL aboutURL = getClass().getResource("icons/exit.png");
    			Image img = Toolkit.getDefaultToolkit().getImage(aboutURL);
    			setIconImage(img);
    			
    			JTextArea textArea = new JTextArea();
    			//InputStream stream = getClass().getResourceAsStream("config/about.txt");  //Option A
    
    			[B]InputStream stream = getClass().getResourceAsStream("C:\\config\\about.txt"); //Option B[/B]
    			Scanner in = new Scanner(stream);
    			Scanner in = new Scanner(stream);
    			while (in.hasNext())
    				textArea.append(in.nextLine() + "\n");
    			
    			add(textArea);
    	}
    	
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT =300;
    	
    }
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at homenetwork.bkr.training.ResourceTestFrame.<init>( ResourceTestFrame.java:21)
    at homenetwork.bkr.training.ResourceTest$1.run(Resour ceTest.java:13)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    This worked okay: Both the image and the source/.java file appeared in the gui.
    The console output was:
    Java Code:
    C:\jexp>java RTF
    url = file:/C:/jexp/images/dukeWaveRed.gif
    Java Code:
    import java.awt.*;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.Scanner;
    import javax.swing.*;
    
    public class RTF extends JFrame {
    
        public RTF() {
            super("Resource Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
            URL url = getClass().getResource(//"icons/exit.png");
                                             "images/dukeWaveRed.gif");
            System.out.println("url = " + url);
            Image img = Toolkit.getDefaultToolkit().createImage(url);
            add(new JLabel(new ImageIcon(img)), "First");
    
            JTextArea textArea = new JTextArea();
            InputStream stream =                     //Option A
                getClass().getResourceAsStream(//"config/about.txt");
                                               "utilities/FileSize.java");
    
            //InputStream stream =                   //Option B
            //    getClass().getResourceAsStream("C:\\config\\about.txt");
            Scanner in = new Scanner(stream);
            while (in.hasNext())
                textArea.append(in.nextLine() + "\n");
    
            add(new JScrollPane(textArea));
            setVisible(true);
        }
    
        public static void main(String[] args) { new RTF(); }
    
        public static final int DEFAULT_WIDTH = 300;
        public static final int DEFAULT_HEIGHT =300;
    }
    Java Code:
    InputStream stream =                     //Option A
        getClass().getResourceAsStream(//"config/about.txt");
                                       "utilities/FileSize.java");
    The utilities folder is located in the same folder as the class file, the current directory, and it contains a file named FileSize.java.
    Java Code:
    InputStream stream =                   //Option B
        getClass().getResourceAsStream("C:\\config\\about.txt");
    With the argument "C:\\config\\about.txt" the class loader will look in the C folder for a folder called config and inside it will be a file named about.txt.
    The file is located within a subdirectory named 'config' in the sub-folder of the project.
    A sub-folder of the project...
    If the project is in the C folder then it contains a sub-folder which then contains the config folder. The class loader won't be looking for the extra folder between C and config.
    Try a path relative to your class file. If the class file is in the project folder you can try:
    Java Code:
    // The project folder is the current directory and contains the class file
    // and also contains a folder named [i]subFolder[/i].
    getResourceAsStream("subFolder/config/about.txt");
    You can use a single forward slash for file separator for/in java.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks. My mistake was not having included the sub-folders under the package name.

    C:\Users\Administrator\workspace\ResourceTestTwo>t ree /F /a
    NOTE: It's noted that somehow a space is inserted between the 't' and the 'r' when I select 'Save' :S
    Folder PATH listing
    Volume serial number is 982D-F9F4
    C:.
    | .classpath
    | .project
    |
    +---.settings
    | org.eclipse.jdt.core.prefs
    |
    +---bin
    | \---homenetwork
    | \---bkr
    | \---training
    | | RTF.class
    | |
    | +---config
    | | about.txt
    | |
    | \---images
    | car.jpg
    |
    \---src
    \---homenetwork
    \---bkr
    \---training
    | RTF.java
    |
    +---config
    | about.txt
    |
    \---images
    car.jpg

Similar Threads

  1. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  3. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  4. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 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
  •