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

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.

Related

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

Null Pointer Exception Java Shopping Cart "sort" Method [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
For a school assignment, we have to make a Shopping cart class, along with an Item class and a runner. I figured out how to print out a receipt for items the user enters, but one thing I can't figure out is how to write a sort method using insertion Sorting, which orders each item in the cart by its total cost (price * quantity)
Here is my error:
java.lang.NullPointerException
at ShoppingCart.sort(ShoppingCart.java:54)
at Shopping.main(Shopping.java:32)
I went back to the lines stated
ShoppingCart: (line 54)
public void sort() {
double temp;
int pos = 0;
for (int i = 1;i< cart.length;i++){
temp = cart[i].itemPrice(); //line 54
pos = i;
while (pos>0 && temp < cart[pos-1].itemPrice()) {
cart[pos] = cart[pos-1];
pos--;
}
cart[pos] = cart[i];
}
}
Shopping: (line 32)
cart.sort();
And here is my method to get the price in the Item Class
public double itemPrice(){
return total; }
I'm not sure how to fix the Null Pointer Exception error.
I assume you've got something like this:
Item[] cart = new Item[50];
This creates an array that has room for 50 items.. but all that room is initialized to null. You still have to make 50 items:
for (int i = 0; i < cart.length; i++) cart[i] = new Item();

Value of Variable Not Updating [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
My Variable c is always zero. I dont understand why its not updating. can anyone please explain why this is happening. what should i do to avoid this
public static int linearSearch(Exam[] marks, String name) {
int c =0;
if( marks==null)
{
return -1;
}
else{
for(int i=0;i<marks.length;i++)
{
//System.out.println(a[i]);
if(performances[i].getName()==name)
{
c= i;
}
}
}
return c;
//to be completed
}
Modify this line as below
performances[i].getName().equalsIgnoreCase(name)
if you want to Ignore upper or lower case
else use the below
performances[i].getName().equals(name)
to check the content of the name instead of references.

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

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.

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.

Categories

Resources