Results 1 to 6 of 6
  1. #1
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default 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:
    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();
        }
    
    }
    Reader's Code:
    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;
        }
    }
    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:
    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!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  3. #3
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Sorry my bad code editing! It's defined just after "File f;" as "Reader r;"

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    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

  5. #5
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default Solved!

    Thank you very much! I knew it was something small and silly, but I really couldn't see it :s

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

Similar Threads

  1. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 11:46 AM
  2. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-18-2008, 11:34 PM
  3. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  4. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •