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 3 of 3

Thread: NULL POINTER EXCEPTION error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation NULL POINTER EXCEPTION error

    Hi, I'm writing a code for my college class. It's my second lab assignment and I'm kinda stuck in rut.
    The program is to show a smiley face animating across a frame and as it hits the wall, it changes color to the color of the wall and reverse direction. Then, the wall changes to the color of the SmileyFace.
    I don't know if I instantiated the objects( "movingSmiley" and "display") correctly or not.
    Note: SmileyFace and SmileyDisplay are the classes my professor wrote for me already.


    It keeps showing me this error.
    Exception in thread "main" java.lang.NullPointerException
    	at SmileyAnimation.hitBottomWall(SmileyAnimation.java:261)
    	at SmileyAnimation.hitSomething(SmileyAnimation.java:113)
    	at SmileyAnimation.animate(SmileyAnimation.java:90)
    	at SmileyFrame.activate(SmileyFrame.java:41)
    	at Smiley.main(Smiley.java:19)


    This is my class from the program that I'm having trouble with:
    // SmileyAnimation.java - Create and animate the smiley
    //
    // ICS 21: Lab Assignment 2
    //
    // Coded by Norman Jacobson October, 2006
    // Direction constants added by Norman Jacobson, October, 2010
     
     
    import java.awt.Color;				// so we can use colors
    import java.util.Random;			// so we can use the random number generator
     
    //	class Color color constants are
    //		BLACK, BLUE, CYAN, GRAY, DARK_GRAY, LIGHT_GRAY, GREEN, MAGENTA,
    //		ORANGE, PINK, RED, WHITE, YELLOW
     
     
    // Panel in which smileys are drawn
    public class SmileyAnimation
    {
    	private SmileyDisplay display;			// screen on which animation appears
    	private SmileyFace movingSmiley;		// smiley that's animated
     
    	// Change left to right, or up to down, or visa versa
    	private static final int REVERSE_DIRECTION = -1;
     
    	private static final int INIT_X_DIRECTION = 1; // Smiley starts moving to the right
    	private static final int INIT_Y_DIRECTION = 0; // smiley starts moving with no vertical movement
     
    	private int currentXMovement;			// 1 = right, -1 = left, 0 = no change
    	private int currentYMovement;			// 1 = down, -1 = up, 0 = no change
     
    	private Random generator;				// random number generator
     
    	private static final long TIME_TO_RUN = 30000; // # of milliseconds to run animation
     
     
    	// Make the smiley to be animated
    	// Paint the starting screen
    	// Set initial smiley movement
    	// Initialize the random number generator
    	public SmileyAnimation(SmileyDisplay d)
    	{
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		SmileyFace movingSmiley = new SmileyFace();
    		SmileyDisplay display = new SmileyDisplay();
    		movingSmiley.getFace().setAttributes(Color.blue, 325,325, 100, 100);
    		movingSmiley.getLeftEye().setAttributes(Color.white, 305, 300, 25, 25);
    		movingSmiley.getRightEye().setAttributes(Color.white, 345, 300, 25, 25);
    		movingSmiley.getSmile().setAttributes(Color.magenta, 325, 350, 35, 35);
    		display.setWallColor("Left", Color.GRAY);
    		display.setWallColor("Right", Color.PINK);
    		display.setWallColor("Top", Color.RED);
    		display.setWallColor("Bottom", Color.YELLOW);
    		display = d;
    		currentXMovement = INIT_X_DIRECTION;
    		currentYMovement = INIT_Y_DIRECTION;
    		Random generator = new Random();
     
    	}
     
     
    	// Reeturn the smiley face
    	public SmileyFace getSmileyFace()
    	{
    		return movingSmiley;
    	}
     
     
    	// Animate the smiley
    	public void animate()
    	{
    		// Set the current time
    		long startTime = System.currentTimeMillis();
     
    		System.out.println("It's great.");
    		// Run the animation for TIME_TO_RUN seconds
    		do
    		{
    			// Continue to move smiley in current direction until it hits
    			// an edge; when that happens, swap color of smiley
    			// and wall and change direction
     
     
     
    			// Use pause() as needed to "slow down" the smiley so that
    			// it does not zip across the screen so fast we can't see what
    			// it's doing!
     
    			// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    			if(hitSomething() == false)
    			{	getSmileyFace().translate(currentXMovement, currentYMovement);
    				System.out.println("It's moving, not changing direction yet.");
    			}
    			else if (hitSomething() == true)
    			{
    				adjustColorAndDirection();
    				getSmileyFace().translate(currentXMovement, currentYMovement);	
    				System.out.println("It's moving, changing direction now.");
    			}
    			else
    				System.out.println("Error, check the method, animate -> do while loop");
     
    			pause(10); // pause for 10 milliseconds
    			System.out.println("SmileyFace is Moved");
    			display.draw(); //helps "smooth" the smileyFace translation to look as if were moving
     
    		} while (System.currentTimeMillis() - startTime < TIME_TO_RUN);
    	}
     
     
    	// Hit something! Return true if hit any wall; false if not hit any of them
    	private boolean hitSomething()
    	{
    		if(hitBottomWall() == true)
    			return true;
    		else if(hitTopWall() == true)
    			return true;
    		else if(hitLeftWall() == true)
    			return true;
    		else if(hitRightWall() == true)
    			return true;
    		else
    			return false;
     
    	}
     
     
    	// Hit an edge: swap color with smiley background
    	//   and move off in a new direction away from the wall
    	// Use helper methods adjustColor() and adjustDirection()
    	private void adjustColorAndDirection()
    	{
    		if(hitSomething() == true)
    			if(hitBottomWall() == true)
    			{	
    				adjustColor("Bottom");
    				adjustDirection("Bottom");
    			}
    			else if(hitTopWall() == true)
    			{	
    				adjustColor("Top");
    				adjustDirection("Top");
    			}
    			else if(hitLeftWall() == true)
    			{	
    				adjustColor("Left");
    				adjustDirection("Left");
    			}
    			else if(hitRightWall() == true)
    			{	
    				adjustColor("Right");
    				adjustDirection("Right");
    			}
    			else
    				System.out.println("Error, check the method, adjustColorAndDirection");
     
    	}
     
     
    	// Swap the colors of the wallHit wall and the smiley
    	// wallHit can be "Left", "Right", "Top", or "Bottom",
    	//  these strings corresponding to the walls they name
    	private void adjustColor(String wallHit)
    	{
    		Color wall = display.getWallColor(wallHit);
    		display.setWallColor(wallHit, movingSmiley.getFace().getColor());
    		movingSmiley.getFace().setColor(wall);
     
    	}
     
     
    	// Change the smiley's direction so it is away from
    	// the wall just hit.
    	// wallHit can be "Left", "Right", "Top", or "Bottom",
    	//  these strings corresponding to the walls they name
    	private void adjustDirection(String wallHit)
    	{
    		// If hit top or bottom wall, y direction is reversed,
    		// x direction can be to the left, to the right, or
    		// no movement at all (translation of 0); it is randomly
    		// chosen
     
    		// If hit left or right wall, x direction is reversed,
    		// y direction can be up, down, or no movement
    		// (translation of 0); it is randomly chosen
     
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		if(wallHit == "Botton" || wallHit == "Top" )
    		{	
    			currentYMovement = currentYMovement * REVERSE_DIRECTION;
    			currentXMovement = currentXMovement * (generator.nextInt(3) - 1);
    		}	
    		else if(wallHit == "Left" || wallHit == "Right")
    		{	
    			currentXMovement = currentXMovement * REVERSE_DIRECTION;
    			currentYMovement = currentYMovement * (generator.nextInt(3) - 1);
    		}
    		else
    			System.out.println("Error, Check the method called, adjustDirection -> else");
     
    	}
     
     
    	// Return true if hit left wall, false otherwise
    	private boolean hitLeftWall()
    	{
    		// Hit wall if x coordinate of leftmost point of smiley is
    		// same or less than edge of the left wall,
    		// and the smiley is moving toward this wall
     
     
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		if(getSmileyFace().getLeftEdge() <= display.getEdge("Left"))
    			return true;
    		else
    			return false;
     
    	}
     
     
    	// Return true if hit right wall, false otherwise
    	private boolean hitRightWall()
    	{
    		// Hit wall if x coordinate of rightmost point of smiley is
    		// same or greater than edge of the right wall,
    		// and the smiley is moving toward this wall
     
     
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		if(getSmileyFace().getRightEdge() >= display.getEdge("Right"))
    			return true;
    		else
    			return false;
    	}
     
     
    	// Return true if hit top wall, false otherwise
    	private boolean hitTopWall()
    	{
    		// Hit wall if y coordinate of top-most point of smiley is
    		// same or less than edge of the top wall,
    		// and the smiley is moving toward this wall
     
     
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		if(getSmileyFace().getTopEdge() <= display.getEdge("Top"))
    			return true;
    		else
    			return false;
    	}
     
     
    	// Return true if hit bottom wall, false otherwise
    	private boolean hitBottomWall()
    	{
    		// Hit wall if y coordinate of bottom-most point of smiley is
    		// same or greater than edge of the bottom wall,
    		// and the smiley is moving toward this wall
     
     
    		// *** REPLACE THIS COMMENT WITH YOUR CODE ***
    		if (getSmileyFace().getBottomEdge() >= display.getEdge("Bottom"))
    			return true;
    		else
    			return false;
    	}
     
    	// Wait x milliseconds before moving to next animation frame
    	private void pause(long millisec)
    	{
    		long startTime = System.currentTimeMillis();
    		long endTime;
    		do
    		{
    			endTime = System.currentTimeMillis();
    		} while (endTime - startTime < millisec);
    	}
     
    }



    Lab Assignment Manual from the professor's Website
    Attached Images Attached Images
    • File Type: jpg 1.jpg (18.4 KB, 1 views)
    • File Type: jpg 2.jpg (15.7 KB, 0 views)
    • File Type: jpg 3.jpg (15.9 KB, 0 views)
    • File Type: jpg 4.jpg (15.3 KB, 1 views)
    • File Type: jpg 5.jpg (16.8 KB, 0 views)
    • File Type: jpg 6.jpg (15.0 KB, 0 views)
    • File Type: jpg 7.jpg (13.5 KB, 0 views)
    Last edited by beefwithrice; October 26th, 2011 at 02:05 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: NULL POINTER EXCEPTION error

    In the constructor, change the line:
    SmileyDisplay display = new SmileyDisplay();
    //with
    display = new SmileyDisplay();
    because you already declared the display object above.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:03 PM.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: NULL POINTER EXCEPTION error

    Quote Originally Posted by daniel.j2ee View Post
    In the constructor, change the line:
    SmileyDisplay display = new SmileyDisplay();
    //with
    display = new SmileyDisplay();
    because you already declared the display object above.
    You must read the stacktrace...

    @beefwithrice: I guess stack trace is descriptive enough to take you to the root.

Similar Threads

  1. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  2. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM