It's not a secret anymore!*
The moose likes Java in General and the fly likes help me in debugging readLine code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help me in debugging readLine code" Watch "help me in debugging readLine code" New topic
Author

help me in debugging readLine code

naresh nallamala
Greenhorn

Joined: Jan 03, 2013
Posts: 6




I am getting a bug on this code...

Bug: Dereference of the result of readLine() without nullcheck in Demo.StringComparison.main(String[])

The result of invoking readLine() is dereferenced without checking to see if the result is null. If there are no more lines of text to read, readLine() will return null and dereferencing that will generate a null pointer exception.

please help me.....
naresh nallamala
Greenhorn

Joined: Jan 03, 2013
Posts: 6
I got it. I need to do null check for strings s1 and s2.

if(s1!=null&&s2!=null)
{
System.out.println("enter first string");;;;;;
String s1=br.readLine();
System.out.println("enter second string");

String s2=br.readLine();

System.out.println(s1.compareTo(s2));
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37990
    
  22
Welcome to the Ranch
You should always use the code button; I have edited your code with it and you can see how much better it looks. But it highlights some indentation problems; you are using tabs, and you have two }s at the same level. You can introduce bugs into your code by incorrect indentation, so please stick strictly to an indentation convention. We have some suggestions here.
What do you mean by having a bug? I think you have used bug-finding software, which has confused you. it has, for example, missed an error of yours. the null test on your Reader (line 10) is unnecessary. You cannot have a null reference in that Reader reference there; if the Reader was not created, you will suffer an Exception in line 9 and line 10 will never be executed.
Your bug software has missed a much more serious error. The problem is that you are not using the conventional format for reading from the keyboard. I use Java7, so I would use try‑with‑resources. The conventional way is used because it works reliably, and can be relied upon to close the stream.
Beware: If you close system.in, you may never be able to open it again. You are better off writing a utility class which deals with reading from the keyboard: all static methods, private constructor never called, and a static field with your BufferedReader in. Then you never close the Reader, so System.in remains open for the lifetime of your JVM. If you are doing advanced programming, you can add a shutdown hook to your utility class which closes all its readers when the JVM terminates.More explanation of the strange‑looking syntax while ((s1 = br.nextLine()) != null)...
  • 1: First thing executed = innermost pair of (). Read a line and assign it to s1.
  • 2: Compare that line with null. The innermost () are needed because != has a higher precedence than =.
  • 3: If the line was not null, go into the loop.
  • 4: Inside the loop s1 has already been assigned to a “real” value.
  • 5: The inner while is unusual; it calls for a repeat if you enter a null line, which you do with ctrl-D or ctrl-Z
  • 6: At the end of the loop, you have the option of entering ctrl-D/ctrl-Z to terminate the iteration.
  • The finally block closes the reader; if an Exception occurs in line 05, the reader will never be created and there is no need to close it.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37990
        
      22
    naresh nallamala wrote:I got it. I need to do null check for strings s1 and s2.

    if(s1!=null&&s2!=null)
    {
    System.out.println("enter first string");;;;;;
    String s1=br.readLine();
    System.out.println("enter second string");

    String s2=br.readLine();

    System.out.println(s1.compareTo(s2));
    }
    As you can work out (see from my last post), that code will not ensure null‑safety for your application.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: help me in debugging readLine code
     
    Similar Threads
    Flagging duplicate lines when comparing the input from two seperate text files
    Kindly help me
    Problem in file transfer
    File Comaprison
    How Elegant Is This Solution? Can You Offer Me Recommendations?