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.
Related
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();
}
}
This question already has answers here:
Remove specific index from array in java
(6 answers)
Why we need to override hashCode and equals?
(4 answers)
Closed 5 years ago.
I have created an array of objects (object representing a flight),
and i'm trying to create a method to remove a specific object from that array, without changing it's length.
I have written the following method :
public boolean removeFlight (Flight f) {
for (int i = 0 ; i < _noOfFlights ; i++) {
if (_flightsSchedule[i].equals(f)) {
_flightsSchedule[i] = _flightsSchedule[(i+1)];
_noOfFlights--;
return true;
}
}
return false;
}
_noOfFlights represents the number of object currently in the array.
For some reason it returns "false" when given an object that was added to the array.
You need to be careful not to change the ground under your feet. You also don't want to return in the middle of the loop, otherwise you won't have moved all the elements properly.
You could do something like this:
public boolean removeFlight (Flight f) {
boolean found = false;
for (int i = 0 ; i < _flightsSchedule.length; i++) {
if (f.equals(_flightsSchedule[i])) {
found = true;
} else if (found) {
_flightsSchedule[i - 1] = _flightsSchedule[i];
}
}
if (found) {
_noOfFlights--;
_flightsSchedule[_flightsSchedule.length - 1] = null;
}
return found;
}
Also, note that I've set the last element to null to avoid an inadvertent memory leak.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I wrote this simple function:
private String getOperatorForCardinality(String op)
{
String operator ="";
if(op!=null)
{
if(op.equals(">="))
{
operator = ">=";
}
else if (op.equals("<="))
{
operator = "<=";
}
}
else
{
operator = "empty";
}
return operator;
}
which returns a string.
In the main program I call this function, when the argument is null the compiler displays the error of NullPointerException.
The reason is pretty clear, but I do not know how to deal with the null value when is passed by argument.
It is impossible for the code you posted to throw a NPE. The error is somewhere else, or you are not running the code you think you are (ie haven't recompiled etc).
That said, your method can be simplified to:
private static List<String> OPS = Arrays.asList("<=", ">="); // can add more valid ops
private static String getOperatorForCardinality(String op) {
if (op == null)
return "empty";
return OPS.contains(op) ? op : "";
}
Or if you don't mind nested ternaries:
private static String getOperatorForCardinality(String op) {
return OPS.contains(op) ? op : op == null ? "empty" : "";
}
Less code is usually clearer code, and leaves less places for bugs to lurk.
It is called defensive programming and you should do something like:
private String getOperatorForCardinality(String op) {
if(null == op) {
//return null;
//throw new NullPointerException("...");
}
....
}
You should think about how your method should react, need to return null if parameter is null or throw an exception? Generally you cant be sure a parameter will never be null so you have always to check and take action.
This can't throw a null pointer exception:
private String getOperatorForCardinality(String op)
{
String operator = "";
if(">=".equals(op))
{
operator = ">=";
}
else if ("<=".equals(op))
{
operator = "<=";
} else {
operator = "empty";
}
return operator;
}
try opposite
if (op==null)
operator = "empty";
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 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.