Results 1 to 8 of 8
  1. #1
    kyussy is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default private static setter method problem

    Hi there, I'm working on my assignment for uni and I'm having a few problems with one of the questions.
    So I'm trying to set the class variable currentLeader to an object of the LongJumper class, it should be null on first run.

    Java Code:
       /*class variables */
      
       private static LongJumper currentLeader;
    then after the first jumper goes he should be set as the currentLeader, which I think I have under control but it's all coming unstuck when I try to use the private setter.

    Java Code:
       /**
        * A private class setter method for the class variable currentLeader
        */
    private static void setCurrentLeader(LongJumper aJumper)
       {
         LongJumper.currentLeader = aJumper;
       }
    Which is called by this..
    Java Code:
         if  (LongJumper.getCurrentLeader()== null)
         {
             LongJumper.setCurrentLeader(this);
         }
    It complies ok, but when I run the program the currentLeader always stays null. I'd really appreciate it if someone would be kind enough to offer me a few pointers on where I'm going wrong this, as I've pretty much run out of ideas and my course work doesn't seem to have included this type of scenario.

    Thanks

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Where is that code called from? Where does it reside?

  3. #3
    kyussy is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    The if statement?

    Java Code:
        
    public void jump()
    {
         super.jump();
         this.incrementMedalCount();
         if (this.getMedalCount() >     LongJumper.getCurrentLeader().getMedalCount())
         {
             LongJumper.setCurrentLeader(this);
         }
         if (LongJumper.getCurrentLeader()== null)
         {
            LongJumper.setCurrentLeader(this);
         }
       
       }
    Everything is in the LongJumper class

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    What does getCurrentLeader() look like?

    Are you sure jump() is being called?
    Stick some debugging println's in there to check. Do that in the setCurrentLeader method as well.

  5. #5
    kyussy is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
       public static LongJumper getCurrentLeader()
       {
          return currentLeader;
       }
    Unfortunately we haven't started the 'dealing with errors' section of the course, it's up next though:rolleyes: so I don't have any experience with debugging.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I'm guessing you are seeing a null pointer????

    I would not have this section...
    Java Code:
    if (LongJumper.getCurrentLeader()== null)
         {
            LongJumper.setCurrentLeader(this);
         }
    below this section

    Java Code:
    if (this.getMedalCount() >     LongJumper.getCurrentLeader().getMedalCount())
         {
             LongJumper.setCurrentLeader(this);
         }

    That is just asking for a null pointer to be thrown because if the current leader is null, it will first attempt to get its medal count, thus a null pointer. But if it does pass through the first if statement, then the current leader is not null and it would fail the second if statement always.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    That's a good point. And because the OP hasn't said they're getting an NPE I would guess that that code is simply not being run at all.

  8. #8
    kyussy is offline Member
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Thanks guys, that's everything working fine now.

    I hadn't even considered the if statement, as it was only supposed to be temporary until I figured out why I couldn't use an 'or' statement in the condition. I guess it just goes to show you can't take anything for granted.

    Thanks once again for all you help.:D

Similar Threads

  1. Null Pointer Exception via setter method?
    By zerkz in forum New To Java
    Replies: 9
    Last Post: 10-29-2009, 09:00 PM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  3. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  4. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

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