1

I am having another problem with NullPointerException. This time it is highlighting tellen = telno.length(). Beore you ask:

  • There is data in the text file to be read to that variable.
  • All variables were initialized.
  • Buffered Reader is also initialized.
  • Here is a snippet of my code:

    while((telno = br.readLine()) != null){
        name = br.readLine();
    
        surname = br.readLine();
    
        company = br.readLine();
    
        house = br.readLine();
    
        street = br.readLine();
    
        locality = br.readLine();
    
        postcode = br.readLine();
    
        telno = br.readLine();
        tellen = telno.length();
    
        mobno = br.readLine();
    
        sep = br.readLine();
    
        if(tellenx > tellen) tellen = tellenx;
    
    
    }
    

Please help. Thanks. Text file:

  • Name
  • Surname
  • Company
  • House
  • Street
  • Locality
  • Telephone number
  • Mobile Number

Problem is in the telephone (tellen) All these names are fictional, and this program is a telephone directory

George
Farrugia
Some Company 
9, Flowers
Quail Street
Bubaqra
BBQ 1569
21369854
79825643
--------------------------------------------
Paul
Zammit

9
Bird Street
St. Julians
STJ 0000
21545796
79745247
--------------------------------------------
Peter
Spiteri
Oak Company
Trees
Birch Street
Oakgrove
TRE 3333
21323323
99323323
--------------------------------------------

The blank after Zammit is a space. That is placed if there is no data to avoid a problem like this.

4
  • Why are you using readLine() repeatedly without null check? Commented Jan 31, 2013 at 18:09
  • Please post the stack trace for the null pointer exception. It probably has a clue to offer.. Commented Jan 31, 2013 at 18:09
  • Can you post a sample of the input file. You say that there is data for all fields it would be nice to see a sample. Commented Jan 31, 2013 at 18:11
  • 1
    You realize that you do tellno = br.readLine() twice, and ignore the first value right? Commented Jan 31, 2013 at 18:12

4 Answers 4

1

This error is getting caused because you are reading 11 lines in one while loop instead you have to read 10 lines only.

So at some point br.readLine() will return null.

You need to read file according to your need that means in one go (your while loop) read 10 lines, then next 10 lines and so on.

while((telno = br.readLine()) != null){ // first line
    name = br.readLine();   // secondline I think this should be first line

    surname = br.readLine();

    company = br.readLine();

    house = br.readLine();

    street = br.readLine();

    locality = br.readLine();

    postcode = br.readLine();

    telno = br.readLine();
    tellen = telno.length();

    mobno = br.readLine();

    sep = br.readLine();  // eleventh line

    if(tellenx > tellen) tellen = tellenx;


}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for explaining this to me. With your answer I managed to solve this problem. Thanks again. When I'll have 15 rep points, I'll vote you up:)
You are welcome. Just a advice. Next time when you caught in this problem try to debug and find the problem yourself. It will help you in long run. If you are using Eclipse then Eclipse Debugging Tutorials
Actually, I am using BlueJ. To be frank, I am still studying for my 'O' Levels / GCSE Equivalent which is why I am not experienced in these matters.
1

Most probably its the end of the file and your bufferedReader.readLine() is returning null and you just invoked a method on an instance which is null, which leads to NPE.

2 Comments

There is data in the text file. None of the lines are null. It was working fine before I added another set of data. Same fields as this is what the program was designed to do.
@markscamilleri at the end of the file, if there is nothing to read br.readLine() returns null.
0

I guess you are trying to read using readLine() without null check.Definitel, it will give NPE.

Comments

0

Adding an if statement to check if there is any null references is a good practice to check for an error in your code.

An example in pseudo code would be:

   telno = br.readLine();

   if(telno == null)
       System.out.println("this will cause null pointer exception  "+ telno.length()); 
   else
       tellen = telno.length();

2 Comments

Tried it, and it is giving me a NullPointerException when there IS data. I don't know what's happening.
I think you mean to say == and you also need to remove lines after if and else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.