# Thread: NullPointerException for no apparent reason?

1. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## NullPointerException for no apparent reason?

Java Code:
```public class World {

public static final int ROWS = 10;
public static final int COLS = 10;
private Organism [][] city;
public static Random rand;
World worldCopy;

public World(int numTypeA, int numTypeB) {
city = new Organism[ROWS][COLS];
while (numTypeA > 0) {
Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
if (city[temp.row][temp.col] == null) {
city[temp.row][temp.col] = new TypeA(worldCopy, temp);
numTypeA--;
}
}
while (numTypeB > 0) {
Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
if (city[temp.row][temp.col] == null) {
city[temp.row][temp.col] = new TypeB(worldCopy, temp);
numTypeB--;
}
}
}

public class Coordinates {
int row;
int col;

Coordinates(int x, int y) {
this.row = x;
this.col = y;
}

}```
So at line 12 ( Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS)); ) I'm getting a NullPointerException and I just can't figure out why.
What I'm trying to do, is randomly place a number of objects of TypeA and then TypeB in an array. This is only part of the code. Your help will be much appreciated. :)

(Both TypeA and TypeB extend Organism)
Last edited by xpl0rerchr; 04-10-2012 at 10:43 AM.

2. ## Re: NullPointerException for no apparent reason?

Refer to Tolls' response at #4 in your last thread. You're making a similar mistake here.

db

3. ## Re: NullPointerException for no apparent reason?

Not a NetBeans question. Moving to New to Java.

db

4. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: NullPointerException for no apparent reason?

But I did do

Java Code:
`private Organism [][] city;`
and then, I did

Java Code:
`city = new Organism[ROWS][COLS];`
which makes me think it must not be a similar mistake to the one you're referring to.

Also, sorry for posting at the NetBeans section, it's just that I'm using NetBeans. Will know better next time.

5. ## Re: NullPointerException for no apparent reason?

I don't see the variable city being used anywhere on line 12. What variables are used on that line? There are four; two of them are primitives and can't be null; one is being assigned a value (or would be if...); the remaining one hasn't been assigned a value and is null.

db

6. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: NullPointerException for no apparent reason?

Java Code:
`Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));`
I just can't see what's wrong with this. I'm creating a Coordinates object the way any object is typically created, right?

7. ## Re: NullPointerException for no apparent reason?

What are the four variables on that line?

db

8. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: NullPointerException for no apparent reason?

Originally Posted by DarrylBurke
What are the four variables on that line?

db
1) temp, which is of type Coordinates
2) a new Coordinates object, to be created and then assigned to temp
3) rand.nextInt(ROWS)
4) rand.nextInt(COLS)

9. ## Re: NullPointerException for no apparent reason?

temp is a variable
a new coordinates object isn't a variable
rand.nextInt(ROWS) isn't a variable.
rand.nextInt(COLS) isn't a variable.

Try again.

db

10. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: NullPointerException for no apparent reason?

Originally Posted by DarrylBurke
temp is a variable
a new coordinates object isn't a variable
rand.nextInt(ROWS) isn't a variable.
rand.nextInt(COLS) isn't a variable.
Originally Posted by DarrylBurke
What variables are used on that line? There are four;
Well there's nothing else on that line and you previously said there were 4 variables.

11. ## Re: NullPointerException for no apparent reason?

rand?

kind regards,

Jos

12. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: NullPointerException for no apparent reason?

Originally Posted by JosAH
rand?
Yes, as I want TypeA and TypeB objects to be created and randomly placed in the city array. So I'm generating random numbers from 0 to (and including) 9 for rows and columns.

13. ## Re: NullPointerException for no apparent reason?

What's the value of rand?

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•