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.
Related
This question already has answers here:
Why would someone use Collections.emptyList in java? [duplicate]
(5 answers)
Closed 2 years ago.
I am just curious to know where exactly the java.util.Collections.emptyList() method is used. Why such method is given which returns immutable list in Java? What could be done with it?
It's useful when you want to return... an empty list. It's more efficient to reuse a canonical empty list than to create a new one each time.
List<Integer> numbersBetween(int from, int to) {
if (to < from) return Collections.emptyList();
else {
//do what you have to do
}
}
In legacy applications it was quite common to use null values instead of empty lists, sets or maps.
public List<String> getElements() {
if (bla < 0) {
return null;
}
}
public List<String> getElements() {
if (bla < 0) {
return Collections.emptyList();
}
}
In my opinion, it is mostly better to work with empty List<>, Set<> or Map<> instead of null values.
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.
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))
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
lblPt1.setIcon(bd.pt1.getImage());
works while
lblPt1.setIcon(bd.pts[0].getImage());
throws a NullPointerException
pts[] is an array containing "Point" objects pt1 through pt24.
I would like to handle it with this for loop where pointLbls[] is an array of JLabels:
for (int i = 0; i < 24; i++)
pointLbls[i].setIcon(bd.pts[i].getImage());
I have determined that it is the object that is null, not its associated image.
What am I doing wrong? I am rather new to programming.
You can insert a Null check condition, try the code below:
for(int i=0; i<24; i++){
Point temp = bd.pts[i]; // store point in a temporary variable
if(temp != null) // check if it is null
pointLbls[i].setIcon(temp.getImage()); // if not null, do what you want!
}
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.