I have this code. it is the row exception filter in the right hand map of a tMap:
row2.method.equals("Twitter") && (
row2.last_notified.equals(null) ||
row2.frequency.equals("Everytime") ||
(row2.frequency == "Hourly" && TalendDate.diffDate(TalendDate.addDate(row2.last_notified,1,"HH"), TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")), "HH") > 0) ||
(row2.frequency == "Daily" && TalendDate.diffDate(TalendDate.addDate(row2.last_notified,1,"dd"), TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")), "dd") > 0)
)
I have a nullPointerException. I know that frequency is not null, as is method. I know that last_notified is null. based on my logic, all rows with "Twitter" as a value with no last_notified should pass. However, it looks like I failed with this.
I understood that OR statements go from left to right. If the first value comes back True, then the OR SHOULD return true? What am I missing?
Regarding
row2.last_notified.equals(null)
Don't test for null with the equals method as that risks throwing the NPE exception since you'll be attempting to call a method on a null variable. Always use == instead since you're not testing the notified reference's value but rather whether the reference itself is null.
So do
row2.last_notified == null
Also regarding
row2.frequency == "Daily"
It's the exact opposite here as you're comparing Strings wrong -- use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here.
Instead do:
row2.frequency.equals("Daily")
Related
I wrote this code, which passes all the test cases
//passes 1100/1100 cases
while (slow != null && slow.next != null && fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next
}
On the other hand, this code does not, but seems to be very similar.
//passes 1/1100 cases
while (slow != null && slow.next != null && fast.next != null && fast != null) {
fast = fast.next.next;
slow = slow.next
}
I just noticed that just by changing the order of condition statements in while(), the number of test cases that pass changes. Why is it so?
Java if-statements use something called short-circuit evaluation, where if there is an && where the first condition is false, or an || where the first condition is true, then it will not evaluate the rest of the condition, because it already knows what the result will be. If there is an && and the first statement is false, then it doesn't matter what the rest of the statements evaluate to, since the whole thing is false, and vice-versa if there is an || and the first statement is true.
A second issue is that if an object is null, then calling any method or accessing any property of the function will throw a NullPointerException. This can be avoided by checking if an object is null before using it in cases where you know it might be null. In your linked list, evidently you are concerned that a node might be null, so you check before accessing its next.
These two combine together in your example. If you do
if (item != null && item.property != null) {}
and item is actually null, then because of short-circuit evaluation, Java will never actually check item.property to see if it's null as well, because it already knows the combined statement will be false.
Conversely, if you do
if (item.property != null && item != null) {}
then Java will try to access item.property, which will throw a NullPointerException if item is null.
In your case, evidently many of your test cases had situations where fast was null at some point. The example where they passed would have avoided calling fast.next when fast is null because of short-circuit evaluation, whereas the second example never avoids the NullPointerExceptions at all.
Is it possible to do something like this?
dog.setIsBarkingIndicator(mailman.getIsAtDoor() != null && mailman.getIsAtDoor().equals("N") ? false : true);
But for what I researched, this is basically the same as: dog.setIsBarkingIndicator(mailman.getIsAtDoor() == null || !mailman.getIsAtDoor().equals("N"))
So it actually sets it at false if its null or not equals to "N"? Did I understand correctly?
Is there any way to check the null without using the if condition?
Thank you!
So, your logical condition is basically like the following:
mailman.getIsAtDoor() == null || !mailman.getIsAtDoor().equals("N")
You can change the instance which performs equal operation. The one instance here is a constant string literal - that means it is never a null value, so it can always perform an equality operation. The condition you are looking for is here:
!"N".equals(mailman.getIsAtDoor()).
This approach does not require from you to check null value of the mailman.getIsAtDoor().
Perhaps the dog or the mailman variable is null. So please try the following code:
if (dog != null && mailman != null)
{
dog.setIsBarkingIndicator(!mailman.getIsAtDoor().equals("N"));
}
when iterate through a List of JSONNODES like so
for(JsonNode result: results){
if (predicate==Predicate.GREATER_THAN && result.has("JarVersion")){
//gets in here even though 'JarVersion' is null
if(result.get("JarVersion").textValue().compareTo(version) > 0)
//fails here because nulls pass through
for some reason even though result.get("JarVersion") returns null, it passes the result.has() check.
Is there an issue with the has() function of this library?
I looked into it and on the interface all it is doing is results.get(fieldName) != null, yet there are many null cases that get through (and break) my code
/*line 1.*/
if((name!=null || address!=null)||(!(name.equals("")||address.equals("")))){
sessionNameModel.setObject(""+name);
sessionAddressModel.setObject(""+address);
add(sessionName);
add(sessionAddress);
}
else{
sessionNameModel.setObject("");
sessionAddressModel.setObject("");
add(sessionName);
add(sessionAddress);
}
I am getting null pointer exception in line 1. I know the value of name and address field is null but i have to check in cases when session updates the value. i need to know how to check these variables name and address as null or not..
Assuming that your condition is correct, change:
if((name!=null || address!=null)||(!(name.equals("")||address.equals("")))){
to:
if((name!=null || address!=null)||(!("".equals(name)||"".equals(address)))){
Explanation:
by using "".equals(name) you guarantee that even if name is null (which is possible since you're using || in the previous expression) the comparison will return false instead of NPE. Same goes for address.
if((name!=null || address!=null)||(!(name.equals("")||address.equals(""))))
Here if name==null still you are checking for name.equals which may cause nullpointer same thing happens for address.Instead use && for checking equals only if it's not null.Or you can use "".equals as suggested by alfasin
if((name!=null && !name.equals(""))||((address!=null && !address.equals(""))))
Apart from the discussion,
I think here use of || not needed instead use && as here || can add null in your session which is not proper.
if((name!=null && !name.equals(""))&&((address!=null && !address.equals(""))))
You can try like this
if((name !=null || name.length()==0) && (address!=null || address.length()==0)){
}
else{
}
name = null;
address = null;
if((name!=null || address!=null)||(!(name.equals("")||address.equals(""))))
Analysis;
first checks name!=null, it's false
then checks address!=null, which is false either
then it tries to check if name.equals(""), but since name is null, it will crash because there is no object as name and so no method as equals
as looking at the other lines you have to change the line as
if((name!=null && address!=null)&&(!(name.equals("")||address.equals(""))))
Let's analyse the new line;
first checks name!=null, which is false
since all other conditions are combined with AND and since one of them is false, no need to check anything else, so executes the second line
At first cobait is right:
if((name!=null || address!=null)||(!(name.equals("")||address.equals(""))))
If either the variable name or the variable address is null, the following code will still be executed. This is probably causing your nullpointer.
Better expression would be if((name != null) && (address != null)) ....
If you want to execute the code for at least for one of the variables, if it is not null, you should split your if statements.
Another alternative for your check would be using the apache commons library and call the function isBlank(). This function checks both, if the String is not null and if it's not emtpy.
Okay, its a simple question, I dont have any problem with it but I just want to be sure. Is it correct if I do something like this ?
if (person != null && person.getAge > 20) {...}
can I do this? null check and object access on the same line? is this correct? or I have to make an inner statement on the null check statement like:
if (person != null)
{
if (person.getAge() > 20) {...}
}
imo, the first statement wouldnt throw a NPE, but I needed feedback from professionals...
Generally my question is that, on if statements with &&, if the first boolean is false, will the JVM stop executing the if statement or it will check it all ?
Yes. The operators && and || are what are known as short circuiting operators. This means that as soon as the answer is known, reading from left to right, the other statements in the condition are not evaluated. In your case, as soon as person != null part evaluates to false, the whole condition will be guaranteed to be false regardless of the other statements, so person.getAge > 20 will not be evaluated and will not throw an exception. Similarly, if you use ||, and the first part evaluates to true, the second part will not be evaluated because the overall conditional is guaranteed to be true at that point.
yes you can do that. if person is null, it will see
if(person != null && otherBoolean )
if person != null is false, than it doesn't matter what otherBoolean is, because the expression will be guaranteed to be false no matter what.
It evaluates the leftmost expression first, so if you had it in the other order, that would't work.
as a side note, you can test this pretty easily by writing:
person = null;
if (person != null && person.getAge > 20) {...}
if (person != null && person.getAge > 20) {...}//good to go with that
Surely in this case if the person is null it will not evaluate person.getAge > 20 so you will not get NPE
Read about the && operator in java surely it will not check the second value
if (person != null && person.getAge > 20) {...}
&& is short circuit logical AND. Thus if person != null is false, person.getAge > 20 will not be evaluated.
& is logical AND that does not short circuit. If you use if (person != null & person.getAge > 20) and person is null, you will get a null pointer exception when person.getAge > 20 is evaluated.
It's called short-circuiting and it's perfectly fine to do that.
if (person != null && person.getAge > 20) {...}
Always make sure you do a null check and && will move to check the next condition only if the previous one is true.
In this case person.getAge > 20 is checked only if person != null is true.
Also check this SO question: Java logical operator short-circuiting
For further reading: Avoiding “!= null” statements in Java?