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.
Related
I print the fotonew key and it sends a null value. But if urlfoto is empty or not, if (urlfoto! = Null &&! Urlfoto.equals (" ")) goes to this section. Normally he should go to else. The urlfoto value goes to the part with a full urlfoto value in the case of null but if else. I want to check if the key I pulled is empty or full.
SharedPreferences sharedPrefNew = getContext().getSharedPreferences("sharedPref",Context.MODE_PRIVATE);
urlfoto = sharedPrefNew.getString("fotonew", "");
if(urlfoto!= null && !urlfoto.equals(""))
{
...
}
else {
System.out.println("boş");
...
}
You already declared default value of SharedPreference on Code.
urlfoto = sharedPrefNew.getString("fotonew", "");
This means if "fotonew" is empty or null sharedPrefNew returns a value ""
this line:
sharedPrefNew.getString("fotonew", "")
is responsible for taking value from shared pref. First parameter is a key, second one is a default value that will be returned if key is not found.
In your case, if there is no object stored for key "fotoNew" an empty string will be returned.
You can return null :
sharedPrefNew.getString("fotonew", null);
Hope that helps.
Check below code
if (sharedPrefNew.getString("fotonew", "") != null && !sharedPrefNew.getString("fotonew", "").isEmpty()){
//your code here
}
I'm aware that getFlightKey is returning null during the initialising itself. It should be null as I'm using my code to build apks for two types of Users. For one user, "flight Key" is used and for the other, it is not used.
else {
tabletMetadata = new TabletMetadata(new DateTime(), getActiveEmployeeIdentifier()
,getAirFiApplication().getTabletId(),getFlightMetadata().get().getFlightKey()
,flightLegIdentifier, new DateTime(), level,
location != null ? String.valueOf(location.getLatitude()) : null,
location != null ? String.valueOf(location.getLongitude()) : null, null, level, null, null);
getFlightMetadata().get().getFlightKey() will be null but how can I add something similar to null check handle this?
If you want to handle both the condition with the same piece of code you can do something like this
String value="";
try{
value = getFlightMetadata().get().getFlightKey();
}catch (NullPointerException e)
{
}
now, relace getFlightMetadata().get().getFlightKey() this with value and it will not be null instead it will have blank value or no value but will not be null.
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.
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 . . . . .
}
In a custom SimpleCursorAdapter, I'm trying to compare a status String, with confusing results.
My string is initialised from the cursor like this (and I've checked with toast that it contains the expected values).
String visitStatus = cursor.getString(cursor.getColumnIndex(CallData.COLUMN_VisitStatus));
visitStatus can be null, Open, Cancelled or Complete.
If I try to compare visitStatus to "any string in quotes", the app crashes with a NullPointerException. Only if I compare to null do I get anything at all - and that is no use to me
if(visitStatus.equals(null)) // the app crashes with a NullPointerException
if(visitStatus == null) // doesn't crash
if(visitStatus != null) // doesn't crash
if(visitStatus == "Complete") // doesn't crash or do anything
if(visitStatus.equals("Complete")) // the app crashes with a NullPointerException.
Basically, I can compare to null, but only in the way that isn't supposed to work. I can't compare to actual strings such as "Open" or "Complete".
I'm going slightly nuts with this, and am badly missing my C# comfort zone. This particular activity is a nightmare of listfragments, contentproviders, customadapters, viewpagers, pagertitlestrips and list row xml templates!
halp!
This is because visitStatus is null. Whenever you try to access its methods, it crashes. (That is: visitString.equals(), visitString.length(), etc., all will crash.)
However, the equality operator (==) supports null parameters on either side of it. (So, if (null == null) is a valid check.)
You should check like this:
if (visitStatus != null && visitStatus.equals("Complete")) {
// ...
}
Or, you can do "Yoda syntax" (backwards checking), which supports null parameters:
if ("Complete".equals(visitStatus)) {
// ...
}
Also, a final note: You cannot compare string contents using == (as in, you cannot do "a" == new String("a"), nor visitString == "Complete"). For a detailed explanation on that, see this Q&A thread.
String should be compared using .equals()
The NullPointerException caused because the visitStatus is null