Results 1 to 3 of 3
- 08-10-2012, 12:29 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
[Solved] Yet another java.lang.NullPointerException - while using the Writer
I'm quite new to the world of Java development and after struggling with different projects I decided to actually complete an "advanced" (for me) program. While playing around with the Writer.class and storing information in .txt files I came across the java.lang.NullPointerException. I've read some other threads about the subject but I can't find the solution to my own problem.
The only thing that I know is that I know is that, after running the program once and storing 2 lines of information I get an error when I start the program again and try to add more lines of text. I've fail searched my vector and when it doesn't contain any information even though it returns 2 when I do person.size().
Edit: Forgot to mention the specific Exception, here we go:
Some help would be much appreciated!Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:157)
at name.and.age.Register.inputWriter(Register.java:47 )
at name.and.age.Register.main(Register.java:90)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Hint: I know that the actual code is a mess, but I'll take care of that later (but constructive feedback is always good).gif)
Java Code:package name.and.age; import java.io.*; import java.util.*; public class Register { static Scanner sc = new Scanner(System.in); public static final Vector<String> person = new Vector<String>(); public void StartProgram() { try{ FileReader fr = new FileReader("Register.txt"); BufferedReader br = new BufferedReader(fr); int lineNumber = 0; while(br.readLine() != null){ lineNumber++; } for(int i = 0; i < lineNumber; i++){ person.add(br.readLine()); } fr.close(); } catch(IOException ioe){ //ignore problem } } public void inputWriter() { try { FileWriter fw = new FileWriter("Register.txt"); BufferedWriter bw = new BufferedWriter(fw); String inputName; String inputAge; System.out.println("Enter the name of the person: "); inputName = sc.next(); System.out.println(""); System.out.println("Enter the age of the person: "); inputAge = sc.next(); person.add(inputName + " " + inputAge + "\r"); for(int i = 0; i < person.size(); i++){ bw.write(person.get(i)); } bw.close(); fw = null; } catch(IOException e){ //ignore problem } } public void filePrint() { try{ FileReader fr = new FileReader("Register.txt"); BufferedReader br = new BufferedReader(fr); String s; while((s = br.readLine()) != null){ System.out.println(s); } } catch(IOException e){ System.out.println("Error! "); } } public static void main(String[] args) throws Exception{ Register yes = new Register(); yes.StartProgram(); int choice; do{ System.out.println (""); System.out.println ("1. Add a new person"); System.out.println ("2. List all the people!"); System.out.println ("3. Exit program"); System.out.println (""); System.out.print("Your choice: "); choice = sc.nextInt(); System.out.println(""); if(choice == 1){ yes.inputWriter(); } else if(choice == 2){ yes.filePrint(); } else if(choice == 3){ System.out.println ("Bye!"); } else { System.out.println ("Wrong input!"); } } while(choice != 3); } }Last edited by Tellier; 08-10-2012 at 01:04 PM. Reason: Missed info
- 08-10-2012, 12:54 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Yet another java.lang.NullPointerException - while using the Writer
Supply the full error text and stack trace, highlighting the line in your code on which the exception occurs.
This will involve changing your exception handling to <exception>.printStackTrace().Please do not ask for code as refusal often offends.
- 08-10-2012, 01:02 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: Yet another java.lang.NullPointerException - while using the Writer
Thank you! I actually tried another solution that I while searching on google which took care of problem:
I removed line 46-48 and changed the FileWriter in inputWriter() to
In order to add new names I just wroteFileWriter fw = new FileWriter("Register.txt", true);
and it took care of the problem... so far.bw.write(inputName + " " + inputAge + "\r");
Similar Threads
-
Java.Lang.NullPointerException, but I am not sure why???
By buildakicker in forum New To JavaReplies: 22Last Post: 07-21-2011, 01:20 AM -
java.lang.NullPointerException
By alaa in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:21 PM -
java.lang.NullPointerException
By nitinverma in forum CLDC and MIDPReplies: 4Last Post: 06-15-2010, 03:56 PM -
java.lang.NullPointerException
By Pombi in forum New To JavaReplies: 6Last Post: 05-15-2010, 03:12 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks