Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NullPointerException Error

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy NullPointerException Error

    Hi,

    I keep receiving a NullPointerException and have been unable to find a way to resolve it. Any assistance would be much appreciated.

    public class FitnessFrog extends Frog
    {
       // instance variable
       private int totalDistanceSprinted;
       private Random randomNumber;
       public FitnessFrog sporty1;
       public FitnessFrog sporty2;
     
       // constructor
       /**
        * Constructor for objects of class FitnessFrog.
        */
       public FitnessFrog()
       {
          super();
          this.randomNumber = new Random();      
          this.totalDistanceSprinted = 0;
       }
     
       /**
        * Setter for totalDistanceSprinted.
        */
       private void setTotalDistanceSprinted(int aValue)
       {
          this.totalDistanceSprinted = aValue;
       }
     
       /**
        * Getter for totalDistanceSprinted.
        */
       public int getTotalDistanceSprinted()
       {
          return this.totalDistanceSprinted;
       }      
     
       /**
        * Sets the value of the totalDistanceSprinted variable to 0.
        * Returns no value.
        */
       public void resetTotalDistanceSprinted()
       {
          this.setTotalDistanceSprinted(0);
       }
     
       /**
        * Returns a random integer value specifying a sprint length 
        * between 2 and 5 inclusive.
        */
       private int getSprintLength()
       {
          return (this.randomNumber.nextInt(4) + 2);
       }   
     
       /**
        * Getter for sporty1.
        */
       public FitnessFrog getSporty1()
       {
          return this.sporty1;
       }  
     
       /**
        * Getter for sporty2.
        */
       public FitnessFrog getSporty2()
       {
          return this.sporty2;
       }   
     
       /**
        *     * 
        * Saves the colour of the receiver, then sets the colour of the
        * receiver to red and gets a sprint length. Next enters a loop 
        * and on each iteration does the following: First, checks if the 
        * receiver is on the last stone (numbered 11) in which case moves 
        * the receiver to the first stone (numbered 1), otherwise moves
        * the receiver right; then increments the total distance sprinted.
        * Upon exit from the loop, casues the receiver to jump once and
        * then resets its colour. Returns no value.
        */
       public void sprint()
       {
          //save colour of the receiver
           OUColour colour1 = this.getSporty1().getColour();
           OUColour colour2 = this.getSporty2().getColour();
     
          //Set colour of receiver to red & get sprint length
         this.setColour(OUColour.RED);
         this.getSprintLength();
     
         //
         if(this.position == 11)
         {
          this.home();
         } else this.right(); this.setTotalDistanceSprinted(this.getTotalDistanceSprinted()+ getSprintLength() + 1);
     
         // Reset receivers colour
          this.getSporty1().setColour(colour1);
          this.getSporty2().setColour(colour2);
       }
     }

    Thanks,
    Steven
    Last edited by stevenroberts; May 6th, 2014 at 11:06 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NullPointerException Error

    I keep receiving a NullPointerException
    Please copy the full text of the error message and paste it here.

    Some problems I see:
    multiple statements on one line
    missing {}s for statement(s) following else
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException Error

    Hi Norn,

    Full error message is below:

    java.lang.NullPointerException
    in FitnessFrog.sprint(FitnessFrog.java:95)
    in Coach.train(Coach.java:99)
    in (OUWorkspace:1)


    Cheers.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NullPointerException Error

    java.lang.NullPointerException
    in FitnessFrog.sprint(FitnessFrog.java:95)
    There is a null value at line 95 when it is executed. Look at line 95 and find the variable with the null value and then backtrack in the code to see why the variable is null.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. nullpointerexception error
    By denbal87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 6th, 2014, 10:15 PM
  2. Error, NullPointerException
    By alex067 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 14th, 2012, 09:05 AM
  3. NullPointerException Error
    By nicsa in forum Exceptions
    Replies: 6
    Last Post: November 19th, 2011, 05:32 PM
  4. NullPointerException error
    By pyrotecc25 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2011, 01:17 PM
  5. NullPointerException error
    By blazerix in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 24th, 2011, 09:59 PM

Tags for this Thread