Results 1 to 4 of 4
- 03-22-2011, 11:19 PM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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:
This is my enhanced for loop and closing code:Java Code:BufferedWriter writer = new BufferedWriter(new PrintWriter(file));
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?Java Code:for(String s : usernames) { writer.write(s); writer.newLine(); } writer.flush(); writer.close();
Thanks a lot,
~Solarsonic
- 03-22-2011, 11:36 PM #2
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.
- 03-22-2011, 11:52 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
- 03-23-2011, 12:08 AM #4
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
-
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
Null Pointer Exception with BufferedWriter
By Sasquatch192 in forum New To JavaReplies: 3Last Post: 10-26-2009, 06:32 PM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks