Reputation: 1518
Normally I make a comparison in every activity(Android) but I have a problem and I couldn't find the solution. I tried everything I could but result is still the same.
In if statement null
value seems to be not null.
What am I doing wrong?
Here is the code :
System.out.println(item.getSub()); // output is null
if (item.getSub()!=null) {
r.putExtra("engsub", item.getSub()); // Normally it shouldn't, but it goes in here
startActivityForResult(r, position);
}
else {
//Do something
}
And I tried this :
String def = item.getSub()+"";
System.out.println(def); // output is not null
if (!def.equals("")) {
r.putExtra("engsub", item.getSub()); // Normally it shouldn't, but it goes in here
startActivityForResult(r, position);
}
else {
//Do something
}
Added :
JSONArray jsonResponse = new JSONArray(result);
asd = new String[5][jsonResponse.length()];
rowItems = new ArrayList<RowItem2>();
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject js = jsonResponse.getJSONObject(i);
// ....codes
asd[4][i] = js.getString("engsub");;
item = new RowItem2(asd[0][i], asd[1][i], asd[2[i],
asd[3][i],asd[4][i]);
rowItems.add(item);
}
RowItem2 class
private String engsub;
public RowItem2(String title, String desc,String desc2,String desc3,String engsub) {
this.engsub = engsub;
//......
}
//.......
public String getSub() {
return engsub;
}
public void setSub(String engsub) {
this.engsub = engsub;
}
Upvotes: 0
Views: 136
Reputation: 29722
I think JSON string result
contains the word "NULL" in it.
Look at the docs about JSONObject on Android Developer site.
Here is the line that solves your problem:
When the requested type is a String, other non-null values will be coerced using valueOf(Object). Although null cannot be coerced, the sentinel value NULL is coerced to the string "null".
Below some ideas when trying to track down an issue like this:
You sound confused, so set your mind at ease. There is nothing wrong with Java / Android. Confirm this with a change that will work correctly every time.
This change challenges your assumption: you assume that item.getSub()
is returning null
.
String nullString = null;
System.out.println(nullString);
if (nullString != null) {
r.putExtra("engsub", item.getSub()); // Normally it shouldn't, but it goes in here
startActivityForResult(r, position);
}
else {
//Do something
}
This will NOT pass the if
test, unless there is something wrong with your IDE.
You may still think you are getting null
. In some cases it could look like this.
Sometimes Eclipse will not put the correct code onto your emulator / device. There could be an issue with the build.
So do a project Clean. Then build again and test.
This will confirm that you are running the correct code.
Sometimes when output looks like a "" string, it can be " " or " ".
Try logging your output surrounded by [ and ].
System.out.println("[" + item.getSub() + "]");
This is why I asked to see the item.getSub()
code, and the extra you have shown.
If the above tests don't help, look to see how that value is set / returned.
Looking into JSONObject.getString()
javadoc, it shows that it cannot return a null
value:
In your case, log out the JSON string, and log out the values in RowItem2
constructor.
That should show you the "NULL" I refer to at the top of this answer.
Upvotes: 2
Reputation: 661
You could try:
if (item.getSub() instanceof String) {
r.putExtra("engsub", item.getSub()); // Normally it shouldn't, but it goes in here
startActivityForResult(r, position);
}
else {
//Do something
}
This way you're sure that the item.getSub() is a string.
Upvotes: 1