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.
Related
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 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:
Try-catch-finally-return clarification [duplicate]
(2 answers)
Multiple returns: Which one sets the final return value?
(7 answers)
Closed 5 years ago.
Hi I have a question about catch and finally.
I looked around for the same case but had no luck but I'm sure this has been asked before.. so this may be a duplicate. Sorry for that.
Anyway this is my question:
Here is a method which returns a boolean
public static boolean runtimeException() {
try {
int i = 1 / 0;
} catch (ArithmeticException ae) {
return true;
} finally {
return false;
}
}
When I call this method in my main function:
public static void main(String[] args) {
boolean flag = runtimeException();
System.out.println(flag);
}
The result is: "false".
Id like to know why this happens.
I know that the finally block has to be executed at any means but logically the results don't make sense to me because the catch block is executed before and the function has already returned true. How can the function return false after already returning true??
Thanks in advance.
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.
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.