Results 1 to 4 of 4
  1. #1
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Arrow Nullpointer Exception with BufferedWriter?

    I'm trying to write out a String array to a text file using a Buffered Writer.

    It works fine when the array has 100 values but when I go higher than that it gives me a NullPointerException caused by the write() method.

    This is my assignment code:
    Java Code:
    BufferedWriter writer = new BufferedWriter(new PrintWriter(file));
    This is my enhanced for loop and closing code:
    Java Code:
    for(String s : usernames) {
    writer.write(s);
    writer.newLine();
    	}
    	writer.flush();
    	writer.close();
    I'm guessing this is because String[] arrays can't hold that much memory, if so what can I use to duplicate what I want?

    Thanks a lot,
    ~Solarsonic

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Not really sure from the info provided.

    When you create an object array, it is filled with the default value of null. So if you create a String array with a length of 100 and fill 70 slots with Strings the other 30 slots will be null. If you then iterate over the full array and try and write all values out to the file then when it hits the 71st slot it will throw the NPE.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Not really sure from the info provided.

    When you create an object array, it is filled with the default value of null. So if you create a String array with a length of 100 and fill 70 slots with Strings the other 30 slots will be null. If you then iterate over the full array and try and write all values out to the file then when it hits the 71st slot it will throw the NPE.
    Nope, that's not it. Checked it thoroughly just now for that.

    What other possibilities are there? I'm familiar with the NullPointerException, I just can't seem to spot the problem for this. Is it possible that the writer is writing too much at once and too fast? Would the wait() method solve the problem?
    Last edited by Solarsonic; 03-23-2011 at 12:00 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The NPE message provides valuable information such as the stack trace. Each method that has been called is included as well as a line number. Go through the stack trace and find the first method that you wrote is listed. go to the line mentioned then look at evrything on that line that maybe null. On the previous line place a print statement to print out all the things that maybe null (if there are more than one then use several print statements. When you have determined exactly what is null then do a bit more detective work and see why it is null.

    No it has nothing to do with memory or speed.

Similar Threads

  1. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  2. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  3. Null Pointer Exception with BufferedWriter
    By Sasquatch192 in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 06:32 PM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

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