GuyInSombrero
GuyInSombrero

Reputation: 67

Null String or not Null String?

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?

Upvotes: 1

Views: 109

Answers (2)

Pravin Fofariya
Pravin Fofariya

Reputation: 346

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.

Upvotes: 3

stinepike
stinepike

Reputation: 54732

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.

Upvotes: 2

Related Questions