0

Here at the line while((anInt=bufferedReader.read())!=-1) iam getting Null Dereference issue when i do my XSS validation on the below code. For this is this enough to check not null or do we have any other check or solution to resolve this..

BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new FileReader(new File (url.toURI())));
    } catch (Exception e) {
        e.printStackTrace();
    }

    response.setContentType("text/plain");
    try{
        int anInt=0;
        //if(!bufferedReader.equals(null)){
            while((anInt=bufferedReader.read())!=-1)
                response.getWriter().write(anInt);
        //}
    } catch(IOException ioe) { }
    return null;

Commented the if condition..

Mdhar9e
  • 1,376
  • 4
  • 23
  • 46
  • 1
    This Question doesn't make a lot of sense to me. Are you talking about a NullPointerException? If so, please add the stacktrace to the Question. – Stephen C Mar 17 '14 at 10:06
  • `BufferedReader.readLine()` returns **null** when it has finished to read. – aran Mar 17 '14 at 10:08
  • 2
    @AsierAranbarri As he isn't calling `readLine(),` your comment has zero relevance. – user207421 Mar 17 '14 at 10:09
  • @EJP it hasn't zero relevance. Now I'm aware of my mistake, so it was useful for me to put that comment here. ; ) – aran Mar 17 '14 at 10:11
  • 1
    @AsierAranbarri It has zero relevance to the question being asked, which is what comments are supposed to be about. It's nice that you learned something from me that you could have learned by reading the question more carefully but again it isn't relevant to the actual question. – user207421 Mar 17 '14 at 10:13

4 Answers4

3

I am getting Null Dereference issue

No you're not. There is no such 'issue'. What you're getting is a NullPointerException. Please be accurate. There is no virtue whatsoever in paraphrasing error messages, or not reading them properly, or whatever it was that led to this error on your part.

The ostensible error here is using bufferedReader.equals() as a test to see whether bufferedReader is null. A moment's thought should convince you of the futility of doing it that way. If it's null, how is calling equals() on it ever going to succeed?

The original error on your part here was poorly structured exception handling. You have code after the catch block that relies on the success of the try block. It should therefore be inside the try block. You will then notice that you only need one catch block ... but please put something inside it, such as exc.printStackTrace(): otherwise debugging becomes a mere guessing game.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

In addition to the bad exception handling pointed out by EJP (and your egregious paraphrasing ...), your code always returns null. That seems kind of pointless.


But the real problem is actually caused by your bad exception handling.

First this:

BufferedReader bufferedReader = null;
try {
    bufferedReader = new BufferedReader(
            new FileReader(new File (url.toURI())));
} catch (Exception e) {
    e.printStackTrace();
}

If the file open fails, you catch the exception AND CONTINUE. That is your first mistake. You should NOT catch the exception there, because you are not ready to handle it there.

Next this:

if (!bufferedReader.equals(null)) {

I expect this is supposed to guard against bufferedReader being null. But in reality, if bufferedReader is null, then the will cause an NPE to be thrown ... because you will be trying to call a method (equals) on a null target object.

If you want to test if bufferedReader is null, you should code it like this:

if (bufferedReader != null) {

But you wouldn't need to test for null at all if you hadn't tried to handle the previous exception at that point ...


ok. i am removing that .equals condition

Sigh. Stop hacking the code and try to understand the Answers.

Removing the test without fixing the first problem is just moving the place where an NPE is going to be thrown.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

bufferedReader.equals(null) should throw a NullPointerException to check bufferedReaderis null or not you can do bufferedReader != null.

Milaci
  • 515
  • 2
  • 7
  • 24
  • Only if `bufferedReader` is null. – user207421 Mar 17 '14 at 10:14
  • Of course, if bufferedReader is not null No NullPointerException should throw. – Milaci Mar 17 '14 at 10:45
  • 1
    For clarity, if `bufferedReader` is null, `bufferedReader.equals()` will throw a `NullPointerException.` If it isn't null, it won't, and indeed even calling it is pointless if you've already tested it for `!= null.` – user207421 Mar 17 '14 at 23:52
-1

bufferedReader may throw EoFException you must handle it!

RayanFar
  • 539
  • 11
  • 28
  • There are no methods of `BufferedReader` that throw `EOFException.` All the `read()` overloads return -1 at EOS, and `readLine()` returns `null.` -1 – user207421 Mar 17 '14 at 23:47