Results 1 to 6 of 6
- 01-07-2010, 12:36 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Why do I get a NullPointerException?
I have two classes in a package called compressor: Compressor and Reader. Compressor takes a file from the first command line argument (args[0]) and then attempts to read it into a byte array using an instance of Reader.
Compressor's code:
Reader's Code:Java Code:public class Compressor { File f; public Compressor(File file) { f = file; System.out.println("File set."); } public void compress() { try { r.read(f); } catch (Exception e) { System.out.println("Caught"); e.printStackTrace(); } } public static void main(String[] args) throws IOException { String fileName = args[0]; File f = new File(fileName); Compressor c = new Compressor(f); c.compress(); } }
Both classes compile fine ("javac compressor/Compressor.java"), but when I try to run them on the command line with a file ("java compressor/Compressor 1") I get the following:Java Code:public class Reader { FileInputStream fileIn; byte[] in; public Reader() throws IOException { fileIn = null; } public byte[] read(File file) throws IOException { long length = file.length(); in = new byte[(int) length]; fileIn = new FileInputStream(file); int offset = 0; int numRead = 0; while (offset < in.length && (numRead = fileIn.read(in, offset, in.length-offset)) >= 0) { offset = offset + numRead; } fileIn.close(); return in; } }
File set.
Caught
java.lang.NullPointerException
at compressor.Compressor.compress(Compressor.java:19)
at compressor.Compressor.main(Compressor.java:30)
Having looked up NullPointerExceptions, etc, I think that the program is not calling the actual file (named "1"), but I'm not sure why not.
Any advice on what I am doing wrong would be greatly appreciated!
- 01-07-2010, 12:40 PM #2
I doubt that they compile fine. r.read(f); r is not defined in Compressor. Or you've posted the wrong code.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-07-2010, 12:42 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Sorry my bad code editing! It's defined just after "File f;" as "Reader r;"
- 01-07-2010, 12:50 PM #4
There you go. Where is the line r = new Reader(); ?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-07-2010, 01:10 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Solved!
Thank you very much! I knew it was something small and silly, but I really couldn't see it :s
- 01-07-2010, 01:14 PM #6
You're welcome.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
NullPointerException
By mjz in forum JDBCReplies: 1Last Post: 08-06-2009, 11:46 AM -
NullPointerException
By Aika in forum New To JavaReplies: 8Last Post: 11-18-2008, 11:34 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks