Results 1 to 4 of 4
  1. #1
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default Null Pointer Exception error

    Hi, I have an assignment to make a voting program. The program compile fine, but I get the following runtime error:
    Exception in thread "main" java.lang.NullPointerException
    at BallotPaper.setPreference(BallotPaper.java:5)
    at Test1.main(Test1.java:17)

    Here's the relevant code:
    Ballot Paper:
    Java Code:
        private ArrayBasedMap<Integer, Candidate> candPreference;
        
        public void setPreference( int pref, Candidate name ){
            candPreference.put( pref, name );
        }
    ArrayBasedMap is something we've been told to use. Put returns NULL if the entry is inserted correctly.
    Test1:
    Java Code:
            Candidate Homer = new Candidate( "Homer", "FF" );
            BallotPaper vote1 = new BallotPaper();
            vote1.setPreference( 1, Homer );
    This might be something really simple, but I just can't figure it out.
    Thanks in advance!
    Last edited by tfitz666; 03-28-2010 at 07:21 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by tfitz666 View Post
    Hi, I have an assignment to make a voting program. The program compile fine, but I get the following runtime error:
    Exception in thread "main" java.lang.NullPointerException
    at BallotPaper.setPreference(BallotPaper.java:5)
    at Test1.main(Test1.java:17)

    Here's the relevant code:
    Ballot Paper:
    Java Code:
        private ArrayBasedMap<Integer, Candidate> candPreference;
        
        public void setPreference( int pref, Candidate name ){
            candPreference.put( pref, name );
        }
    Test1:
    Java Code:
            Candidate Homer = new Candidate( "Homer", "FF" );
            BallotPaper vote1 = new BallotPaper();
            vote1.setPreference( 1, Homer );
    This might be something really simple, but I just can't figure it out.
    Thanks in advance!
    From what I can see you never actually created a candPreference object (so the reference initialized and stays null).

    kind regards,

    Jos

  3. #3
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    *Face Palms* That's embarrassing:D Thanks!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by tfitz666 View Post
    *Face Palms* That's embarrassing:D Thanks!
    Thou art forgiven because I make the same mistake more often than not ;-)

    kind regards,

    Jos

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  3. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 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
  •