Results 1 to 5 of 5
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Getting a nullpointer with unit testing

    I'm unit testing a bit and i get a nullpointer at this line in the testcode:
    Java Code:
     boolean result = instance.gotoStartPosition(player);
    This is the whole testcode, just a first prototype created with netbeans:
    Java Code:
     @Test
        public void testGotoStartPosition() {
            System.out.println("gotoStartPosition");
            Speler speler = null;
            PlaygroundController instance = null;
            boolean expResult = false;
            boolean result = instance.gotoStartPosition(player);
            assertEquals(expResult, result);
          
        }
    This is the method i need to test.

    Java Code:
    public boolean gotoStartPosition(Player player)
        {
           boolean returnBool = true;
           if (player!=null){
           
                
                for(Chest chest : playground.chestList)
                {
                    if((chest.getXIndex() == player.getXStart()) && (chest.getYIndex() == player.getYStart()))
                        returnBool = false;
                }
    
                
                if((player.getXIndex() == player.getXStart()) && (player.getYIndex() == player.getYStart()))
                    returnBool = false;
    
                if(returnBool == true)
                {
                    player.setXIndex(player.getXStart());
                    player.setYIndex(player.getYStart());
    
                    player.setNumberOfSteps(player.getNumberOfSteps()+5);
                }
                return returnBool;
              
            }
            else{
                System.out.println("The player class is not instantiated");
            }
            return returnBool;
            
        }
    I expect the nullpointer so thats why i use the if. But it doesn't catch it.
    Any ideas?
    Last edited by aborgeld; 04-16-2011 at 01:43 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aborgeld View Post
    Java Code:
            PlaygroundController instance = null;
            ...
            boolean result = instance.gotoStartPosition(player);
    What do you expect variable 'instance' to be?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    The method i need to test is in the PlaygroundController class.
    So this is and instance of the playgroundcontroller class.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aborgeld View Post
    The method i need to test is in the PlaygroundController class.
    So this is and instance of the playgroundcontroller class.
    No, instance is null because you said so yourself and a NullPointerException is thrown.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Ops ;-). I created and extra empty constructor in this class and made a new instance for the test class it works now. Thanks.

Similar Threads

  1. XML to JSP Unit Testing
    By Abder-Rahman in forum Advanced Java
    Replies: 2
    Last Post: 02-15-2009, 12:35 AM
  2. Unit testing Java EE 5 applications
    By tpbkl in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-23-2008, 02:04 PM
  3. p-unit 0.12
    By Jamie in forum Java Software
    Replies: 0
    Last Post: 06-16-2007, 09:33 AM
  4. p-unit 0.11
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-04-2007, 08:07 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
  •