NullPointerexception

Hi

I just starting to learn java and i have created this program, which does compile, but as soon as i try to run it, it gives me an "nullpointerexception". I'll post the code and see if anyone can help me with this. I'm using bluej, and therefor doesn't have a main. It gives me the exception in the race class by move.advance()

Re: NullPointerexception

it gives me an "nullpointerexception" ... It gives me the exception in the race class by move.advance()

It might help if you can post the entire stack trace. There is nothing (obvious) in the advance() method that could be null.

I don't know BlueJ, but a brief glance at its Tutorial suggests that you can somehow run any method. "An execution in BlueJ is usually done by
creating an object and then invoking one of the object’s methods". The following is just a guess, but perhaps you are invoking the run() method of the Race class. If so you actually have three different tracks: move, track, and chWin.

-----

It's not clear what is going on here as the cows gallop around the move track, and yet you ask the chWin track who the winner is.

-----

Null pointer exceptions occur when you use a variable whose value is null (ie its value does not refer to any object) as if it had a nonnull value (ie as if its value referred to an actual object). So, if you are invoking run(), it might be a good idea to check the values of the various tracks. You would expect them to have nonnull values, and things will certainly go wrong if move or chWin are null as you call methods of them (which assumes they refer to something).

Re: NullPointerexception

Both move and chWin are null before i call the run method, and therefor it's crashes, but i don't know how to make them not be null. Move is just refereeing back to the advance() method in RaceTrack, which chooses one of the cows, and move them one. chWin referes back to the Winner() method in RaceTrack, which check if any of the cows have gotten through the track by seeing if the any cow has the value of the length of the track. What i need to to is to make advance() run until one of the cows has the value of the length of the track and then invoke the Winner() method, but i need to do it in the Race class

Re: NullPointerexception

Did you understand what I said about variables being null unless and until you have called the constructor and assigned something to them?

Java Code:

Racetrack move;
//move = new Racetrack(42);
move.advance();

In those few lines of code the commented out line is absolutely critical. Without it move is null and calling advance() will result in a NullPointerException being thrown.

-----

Move is just refereeing back to the advance() method in RaceTrack, which chooses one of the cows, and move them one. chWin referes back to the Winner() method in RaceTrack...

Before you start figuring out where where you put calls to the Racetrack constructor ask yourself a question: how many race tracks are there in this problem? Because that is (probably) how many Racetrack variables you should declare and how many racetrack instances you should create to initialise the variables.

Racetrack instances (like move and chWin) do not "refer back" to methods. Rather, they refer to race tracks. And race tracks have behaviour like advancing their cows and reporting a winner. Note also that race tracks, the way have set them up, each have their own herd of cows.

(I feel you may be missing a basic point: classes are not things but templates or types of thing. Your textbook may help here.)

In your current code you have three race track variables. So there will be three separate "copies" of the advance() and winner() behaviour. And there will be three herds of cows involved. None of this seems right, and I think if you address the question above about the number of race tracks there should be then the NullPointerException will probably go away.