Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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()

    Java 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;
    } 
    }
    Java 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; 
        
    }
    
    }
    Java 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();
    
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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).

    Java Code:
    public Cow run()
    {
        System.out.println("The cows are ready to run.  Marshalls report:");
        System.out.println("    move=" + move);
        System.out.println("    track=" + track);
        System.out.println("    chWin=" + chWin);
        do
        {
         move.advance();
        }
        while(chWin.Winner() == null );
         
        return chWin.Winner();
     
    }
    Things will only get nonnull (/good/useful) values when you call their constructor and assign the result to the variable:

    Java Code:
    Thing foo;
    Thing bar;
    
    foo = new Thing();
    
    foo.doSomething(); // ok
    bar.doSomething(); // bad!  This will throw a NullPointerException
    Last edited by pbrockway2; 11-25-2011 at 11:07 PM. Reason: (tried to separate three different points)

  3. #3
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: NullPointerexception

    i don't know how to make them not be null.
    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.
    Last edited by pbrockway2; 11-26-2011 at 02:07 AM.

  5. #5
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerexception

    Thanks for the help. I didn't need to create three tracks and that's what messed it up :)

Similar Threads

  1. NullPointerException
    By speedzojie@gmail.com in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 06:39 PM
  2. NullPointerException
    By donchini in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 02:11 AM
  3. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 09:26 PM
  4. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 02:14 PM
  5. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 11:51 PM

Posting Permissions

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