AVL TREE REBALNCE [duplicate] - java

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

Related

JUnit java.lang.NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
Im new to software testing, i keep getting java.lang.NullPointerException for some reason and i don't know what it is
This is what i am testing on
public int getNumberOfStudents()
{
int validStudents = 0;
int i = 0;
boolean done = false;
do {
if (students[i] != null && students[i].exists())
validStudents++;
else
done = true;
i++;
} while(i < students.length && !done);
return validStudents;
} // end of getNumberOfStudents
This is the test case
class Testing {
RegistrationSystem GNS = new RegistrationSystem();
#Test
//#SpiraTestCase(testCaseId=5487)
public void test() {
assertEquals("validStudents", GNS.getNumberOfStudents());
}
}
I guess at line if (students[i] != null && students[i].exists()) you have not initialized students while creating RegistrationSystem Object, create it and it will work also I can see a problem in assert that you are comparing string with int :)assertEquals("validStudents", GNS.getNumberOfStudents());
Where exactly are you getting error.
One problem with your test case is you are comparing string "validstudents" with integer returned from function GNS.getNumberOfStudents(). You need to set a number of students, then call getNumberOfStudents() assert it with the expected value.

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

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

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

compatibility null pointer error? [duplicate]

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.

Categories