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()
Code:public class Cow
{
private String name;
private String breed;
public Cow(String name, String breed)
{
this.name = name;
this.breed = breed;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
}
Code:import java.util.Random;
public class RaceTrack
{
int tracklength;
Cow oldCow1, oldCow2, oldCow3, oldCow4, chCow;
int valCow1, valCow2, valCow3, valCow4;
public RaceTrack(int x)
{
this.tracklength = x+0;
valCow1 = 0;
valCow2 = 0;
valCow3 = 0;
valCow4 = 0;
oldCow1 = new Cow("Speedo", "bad");
oldCow2 = new Cow("Moody", "better");
oldCow3 = new Cow("Fast", "best");
oldCow4 = new Cow("Slow", "worst");
chCow = new Cow("test", "test");
}
public void advance()
{
int cowpick = new Random().nextInt(4);
if(cowpick == 0)
{
valCow1 = valCow1 +1;
}
else if(cowpick == 1)
{
valCow2 = valCow2 + 1;
}
else if (cowpick == 2)
{
valCow3 = valCow3 + 1;
}
else
{
valCow4 = valCow4 + 1;
}
}
public Cow Winner()
{
if(valCow1 == this.tracklength)
{
return oldCow1;
}
else if(valCow2 == this.tracklength)
{
return oldCow2;
}
else if(valCow3 == this.tracklength)
{
return oldCow3;
}
else if(valCow4 == this.tracklength)
{
return oldCow4;
}
return null;
}
}
Code:public class Race
{
RaceTrack move;
RaceTrack track;
RaceTrack chWin;
public Race(int x)
{
track = new RaceTrack(x);
}
public Cow run()
{
do
{
move.advance();
}
while(chWin.Winner() == null );
return chWin.Winner();
}
}
