Results 1 to 3 of 3
- 05-30-2009, 07:04 PM #1
[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:
Error: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; }
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)
- 05-30-2009, 11:51 PM #2
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; }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 A getClass().getResourceAsStream(//"config/about.txt"); "utilities/FileSize.java");
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.Java Code:InputStream stream = //Option B getClass().getResourceAsStream("C:\\config\\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:
You can use a single forward slash for file separator for/in java.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");
- 05-31-2009, 05:03 PM #3
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
-
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:36 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 2Last Post: 02-27-2009, 10:11 AM -
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM -
java.lang.NullPointerException
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:39 PM -
java.lang.NullPointerException
By Felissa in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks