Java.null.Exception

Discussion in 'Programmer's Corner' started by hitmen, Nov 12, 2008.

  1. hitmen

    Thread Starter Active Member

    Sep 21, 2008
    159
    0
    I had a lab assignment in which I have difficulty. My program is supposed to read a text known as "names.txt". Thereafter, if the name is less than 5, the names will be written to "small.txt" else it is written to "big.txt". However, my program only reads alternate names from "names.txt". Thereafter, it produces a java.null exception. What is wrong??

    I have already changed my program so that the pwstream is b4 the while loop. However, how my program reads only alternative names from my name.txt. Thereafter, it reads the null string and then produce an error message. Here it is:

    Here is my original text file ("names.txt"):

    Alex
    Beckham
    Giggs
    Blanc
    Neville
    Scholls
    Keane
    McDonald
    KennyRoger
    BurgerKing
    AandW
    PHut

    and here is my program:

    import java.util.Scanner;
    import java.io.*;

    public class lab8 {

    public static void main(String[] args) {
    try{
    Scanner sc = new Scanner (System.in);

    //Step 1: create and open file stream
    FileReader openfile = new FileReader ("names.txt");
    BufferedReader brstream = new BufferedReader (openfile);

    //Step 2 : must write into new file
    FileWriter fw1= new FileWriter ("small.txt");
    BufferedWriter bw1 = new BufferedWriter (fw1);
    PrintWriter pw1 = new PrintWriter (bw1);

    FileWriter fw2= new FileWriter ("big.txt");
    BufferedWriter bw2 = new BufferedWriter (fw2);
    PrintWriter pw2 = new PrintWriter (bw2);

    //Step 3: check length of name
    String name;
    while ( (brstream.readLine() != null) ){
    name = brstream.readLine();
    System.out.println("Name is " + name);
    if (name.length() < 5){pw1.println(name);}
    else { pw2.println(name) ; }
    }//close while

    //step 4: close all the stream
    brstream.close();
    pw1.close();
    pw2.close();}//close try

    catch(FileNotFoundException e)
    {System.out.println("File not found!"+e.getMessage() );
    System.exit(0); }

    catch(IOException e)
    {System.out.println("IO Error! "+ e.getMessage());
    e.printStackTrace();
    System.exit(0);}

    }
    }

    Here is the output:

    Name is Beckham
    Name is Blanc
    Name is Scholls
    Name is McDonald
    Name is BurgerKing
    Name is PHut
    Name is null
    Exception in thread "main" java.lang.NullPointerException
    at lab8.main(lab8.java:36)

    What is wrong?:(
    Thanks! :)
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    The reason your code is skipping names in the input file is that your are calling readLine twice per loop iteration: once in the test and once in the loop body. This is one time too many.
    Your code is throwing the null exception for the same reason. Assuming that the readLine call in the while loop condition has read the last name in the file and doesn't return null, the next call to readLine will return null in name. The expression name.length() is probably the one that is throwing the exception.

    Mark
    Code ( (Unknown Language)):
    1.  
    2. //Step 3: check length of name
    3. String name;
    4. while ( (brstream.readLine() != null) ){
    5. name = brstream.readLine();
    6. System.out.println("Name is " + name);
    7. if (name.length() < 5){pw1.println(name);}
    8. else { pw2.println(name) ; }
    9. }//close while
    10.  
     
  3. hitmen

    Thread Starter Active Member

    Sep 21, 2008
    159
    0
    Thanks Mark! I tried your suggestion and I altered my code. However, it only reads in the first name Alex (in the first line) then it loops infinitely. Here is my code again. Also, can you suggest what I should do to the program?

    //Step 3: check length of name
    String name = brstream.readLine();

    while ( (name != null) ){

    System.out.println("Name is " + name);

    if (name.length() < 5){
    pw1.println(name);}

    else {
    pw2.println(name);}
    }//close while




    I have also tried using a do while loop but the program will crash because it will read the program one last time when name is null.:(
     
    Last edited: Nov 14, 2008
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I put your code in a [$code] ... [$/code] block to make it more readable. (Don't use the $ character - those are to keep the browser from parsing these.)

    Your problem now is that your call to readLine occurs only once, just before the loop starts, and doesn't occur inside the loop body. Think about what happens each time the loop runs. To be able to write code in whatever language, you need to be able to look at a section of code and predict correctly what it will do.

    Here's how you can "play computer." Make a table headed by name and name.length. Before the loop starts, what values do these have? Each time through the loop, how do these change? What will cause the loop to terminate?
     
  5. hitmen

    Thread Starter Active Member

    Sep 21, 2008
    159
    0
    I understand why. But the problem I am facing is that if I leave "name" to be undeclared before the loop then it wouldnt compile. :(
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I'm not suggesting that you can use name without declaring it first, but the line you're referring to does more than declare name; it also initializes name with the value returned by readLine.
    Code ( (Unknown Language)):
    1.  
    2. String name = brstream.readLine();
    3.  
    What I said before was that your code calls readLine once before the loop is entered, and then never calls it inside the loop. It should be fairly obvious that to process a list of names, your code will have to call readLine each iteration of the loop. You also need to take care not to read past the end of the list of names.