why it result in a NullPointerException when Object is null? [duplicate] - java

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))

Related

What is the difference between null and isEmpty() in queue [duplicate]

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

how to check jTable nullpointer in row [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
how to check my jtable if null pointer?
my code is always null pointer and then can't export to xls.
this my code
if (tbGudangSales.getValueAt(i, 5).toString().trim().length() == 0) {
status = "tidak ada penjualan";
} else{
status = tbGudangSales.getValueAt(i, 5).toString();
}
label = new Label(4, baris, status, bordertabel);
excelSheet.addCell(label);
this my error
this my value table
All you need to do is to check if the value is null. For example:
Object value = tbGudangSales.getValueAt(i, 5);
if (value != null)
{
if(value.toString().trim().isEmpty()) // instead checking the length "manually", you can use the isEmpty() method
{
status = "tidak ada penjualan";
}
else
{
status = value.toString();
}
}

Check Boolean if null [duplicate]

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");
}

AVL TREE REBALNCE [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
in Line 7 i get nullpointerexception, how can i fix that ? any ideas?
private void updateHeights(BSTreeNode v) {
BSTreeNode u = v;
while (u != null) {
int bfc=updateNodeHeight(u);
u = u.parent;
if (bfc<=-2) {
if( getHeight(u.left.left) >= getHeight(u.left.right) ) { // Null Pointer Exception
u = rotateRight(u);
} else {
u=LR(u);
}
}
}
}
There are two possibel problems, and as many solutions. First is placing a null check in getHeight() which I assume has been done based on your programming style (the check in while conditional). That leaves a possible null pointer issue with u.left. As such:
Ensure u.left is not null, like this:
getHeight(u.left != null && ((u.left.left)>=getHeight(u.left.right))

catch Exception with add [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I wrote this statement in the main method
the Exception will be in the (GuaranteeYears) so i put it in the try block.
First in the main I make a Store object and pass number 4 in parameter, in the Store class I will have a array called arrProduct that will have the length 4:
boolean flag1 = true;
while (flag1) {
try {
System.out.println("Ente GuaranteeYears");
int GuaranteeYears=read.nextInt();
StrongDevice s = new StrongDevice(code,type,price,Model,Capacity,GuaranteeYears); //the object
flag1 = false;
x.addProduct(s);
}
and this is my add method
public boolean addProduct(Product p) {
if(arrProduct.length==NumOfProduct)
return false;
arrProduct[NumOfProduct]=p;
NumOfProduct++;
return true;
}
My question is: when I make the object and add it to the array it gives me NullPointerException - why that happened?
As far as I understood, the line which cause NPE is
x.addProduct(s);
Most likely this is caused by array arrProduct which is not initialized and cause NPE when it is being accessed.
To fix that, please verify that arrProduct is always correctly initialized at Store object creation.

Categories