Results 1 to 3 of 3
  1. #1
    Tellier is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Exclamation [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:
    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)
    Some help would be much appreciated!

    Hint: I know that the actual code is a mess, but I'll take care of that later (but constructive feedback is always good)

    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    Tellier is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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
    FileWriter fw = new FileWriter("Register.txt", true);
    In order to add new names I just wrote
    bw.write(inputName + " " + inputAge + "\r");
    and it took care of the problem... so far.

Similar Threads

  1. Java.Lang.NullPointerException, but I am not sure why???
    By buildakicker in forum New To Java
    Replies: 22
    Last Post: 07-21-2011, 01:20 AM
  2. java.lang.NullPointerException
    By alaa in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:21 PM
  3. java.lang.NullPointerException
    By nitinverma in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-15-2010, 03:56 PM
  4. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 03:12 PM
  5. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 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
  •