This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm writing a for loop to iterate through the array and find the index of the employee with the social security number of 123456789. Why do I keep getting a NullPointerException?
.getSocSecNum() returns a String
empnew[21] = Empthe1;
String temp = "123456789"
for(int i = 0; i < empnew.length - 1; i++){
if(empnew[i].getSocSecNum().compareTo(temp) == 0){
System.out.println("the location of the employee is " + i);
}
}
I want it to output "the location of the employee is 21" but all I get is a NullPointerException
The problem is most likely that empnew has at least one null in it.
So in the loop, you try to get empnew[i].getSocSecNum(), which expands to null.getSocSecNum(), which gives you a null pointer exception, because you can't access class members on null, because it isn't a real object.
The easiest solution might be to just check that empnew[i] != null before accessing the value:
if (empnew[i] != null && empnew[i].getSocSecNum...) {
...
}
Related
This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 months ago.
I created an array of 50 points, which I now want to fill with points ordered by their y coordinate in a 10x5 grid. I get a NullPointerException at if(i<5) P[i].x=0. What am I doing wrong here?
Point[] P= new Point[50];
for (int i=0; i<P.length; i++) {
if (i<5) P[i].x=0;
else if (i<10) P[i].x=50;
else if (i<15) P[i].x=100;
else if (i<20) P[i].x=150;
else if (i<25) P[i].x=200;
else if (i<30) P[i].x=250;
else if (i<35) P[i].x=300;
else if (i<40) P[i].x=350;
else if (i<45) P[i].x=400;
else P[i].x=450;
if (i%5==0) P[i].y=0;
else if (i%5==1) P[i].y=50;
else if (i%5==2) P[i].y=100;
else if (i%5==3) P[i].y=150;
else P[i].y=200;
}
Point[] P= new Point[50];
for (int i=0; i<P.length; i++) {
if (i<5) P[i].x=0;
else if (i<10) P[i].x=50;
else if (i<15) P[i].x=100;
....
Point[] p = new Point[50];
This creates an array which can hold up to 50 instances of the Point class. However, you haven't instantiated any of them, so they're all null.
if (i<5) P[i].x=0;
else if (i<10) P[i].x=50;
Depending on the value, you are here trying to set a value to a variable within the instance located at p[i], but that is still null, as explained earlier.
You first need to add actual instances in your array.
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 4 years ago.
For the following code I am getting an index out of bounds exception and I am not sure as to why. Any help is greatly appreciated.
public Rabbit nearestRabbit()
{
List<Rabbit> rabbits = this.getWorld().getObjects(Rabbit.class);
if (this.getWorld().getObjects(Rabbit.class) == null)
{
return null;
}
Rabbit nearest = rabbits.get(0);
double distance = distanceTo(nearest);
for (Rabbit rabbit : rabbits)
{
double thisDistance = distanceTo(rabbit);
if (thisDistance > distance)
{
distance = thisDistance;
nearest = rabbit;
}
}
return nearest; //#######
}
This is mainly due to your list size, I know that you have checked null for the list but since the list is not null it can still be empty with the size 0 and you can not access to the element at index 0.
I recommend you to check the list size.
You are getting this exception by not checking if the index is in bounds in the call rabbits.get(0).
A simple workaround is to put this code first.
if (rabbits.isEmpty())
return null;
This checks if the list is empty before making any other calls.
Also, it would be best to refrain from reference this.getWorld().getObjects(Rabbit.class) more than once, especially after holding it as a variable which is much more accessible.
Add a size check to your if
if (this.getWorld().getObjects(Rabbit.class) == null || rabbits.isEmpty() )
A list may well not be null yet still be empty. E.g. any freshly generated one:
LinkedList<Object> objs = new LinkedList<>();
System.out.println(objs != null && objs.isEmpty()); //true
Your IndexOutOfBound is probably when you are getting element at index 0 where the list might be empty.
if (!rabbits.isEmpty())
{
Rabbit nearest = rabbits.get(0);
}
This question already has answers here:
Checking for a null int value from a Java ResultSet
(13 answers)
Closed 6 years ago.
I guess that an integer null value within a sqlite table is not equivalent to 0 in Java. So how can I check whether the int I extract from my table with
if(resultSet.getInt(columnNumber) != 0){
System.out.println("int is not null");
}
is not null? I do not want to use the sqlite constraint "IS NOT NULL".
It all depends what this method is doing: resultSet.getInt(...)
if getInt is returning a primitive integer, then that can not be null, but if that is an object of the class Integer then you need to check the nullabilty by doing a normal check like: resultSet.getInt(...)!= null and then after been sure that is ot null check if is not zero:
resultSet.getInt(...)!=0
Example:
if(resultSet.getInt(columnNumber) != null){
if(resultSet.getInt(columnNumber) != 0){
System.out.println("int is not zero");
} else {
System.out.println("int is zero");
}
}else{
System.out.println("int is null");
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I'm trying to find a solution for a programme which I'm writing. The problem occurs even though I tried to prevent it and I don't seem to be able to find a mistake. This is the ''problematic'' part of my code:
if (this.s != null) {
if (s.s != null && s.s.sta.length != 0) {
for (int n = x; n < s.s.sta.length + x; n++) {
this.sos[n] = s.s.sta[n-x];
}
x = x + s.s.sta.length;
}
}
I have an array STA which I'm using and a method which gives me the ''s'' neighbour of an object, so s.s is a neighbour of a neighbour.. What I'm trying to do is copy the objects from more than one specific neighbour into one array with many different if sentences. This one is an example but it doesn't work.
Thank you and I really hope I get some info because I'm completely lost.
In the second line of your code you have:
if (s.s != null && s.s.sta.length != 0) {
You do check if s.s is null, but you do not check if s.s.sta is null.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
I have
public Object get(int index)
{
if (index <= 0)
return null;
Node Current = head.getNext();
for (int i = 1; i < index; i++) {
if (Current.getNext() == null)
return null;
Current = Current.getNext();
}
return Current.getData();
}
this method for a linked list to get something in an index. That something is a Painting object, and I want to get the title of the painting using a getTitle method within the Painting class.
Here is what I tried to do,
for(int i = 0; i < paintings.size(); i++){
Painting x = (Painting) paintings.get(i);
System.out.println((i+1) + ": " + x.getTitle());
}
Unfortunately at x.getTitle() java gets extremely salty and gives a null pointer exception. I've tried multiple ways of doing this with different casts and I'm just personally lost. Any help would be deeply appreciated.
You should have a validation since x can be null. When x=null, x.getTitle() become null.getTitle() which cause NullPointerException.
Painting x = (Painting) paintings.get(i);
System.out.println((i+1) + ": " + (x!=null?x.getTitle():"x is null"));
You are returning null if nothing is present at that index, and then doing x(null).getTitle() causing NullPointerException.
Just add a null check before calling the method.