how to validate the null string array (Android Java) - java

I have a question about how to check either the array string got the null value.
My code is like below but still got the string return even the value is null.
for (int i=17;i<29;i++)
{
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
{
Log.d("Get additional label","Additional label = "+label[i]);
}
}
Problem Solved
The problem solved when I change from
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
to
if (label[i].length() != 0)
Thanks for those who replied :)

You could try using the StringUtils api from the apache commons lib:
if(!StringUtils.isEmpty(label[i]))
Check out the docs for more details.

Use == or != to check for null.
e.g. label[i] != null and also the way have it now label[i].equals should end up in NPE if it's really null.

Are you sure your string is actually null and not just an empty string?

Related

What is the difference of null position placedin front and at back

In java, what is the difference between this 2 lines of code, or the second line of code don't even exist
if (name != null){}
if (null != name){}
the real code is something like this
if ( null != name&& !StringUtils.isEmpty(name) )
by the way, I have tested this code and it works
There is no difference between your two top lines of code as both do a valid null check. The main issue is if you did something like this:
if (myString.isEmpty() && (myString != null)) {...}
This would throw a NullPointerException if myString is null since you're dereferencing the variable before doing the null check. Better to do instead:
if ((myString != null) && myString.isEmpty()) {...}
The && operator does a boolean AND test, and will short circuit, will end and not do the right sided test if the test on the left is false.
There is no difference, != is a Logical Operator, and is checking to make sure they are not equivalent, so it doesn't matter which side things are on. For example:
String a = "a";
if(a != null)
{
System.out.println("they are not equal");
}
if(null != a)
{
System.out.println("they are not equal");
}
returns
they are not equal
they are not equal
Difference is in that you cannot assign to NULL anything.
If you would make typo and miss a ! sign within
if (name != null) {}
You could possibly make
if (name = null) {}
And by that assign null value to name
Read this
https://knowthecode.io/yoda-conditions-yoda-not-yoda

Depreciated CellType method

I've seen other forums here with suggestions for solutions I don't get any of them to work.
I want to check if a cell is either null or blank, the depriciated code that I use is (both getCellType() and CELL_TYPE_BLANK is depreciated):
if( (c == null) || c.getCellType() == c.CELL_TYPE_BLANK){
//do something
}
For example I've been looking at solution in this thread:
Alternative to deprecated getCellType
and I was thinking that a solution could possibly look like this:
if( (c == null) || c.getCellTypeEnum() == CellType.BLANK){
//Error: incomparable types: org.apache.poi.ss.usermodel.CellType and int
//do something
}
or
if( (c == null) || c.getBooleanCellValue()){
//do something
}
but it does'nt work and apaches documentation is not that helpful either. Does anyone have a solution that does'nt produce warnings? I'm using poi 3.17.
BR
I've been struggling with the same issue and I found the following works for me.
It is the inverse of your approach.
I check if the value is not null.
The code below is for handling string values:
private static String checkForNullString(Cell cellToCheck) {
String strCheck;
if (cellToCheck != null && cellToCheck.getCellTypeEnum() != CellType.BLANK) {
cellToCheck.setCellType(CellType.STRING);
strCheck = cellToCheck.getStringCellValue();
}
else
strCheck = "";
return strCheck;
}
To handle numeric values, simply change the following:
cellToCheck.getStringCellValue();
to
cellToCheck.getNumericCellValue());

Why is the code ignoring one of the IF requirements?

I'm sure it's obvious but can't figure out what I'm missing. Code is:
} else if (flag != null && date != null &&
!date.equals("") && (disability != null || flagEnd == null))
//do stuff
}
I've double checked the variables. flag is not null, date IS null, disability is null and flagEng is null. The code shouldn't be stepping into this because the date is null, and yet it is. Do I have this written incorrectly?
Note: date is a string. flagEnd is a java.util.Date. Thanks!
Edit: Here is the code that creates the date variable.
String date = (rs.getString("date") != null && rs.getString("date").length() > 0 ? rs.getString("date") : "NULL");
Instead of date != null, should I maybe instead have this?
!date.equals("NULL")
The string was actually storing the word "NULL", as opposed to being an empty string. Once I changed the code to !date.equals("NULL"), it skipped the ELSE IF statement like it should have.

Checking null String in android

I am developing an android application.I am getting a String value as null from webservice. I am fetching the value and storing it in a String variable. Then when I print the value using Log, like Log.i("tag", "````!!!cell >>"+cell);, I an getting null printed in the screen. Now what i need is that I need to check the variable for 'null' and I want to display a textfield with no value if it is 'null'. I am using the following statement for checking
if(!cell.equals(null) || !cell.equals("")) {
_______
} else {
_______
}
But the control is not going inside the else part if the value us 'null'
Please give me a solution.
Thanks in advance.
when cell is null , and you are trying to invoke a method on it, you will hit by a null pointer exception.
I'd say
if(cell !=null && !cell.isEmpty()) {
_______yes, disply
} else {
_______nope, some thing wrong
}
its not equals(null) its
if(cell != null || !cell.isEmpty())
Try TextUtils.html#isEmpty(java.lang.CharSequence)
I would give try this, seems to be working for me!
if(TextUtils.isEmpty(yourString) && yourString == null){
}
else if(!TextUtils.isEmpty(yourString) && yourString != null){
}
If the value of the string is null, !cell.equals("") will evaluate to true and hence it will go in the if part and not the else part as you are using an OR condition.
NULL != "" (Empty string)!!!
Use this :
if (cell != null && cell.trim().length() > 0) {
// do whatever you want
} else {
// the string received is null
}
Android provides a simple utility
TextUtils.isEmpty(<<stringVariable>>);
More details # http://developer.android.com/reference/android/text/TextUtils.html
WORKING !!!
if (string.matches("")&& string.trim().equals("null")){
return false;
}
else{
return true;
}

How to check if there is text within my JButton

I want to check whether there is something in my JButton. What would i insert into the equalsIgnoreCase() area?
if (jButton1.getText().equalsIgnoreCase("") &&
jButton2.getText().equalsIgnoreCase("") &&
jButton3.getText().equalsIgnoreCase("") &&
jButton4.getText().equalsIgnoreCase("") &&
jButton5.getText().equalsIgnoreCase("") &&
jButton6.getText().equalsIgnoreCase("") &&
jButton7.getText().equalsIgnoreCase("") &&
jButton8.getText().equalsIgnoreCase("") &&
jButton9.getText().equalsIgnoreCase(""))
To check that there is a text, you can do:
!jButton1.getText().isEmpty()
or, if you want to exclude a text that only contains spaces:
!jButton1.getText().trim().isEmpty()
You can use
jButton1.getText().isEmpty()
(use ! to negate if you want to know it's not empty...)
or you can check the length of the value
jButton1.getText().length > 0
you can use
bool somethingIn = !jButton1.getText().isEmpty();
Nothing, just what you did. But you should put exclamation symbols before conditions to negate them:
if (!jButton1.getText().equals("") &&
!jButton2.getText().equals("") &&
!jButton3.getText().equals("") &&
!jButton4.getText().equals("") &&
!jButton5.getText().equals("") &&
!jButton6.getText().equals("") &&
!jButton7.getText().equals("") &&
!jButton8.getText().equals("") &&
!jButton9.getText().equals(""))
In this case body of if will be executed if all your JButtons have some text. And ignoreCase is not necessary. Emptiness has no lower or upper case ))

Categories

Resources