NullPointerException

Hey, all:

I'm getting a NullPointerException when I try to run my agent based model at Model.java in the step method and at Ocean, also in the step method. I've marked where I'm getting the null pointers; I thought I understood what NullPointerExceptions are--it's when I'm trying to do something to an Object that isn't there, right? There are 64 spaces in the model, like a chessboard, and there are 20 swimmers, 20 surfers, and 24 oceans. By definition, there aren't any places where there are null locations, so I don't know what's wrong here--do any of you?

publicvoid step (){

System.out.println("==> Model step %.0f:\n" + getTickCount() );// Checking step #

for (int i = 0; i < surferList.size(); i++ ){

Surfer aSurfer;// Makes new surfers when needed

aSurfer = (Surfer) surferList.get (i);// Identifies surfers

aSurfer.step ();

}

for (int i = 0; i < swimmerList.size(); i++ ){

Swimmer aSwimmer;// Makes new swimmers when needed

aSwimmer = (Swimmer) swimmerList.get (i);// Identifies swimmers

aSwimmer.step ();

}

for (int i = 0; i < oceanList.size(); i++ ){

Ocean aOcean;

aOcean = (Ocean) oceanList.get (i);

aOcean.step ();//here's the NullPointerException in Model.java

}

dispSurface.updateDisplay();// Updates the display with new surfers and swimmers and identities

System.out.println("<== Model step done.\n" );

}

....

publicvoid step (){

System.out.println("==> Ocean step beginning" );

boolean stepCompleted =false;// Has not yet moved this step

if (!stepCompleted){//Proceed with the step method

//System.out.println("==> created variables in the step method");

vector = world.getMooreNeighbors(x, y,true);//here's the NullPointerException in Ocean.java

for (int i = 0; i < vector.size(); i++ ){

Object o = vector.get(i);

if (oinstanceof Surfer){

surferNeighbors.add(o);

System.out.println("Object #" + i +" is a " + o.getClass().getName());

System.out.println("==> surfers getting added to list now");

}

elseif (oinstanceof Swimmer){

swimmerNeighbors.add(o);

System.out.println("Object #" + i +" is a " + o.getClass().getName());

System.out.println("==> swimmers getting added to list now");

}

elseif (oinstanceof Ocean){

equalNeighbors.add(o);

System.out.println("Object #" + i +" is a " + o.getClass().getName());

System.out.println("==> oceans getting added to list now");

}

else{

System.out.println("Something is VERY WRONG here!!");

}

[4709 byte] By [tarahmariea] at [2007-10-3 3:20:00]
# 1

for ( int i = 0; i < oceanList.size(); i++ ) {

Ocean aOcean;

aOcean = (Ocean) oceanList.get (i);

aOcean.step ();//here's the NullPointerException in Model.java

}

Try commenting this peice of code .... I bet the then you will see a NULL Pointer exception in the loop above at " aSwimmer.step ();"

Basically try to minimize the problem and go from them, also if you have a debugger try using that.

Thanks

oops never mind, the problem is actually in the second STEP method .. rite ?

Message was edited by:

ashucool83

ashucool83a at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 2

the problem is actually in the

> second STEP method .. rite ?

>

> Message was edited by:

> ashucool83

Yeah--It's in the step method for Ocean.java. When I comment out the command in Model.java calling on the Ocean step method, the model runs just fine with the swimmers and surfers. The problem, of course, is that even if my code works, it's not doing what I want it to; it has to take the instances of Ocean.java into account to give me accurate results. I'm trying to understand the theory here; null pointer exceptions only happen when I'm trying to do something to an Object that isn't really there, right? Imagine a chessboard in which there are 20 surfers, 20 swimmers, and 24 pieces of ocean. There's 64 squares, and at the beginning of the step method, I ask Ocean.java to find its Moore neighbors--that being the neighbors to the N, S, E, W, NW, NE, SW, and SE, like having Ocean instances in the center of a TicTacToe diagram. In Von Neumann space, I'd only be asking for the N, S, E, and W neighbors. I'm using Torus space instead of Grid space, meaning that the swimmers, surfers, and oceans on the edges of the chessboard wrap around to the other side when finding their neighbors on all sides. How can I be getting a NullPointerException when by definition there aren't any null locations? I thought that maybe the problem was that while the swimmers and surfers were getting created and properly placed, the oceans weren't, but I am getting debug messages telling me that oceans are being constructed and placed in the chessboard. Here's the for loop in Model.java that constructs and places the Ocean instances:

for ( int i = 0; i < numOceans; i++ ) {

aOcean = new Ocean();

oceanList.add( aOcean );

allList.add( aOcean );

//tells Oceans to find all the other places not occupied by Swimmers and Surfers

do {

randomX = Random.uniform.nextIntFromTo( 0, world.getSizeX () - 1 );

randomY = Random.uniform.nextIntFromTo( 0, world.getSizeY () - 1 );

} while ( world.getObjectAt( randomX, randomY ) != null );

world.putObjectAt( randomX, randomY, aOcean );

aOcean.setX( randomX );

aOcean.setY( randomY );

int aOceanID = aOcean.getID();

System.out.println( "- created Ocean with ID=%d placed at x,y=%d,%d.\n" +

aOceanID + aOcean.getX() + aOcean.getY() );

}

Why the devil am I getting null pointer exceptions when I get confirmation debug messages telling me that 20 surfers, 20 swimmers, and 24 oceans have been created and placed in my world?

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 3

Wait a minute--I think I might have figured it out, but I don't know how to fix the error. Here's the problem: when my surfers and swimmers are trying to move, they are looking for null locations to move to. Since in the model I've made it so that there are never any null locations, that might be it. I think I need to figure out a way to (a) make it so that the surfers and swimmers will switch places with oceans and (b) to make the oceans ok with that.JAS--let me try to fix the first part of it...

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 4

Ok--here's the part of the Swimmer step method in which it looks for null locations:

// Swimmer now begins to move square by square to find a spot

do {

newX = x + Random.uniform.nextIntFromTo( -1, 1 );

newY = y + Random.uniform.nextIntFromTo( -1, 1 );

++tries; // P tries again and again to find a spot

} while ( world.getObjectAt( newX, newY ) != null && tries <= maxTries );

// P tries to move from where she was to where she wants to be

if ( world.getObjectAt( newX, newY ) == null ) { // Square is empty!

world.putObjectAt( x, y, null );// Make that cell empty

world.putObjectAt( newX, newY, this ); // P moves to new home

I just tried to replace 'null' with instanceof Ocean, but it didn't work--I got "Ocean cannot be resolved" and "syntax error on instanceof"--How would I phrase this appropriately?

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 5

Change your loop to:

do

{

...

} while ( (!(world.getObjectAt( newX, newY ) instanceof Ocean)) && tries <= maxTries );

Then do a swap of the Ocean and the Swimmer/Surfer:

if ( world.getObjectAt( newX, newY ) instanceof Ocean) { // Square is an Ocean!

Object oldOcean = world.getObjectAt(newX, newY); // Save old Ocean.

world.putObjectAt( newX, newY, this ); // P moves to new home

world.putObjectAt( x, y, oldOcean);// Ocean moves to new home

MLRona at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 6

I know that certainly fixed a theoretical problem with the model, but the null pointer exception JUST WON'T GO AWAY. I checked the output again, and I see that even though the oceans are getting created by the main class, the surfers and swimmers still don't 'see' the oceans that are their neighbors. They think the spaces are still null--here's the debug message the swimmers and surfers are kicking back:

==> Surfer step beginning

==> created variables in the step method

Object #0 is a Model.Surfer

==> surfers getting added to list now

Object #1 is null

Object #2 is null

Object #3 is null

Object #4 is a Model.Surfer

==> surfers getting added to list now

Object #5 is null

Object #6 is null

Object #7 is a Model.Swimmer

==> badNeighbor list being populated

1.0

0.125

12.5

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 7

Ok, at least now the Swimmers and Surfers 'see' the Oceans. Now I'm getting a really weird exception; here's the output:

The happy ratio for this Swimmer is 63

==> Swimmer step beginning

==> created variables in the step method

Object #0 is a Model.Swimmer

==> swimmers getting added to list now

Object #1 is a Model.Surfer

==> badNeighbor list being populated

Object #2 is a Model.Ocean

Object #3 is a Model.Ocean

Exception in thread "Thread-4" Object #4 is a Model.Surfer

==> badNeighbor list being populated

Object #5 is a Model.Ocean

Object #6 is a Model.Surfer

==> badNeighbor list being populated

Object #7 is a Model.Swimmer

==> swimmers getting added to list now

3.0

0.375

37.5

==> happyRatio successfully calculated at 63

-- moved to new x,y=%d,%d.

46

The happy ratio for this Swimmer is 63

If the swimmer found that neighbor number 4 is a surfer, why is it throwing an exception AGAIN? Also, just to make sure that the surfers and swimmers want to move around, I changed their happyRatio percentage to 75, which means that this swimmer not only wasn't happy, but successfully moved to a different location, according to the output. If that's so, why am I still getting the same NullPointerException I originally had in addition to this new exception?

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 8
Here's the error message for the null pointer exception; it says something about Thread 4--have I inadvertently created a thread or something?
tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 9

Are you sure the message about "Object #4 is a Model.Surfer" is part of the Exception message? I'd guess that it isn't.

Do you have the following in all of your Exception handlers (catch blocks)?

catch(Exception exc) // whatever exception you catch

{

exc.printStackTrace(); // Tell where it is coming from.

}

You will always have a few threads in the application, even if you don't start one yourself. They get automatic numbers if you don't name them yourself.

MLRona at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 10

> Are you sure the message about "Object #4 is a

> Model.Surfer" is part of the Exception message?

> I'd guess that it isn't.

>

> Do you have the following in all of your Exception

> handlers (catch blocks)?

> catch(Exception exc) // whatever exception you

> catch

> {

> exc.printStackTrace(); // Tell where it is coming

> from.

>

>

> You will always have a few threads in the

> application, even if you don't start one yourself.

> They get automatic numbers if you don't name them

> yourself.

I had never used try/catch blocks before; I just now used a few tutorials on them, and successfully found my NullPointerExceptions--of which there were disturbingly many, let me tell you. It came down to the fact that while I had instantiated all seven of the Vectors I needed, I hadn't declared them locally, and so the compiler was left thrashing around to find Vectors that had nothing in them.

Thanks, MLRon!! It worked, and now my GUI is a blossoming heap of colors! I've still got a couple of bugs to work out, and I'll start on those next, but now all my classes are talking to each other and doing their thing without errors!!

If anybody's looking for good resources on why and how to use exceptions to more closely pinpoint NullPointerExceptions, here are two really good websites:

http://www.javaworld.com/javaworld/jw-07-1998/jw-07-techniques.html

and

http://today.java.net/pub/a/today/2003/12/04/exceptions.html

They taught me how to create try/catch blocks, what to import (java.lang.NullPointerException), and what to do about them. I'll never write code again without using exceptions to find problems!!

tarahmariea at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...
# 11
Glad it works, and that you learned about exceptions. You don't need to import java.lang.NullPointerException, though. Anything in java.lang is available automatically (without an import).You're welcome for the help.
MLRona at 2007-7-14 21:12:05 > top of Java-index,Java Essentials,Java Programming...