I'm trying to get values from Json object and I have a problem. I'm using getint function to get value but the value is null and getint function gving error.
How can I solve this problem?
Code :
firmInfo.setFirmID(object.getInt(Constants.FirmID));
Thanks.
Assuming that object is of type JSONObject you can use
object.optInt(Constants.FirmID)
or
object.optInt(Constants.FirmID, defaultValue)
You can check if the object you recieve is an instanceof JSONObject before you try to getInt(). Also you need to check if null before passing as a param to your getInt(). Like below
if(Constants.FirmID != null){
firmInfo.setFirmID(object.getInt(Integer.parseInt(Constants.FirmID)));
}
Check this link
getint gives error message if there is no such key in JSONObject or your error is while setting it to firmInfo
check whether the id is present or not using
object.has("Constants.FirmID")
if it has the key , check whether it is null or not
if(String.valueOf(jArray.getInt("sdfgh")) != null)
{
// add your code here . . . . .
}
or
if(String.valueOf(jArray.getInt("sdfgh")).length < 1)
{
// add your code here . . . . .
}
Related
This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 6 years ago.
May occur any exception in coding for checking string is null or not ? Please help.
String code ;
if (!code.equals(null)) {
}
else
{
}
Here is how you can check if String value is null or not
if(code != null){
}else{
}
You can not us !code.equals(null) because, equals is used to compare same object type. null is not any object type and code is String. If you consider null as String, then you can use !code.equals("null")
String can be checked like this:
if(code.equals("null") || code.equals("")) {
// code to do when string is null.
}
else {
// code to do when string is not null.
}
equals() is use to check the similarity of two String, and == or != is use to check the condition. In your case you are checking the similarity of string.
if (!code.equals(null)) {
//code checks that String code is equal to null or not
}
else
{
}
another
if (code != null) {
//code checks if code is not equals to null (condition checking)
}
else
{
}
There are many ways to check if String is empty in Java, but what is the right way of doing it? right in the sense of robustness, performance and readability. If robustness is your priority then using equals() method or Apache commons StringUtils is the right way to do this check. If you don't want to use third party library and happy of doing null check by yourself, then checking String's length is the fastest way and using isEmpty() method from String is most readable way. By the way, don't confuse between empty and null String, if your application treat them same, then you can consider them same otherwise they are different, as null may not be classified as empty. Here are three examples of checking String is empty or not by using JDK library itself.
Read more Here
You can't use .equals(null) to make a null check, because the API description for Object#equals states that:
For any non-null reference value x, x.equals(null) should return false.
Not only would this be a useless check (since it always returns false), it would also throw a NullPointerException if code actually was null, because a null value does not define an equals method.
Object x = null;
boolean isNull = x.equals(null); // NullPointerException on .equals
The only practical way to do a null check is to use:
if (code != null) {
}
If you want to check whether string is empty i.e. null or "" then use
if(TextUtils.isEmpty(code)){
}else{
}
equals checks the value exists in string.
Hitting a brick wall in my code at the moment for fetching json objects from multiple pages(using a loop) in a AsyncTask. It reaches the last page, but getting the correct if statement to ensure that the loop DOESN'T run again and continues on is baffling me.
String data = //some correct json data with next element that holds a uri parseable string
JSONObject initial = new JSONObject(data);
String next = initial.getString(nextObjSTR);
//gonna start from the "last" page and recursively return to the 1st page
if(*The if condition I need help with*) {
//there is another page
makeConnection(Uri.parse(next));
}
Basically, the last page of json elements has a next element with a null or no element value, which triggers the IOException error caught in makeConnection method because my initial if statement has always been failing.
Can I get a reason or help as to the appropriate if check for Strings from json? I've tried String != null as NullPointerExceptions occur if I use any method from String to compare. Likewise, JsonObject.NULL comparison doesn't work for me either.
None of the other answers worked, and I ended up questioning whether the element was really null despite looking at the parsed json data via an online tool. In the end, JSONObject.IsNull(element mapping name) is the right approach.
If you're sure that the value is either null (empty) or a correct URI, and assuming that the nextObjSTR key is always present in the data JSON, then that will do:
if (next != null && !next.trim().isEmpty()) {
makeConnection(Uri.parse(next));
}
Or, since you're on Android, it's better use the more convenient method:
if (!TextUtils.isEmpty(next)) {
makeConnection(Uri.parse(next));
}
You can use the optString Method of the JSONObject. If the JSON key is not this method will return a empty string, so you can check it easily:
String next = initial.optString(nextObjSTR);
if ( ! next.isEmpty() ) {
makeConnection(Uri.parse(next));
}
Source: https://developer.android.com/reference/org/json/JSONObject.html#optString(java.lang.String)
you must check value with key is has in json object.
Try below code:
JSONObject initial = new JSONObject(data);
if(initial.has(nextObjSTR)) {
String next = initial.getString(nextObjSTR);
if (next != null && !next.isEmpty()) {
makeConnection(Uri.parse(next));
}
}
I do like this...
String value;
if(jsonObject.get("name").toString.equals("null")){
value = "";
else{
value = jsonObject.getString("name");
}
I am getting some data from server and setting the values in a getter and setter class. When the values seems to be null i am getting following values in my getter setter class
Tag [seatNo=null, Value=null, player1=null, player2=null, player3=null, player4=null, player5=null ]
The above value is been printed in Logcat
after getting above values i am checking an if condition because of which the app crashes
if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))
{
}
how to check it is null or not ?
First you need to check if the value is not empty.
if(!TextUtils.isEmpty(bet.getSeatNo())){
//Check your condition here
}
Try doing this way:
if (bet.getSeatNo() != null)
{
if(!bet.getSeatNo().isEmpty() && !bet.getSeatNo().equals("null"))
{
}
}
Before checking the field empty/null, check whether "bet" object is null or not.
NullPointerException occurs while accessing fields of null object.
if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))
If bet.getSeatNo() != null is false, only then the second !bet.getSeatNo().isEmpty() is evaluated. But since the first only evaluates to false if getSeatNo() returns null, the second conditional must NPE. Notice the problem?
Use && instead of || so that the short-circuiting stops on first false and not on the first true.
My JSON stream can be different each time. For example sometime it can include a "Song" field and sometime not.
I am getting this fields value asText ? How to tell Jackson to get this value as an Empty String if it is not defined ?
Example
"Content": "MusicContent",
"Song": "Track_1",
if try node.get("Song").asText() it will give "Track_1"
"Content": "MusicContent",
Now , if i try to get node.get("Song") it gives null pointer exception. I want to get an empty string when calling asText().
How can i do that ?
Thanks
You could check for null before calling the asText() on the node. i would probably do it like this :
if (node.get("Song") != null){
myString = node.get("Song").asText();
} else {
myString = "";
}
Or in a fancy way like this :
myString = ((node.get("Song")!=null) ? node.get("Song").asText() : "");
I am working on json parser. In json response some values contains boolean or null values, then how to check whether it is boolean or null? For example : user = false or user = null . At the time of parsing it gives exception as "user is not jsonobject".
JSONObject json = new JSONObject(query);
JSONObject info = json.getJSONObject("info");
JSONObject user = info.getJSONObject("user");
Thanks,
Vishakha.
Have you tried ingo.optBoolean("user")?
From the SDK documentation for optBoolean:
Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.
Note that null will be treated as a false value in this example.
isNull() you can use if(json.isNull("user")||your conditions)
I think the problem might be that you are trying to get a JSONArray with getJSONObject(). try json.getJSONArray("info"). I had the same problem that i was trying to do if(object.getJSONObject("string") == null) but it threw an exception when trying to get the object to check for its value, and .optBoolean worked perfectly
JSONObject json = new JSONObject(data);
String user = json.getString("user");
variable.setUser(courses_icon_file_name == null ? "": user);