Results 1 to 2 of 2
  1. #1
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default JUnit test nullPointerException

    Hey guys, I'm new to JUnit testing and java and i'm attempting my first JUnit test:

    Java Code:
    	@Test
    	public final void testColour() {
    		// run the main function
    		Main.MainControls testC = new Main.MainControls();
    
    		for (int i = 0; i < Main.N; i++) {
    		Main.playerObject.setX(Main.barriers[i].getX());
    		Main.playerObject.setY(Main.barriers[i].getY());
    		Main.mugeTest.update(20);
    		}
    
    		for (int i = 0; i < Main.N; i++) {
    
    			System.out.println(Main.playerObject.getX());
    			testC.update(20);
    			assertEquals(0.1, Main.barriers[i].getColour(i),0.00001);
    		}
    
    	}

    and I keep getting a nullPointerException at
    Java Code:
    		Main.playerObject.setX(Main.barriers[i].getX());
    Can anyone advise me what could be incorrect?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: JUnit test nullPointerException

    Quote Originally Posted by danboy4 View Post
    Can anyone advise me what could be incorrect?
    Too much :)
    - Main.playerObject could be null
    - but I suspect Main.barriers[i] is null. Do you have created that array? -> barriers = new ....[...]; ?

    Debug your code (best with a debugger)! Or System.out.print(Main.playerObject) ? System.out.print(barriers[0]) ? ....
    NPE are easy to find and fix

Similar Threads

  1. JUnit Test in Eclipse
    By Wolfsy in forum Eclipse
    Replies: 1
    Last Post: 03-31-2012, 01:12 AM
  2. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 09:41 PM
  3. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Tags for this Thread

Posting Permissions

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