This question already has answers here:
What is null in Java?
(14 answers)
What does null mean? [duplicate]
(4 answers)
Closed 4 years ago.
What the difference between using q != null and !q.isEmpty()?
when I used q!=null in the following code, it's not going to be compiled. But !q.isEmpty() works pretty well.
java
Queue<TreeNode[]> q=new LinkedList<>();
q.add(new TreeNode[]{t1, t2});
while(q!=null){ // is not complied
TreeNode[] t=q.remove();
if(t[0]==null || t[1]==null) continue;
t[0].val+=t[1].val;
if(t[0].left==null) t[0].left=t[1].left;
else q.add(new TreeNode[]{t[0].left, t[1].left});
if(t[0].right==null) t[0].right=t[1].right;
else q.add(new TreeNode[]{t[0].right, t[1].right});
}
'q' will never be null because you already instantiated it with new LinkedList so there will result in an infinite loop. So, in your example, you have to check if the list is empty. But it should compile
Related
This question already has answers here:
What does <T> (angle brackets) mean in Java?
(6 answers)
What is a raw type and why shouldn't we use it?
(16 answers)
Constructor and new operator in Java
(1 answer)
Closed 2 years ago.
I am trying to understand what's this line doing:
Set<ListNode> nodesSeen = new HashSet<>();
This is from a solution to check if we have a cyclic linkedList or not.
Could someone help me understand this line's assignment please (I bolded it in the code snippet below)?
Thank you!
public boolean hasCycle(ListNode head) {
Set<ListNode> nodesSeen = new HashSet<>();
while (head != null) {
if (nodesSeen.contains(head)) {
return true;
} else {
nodesSeen.add(head);
}
head = head.next;
}
return false;
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Boolean valid = null;
if (valid == null) {
log.info("******");
}
I have the expression above, I wanted to check if Boolean value is null and if it is then execute statement inside if clause, but what happened is that it throws me a NullPointerException. What should I do to have my if clause evaluated with that given condition valid == null.
Thanks in advance
For sure, your log object contains null reference. You can't invoke any method with null reference. Try this:
Boolean valid = null;
if(valid == null) {
System.out.println("Hello");
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
when objec is null:
if(object != null && object.string.equals("")) {
System.out.println("no error");
}
it will result in NullPointerException, and why if it checks the first result is false, it still check the second instead of stop check and print "no error"?
sorry for my bad english -_-#
It's not object which is null but object.string.
Try: (object != null && "".equals(object.string))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Replace will create new object and both side this new will be compared. then why it showing false.
When exactly created new string will be added in string pool?
if("String".replace("g", "G") == "String".replace("g", "G"))
{
System.out.println("True");
} else {
System.out.println("False");
}
because replace() will always return a new String instance. So the 2 same calls to replace method will return 2 different instances with same value.
use equals() instead of == if you want to compare value
Use intern() on both replaced values if you want to add the string to the string constants pool (and are bent on using == :P)
if ("String".replace("g", "G").intern() == "String".replace("g", "G").intern()) {
System.out.println("True");
} else {
System.out.println("False");
}
}
OP :
true
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
int e=0;
int Compatibility=0;
int numOfDisagreements=0;
int tsize=likes.size()+dislikes.size();
int numOfCommonLikes=0;
int numOfCommonDislikes=0;
while(e<tsize){
if (user.likes.get(e).equals(Stranger.likes.get(e))){
if ((Stranger.howDoYouLike(likes.get(e)))== 1 && user.howDoYouLike(likes.get(e))==1)
{
numOfCommonLikes=numOfCommonLikes+1;
}
}
else if (user.dislikes.get(e).equals(Stranger.dislikes.get(e))){
if ((Stranger.howDoYouLike(dislikes.get(e)))== -1 && user.howDoYouLike(likes.get(e))==-1)
{
numOfCommonDislikes=numOfCommonDislikes+1;
}
Am getting a nullpointer error, I don't know what that means but can someone tell me what is wrong with this code please, I am looking real hard
else
{
numOfDisagreements=numOfDisagreements+1;
}
e+;
}
Compatibility=(numOfCommonLikes+ numOfCommonDislikes)- numOfDisagreements;
return Compatibility;
}
Are you initializing you Lists? I see the line:
int tsize=likes.size()+dislikes.size();
But no proof that you ever created any list objects. A NullPointerException is when you try to use an object that has not yet been created. For example, if I tried to do this:
List<Object> objects;
objects.size();
Then I would get a NullPointerException. I would need to create the list like so:
List<Object> objects;
objects = new List<Object>();
Before I tried to call any method belonging to it.