NullPointerException where there's no possibility for one [duplicate] - java

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.

Related

Getting a NullPointerException when trying to fill an Array with Points [duplicate]

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.

Why am I getting a NullPointerException for this loop? [duplicate]

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...) {
...
}

Error message being thrown: Index out of bounds exception [duplicate]

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

Java getting functions from Object type [duplicate]

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.

Java: NullPointerException with substrings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a problem in one of my methods. Please keep in mind that I am learning Java in college so I might not be up to speed on simple things. Below is a method that is made to add expressions. The problems I'm running into is found where x = x.substring.(1, x.length() - 1); I'm getting an exception that reads:
Exception in thread "main" java.lang.NullPointerException
I have no idea what that means and/or how to fix it. If you could point me in the right direction, that'd be great.
Thanks.
public static int adder(String x){
int total = 0;
x = x.substring(1, x.length() - 1);
sopln(x);
String[] nums = x.split("\\+");
for(int i = 0; i < nums.length; ++i){
if(nums[i].charAt(0) == ' ' || nums[i].charAt(nums[i].length()-1) == ' '){
sopln("ERROR: Excess whitespace identified.");
nums[i] = nums[i].trim();
}
nums[i] = nums[i].replaceAll(" ", "");
if(nums[i].charAt(0) == '-')
total -= Integer.parseInt(nums[i]);
else
total += Integer.parseInt(nums[i]);
}
return total;
}
It probably means that your String x is null and not actually set to an Object of a String.
How are you calling the method?
Does it happen when you call it with a hard coded string like
int num = adder("string checking in");
if not, then somewhere upstream in your code, that String variable you are passing into the adder method is null.
you are passing a null value when you call adder(the_string), where the_string is null
The problems I'm running into is found where x=x.substring.(1,x.length() - 1);
This means that at some point you are calling adder with an argument that is null. That's the only way you can get an NPE at that point.
Find out where and why the argument is null, and then fix it.
(It could be an explicit null, but it is more likely to be coming from an uninitialized field, or from some method that returns a null to indicate something; e.g. BufferedReader.readLine() returns null when the reader reached the EOF position.)

Categories

Resources