1

I'm trying to get a string value from external file with this method:

File file = new File(name);
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line = br.readLine();
    br.close();

And I have some lines that are equal to "null". But if I compare them to null or "null", or even to "", I get FALSE. What am I doing wrong?

7
  • 3
    How do you compare them? That's the relevant code. (Also, what is in the line, "null", null, empty string, or you really are getting the java null value? Commented Mar 28, 2016 at 4:03
  • Don't you need a loop to traverse through all the lines present in file. Right now it should be returining just first line and ending. Commented Mar 28, 2016 at 4:04
  • equal to null or String "null" ? Commented Mar 28, 2016 at 4:10
  • How some the lines can be null? It may be empty instead. Commented Mar 28, 2016 at 4:29
  • I don't know what I am getting from file. There is actually string "null" value, but if i compare it to string, it also returns false Commented Mar 28, 2016 at 4:47

2 Answers 2

3

If you want to check the null or not then you can check like this way:

String a;
if(a!=null) {}

Or, if you want to compare two string are equivalent or no, then you can give condition like:

 String a="hello";
 if(a.equals("hello")) {}

If you get null then you must check if the result is null or not. You can do it like this:

if(result!=null && !result.equals("")) {


}

If the result is null then next condition will not check and your issue will solve.

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

Comments

2

I guess you are trying to compare with equals method. in equal method if the parameter is null then it will return false. Use == operator instead.

Comments

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.