Results 1 to 5 of 5
  1. #1
    kendraheartt is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Exclamation Need Help With Null Pointer Exception

    Okay so I am writing a program for a space invaders game, and i keep getting a null pointer exception in my bomb class, missile class, and shield class. If anyone could help me out I would really appreciate it I've only been taking Java for about a year, so this is probably a really easy problem, but I just need a little bit of help to get me on the right track I'm also new to this forum so i don't really know what I'm doing, sorry if I do something wrong

    The Bomb class

    Java Code:
    public class Bomb extends ActiveObject
    {
    public class Bomb extends ActiveObject
    {
     	/*
    	 * constants
    	 */
    
    	private static final int SPEED =  3;
    	private static final int DELAY = 33;
    
     	/*
    	 * instance fields
    	 */
    
    	private FilledRect theBomb;			// the bomb being dropped
    	private FilledRect theMissile;		// the missile being shot off by the base
    	private LaserBase theBase;			// the laser base for the game
    	private Shield theShield;			// the shield for the game
    
    	/**
    	 * Bomb constructor.
    	 *
    	 * @param x the X coordinate where to create the bomb
    	 * @param y the Y coordinate where to create the bomb
    	 * @param base the laser base associated with the game
    	 * @param canvas the canvas in which the image is created
    	 */
    
    	public Bomb (double x, double y, LaserBase base, FilledRect missile, Shield shield, DrawingCanvas canvas)
    	{
    		/*
    		 * construct the actual bomb
    		 */
    
    		theBomb = new FilledRect(x, y, 2, 10, canvas);
    		theBomb.setColor(Color.GREEN);
    
    		/*
    		 * remember the values of relevant variables
    		 */
    
    		theBase = base;
    		theMissile = missile;
    		theShield = shield;
    
    		start ();
    	}
    
    	/**
    	 * Move the bomb - execute it's behavior
    	 */
    
    	public void run ()
    	{
    		/*
    		 * bomb will go down until it gets to the bottom of the screen or hits the
    		 * laser base
    		 */
    
    		while (((theBomb.getY () + theBomb.getHeight ()) <
    				SpaceInvaders.BOTTOM_BORDER) && !theBase.isHit (theBomb) && !theShield.isHit (theMissile, theBomb))
    		{
    			theBomb.move (0, SPEED);
    			pause (DELAY);
    		}
    		theBomb.removeFromCanvas ();
    	}
    }
    The Shield Class

    Java Code:
    	public Shield (double x, double y, DrawingCanvas canvas)
    	{
    		shield = new FilledRect[ROW][COL];
    		Random randGen = new Random ();
    
    		for (int row = 0; row < shield.length; row++)
    		{
    			for (int col = 0; col < shield[0].length; col++)
    			{
    				shield[row][col] = new FilledRect (x + row * 3, y + col * 3, 3, 3, canvas);
    
    				switch (randGen.nextInt (5))
    				{
    					case 0: shield[row][col].setColor (Color.PINK);
    							break;
    					case 1: shield[row][col].setColor (Color.CYAN);
    							break;
    					case 2: shield[row][col].setColor (Color.YELLOW);
    							break;
    					case 3: shield[row][col].setColor (Color.ORANGE);
    							break;
    					case 4: shield[row][col].setColor (Color.MAGENTA);
    							break;
    				}
    			}
    		}
    	}
    
    	/**
    	 * isHit method
    	 */
    
    	public synchronized boolean isHit (FilledRect missile, FilledRect bomb)
    	{
    		for (int row = 0; row < shield.length; row++)
    		{
    			for (int col = 0; col < shield[0].length; col++)
    			{
    				if (=====>(!shield[row][col].isHidden () && missile.overlaps (shield[row][col])) <=======  
    					|| (!shield[row][col].isHidden () && shield[row][col].overlaps (bomb)))
    				{
    					shield[row][col].hide ();
    					missile.hide ();
    					bomb.hide ();
    
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    }
    The Missile Class

    Java Code:
    public class Missile extends ActiveObject
    {
     	/*
    	 * constants
    	 */
    
    	private static final int SPEED = -5;
    	private static final int DELAY = 33;
    
     	/*
    	 * instance fields
    	 */
    
    	private FilledRect theMissile;			// the missile being shot
    	private FilledRect theBomb;				// the bomb shot from the alien
    	private SpaceShip theShip;				// the space ship for the game
    	private Invaders theInvaders;			// the invaders for the game
    	private Shield theShield;				// the shield
    
     	/*
    	 * class fields
    	 */
    
    	private static int count = 0;			// number of missiles on screen
    
    	/**
    	 * Missile constructor.
    	 *
    	 * @param x the X coordinate where to create the missile
    	 * @param y the Y coordinate where to create the missile
    	 * @param invaders the invaders associated with the game
    	 * @param spaceShip the space ship associated with the game
    	 * @param canvas the canvas in which the image is created
    	 */
    
    	public Missile (double x, double y, Invaders invaders, SpaceShip spaceShip, FilledRect bomb, Shield shield,
    					DrawingCanvas canvas)
    	{
    		/*
    		 * construct the actual missile
    		 */
    
    		theMissile = new FilledRect (x, y, 2, 10, canvas);
    		theMissile.setColor (Color.RED);
    
    		/*
    		 * remember the values of relevant variables
    		 */
    
    		theShip = spaceShip;
    		theInvaders = invaders;
    		theBomb = bomb;
    		theShield = shield;
    		count++;
    
    		start ();
    	}
    
    	/**
    	 * Retrieve the number of missiles currently on the screen.
    	 *
    	 * @return number of missiles still on the screen
    	 */
    
    	public static int onScreen ()
    	{
    		return count;
    	}
    
    	/**
    	 * Move the missile - execute it's behavior
    	 */
    
    	public void run ()
    	{
    		/*
    		 * missile will go up until it gets to the top of the screen or hits one of
    		 * the invaders or the space ship
    		 */
    
    		while (((theMissile.getY () + theMissile.getHeight ()) >
    				SpaceInvaders.TOP_BORDER) && !theShip.isHit(theMissile) &&
    				!theInvaders.isHit(theMissile) && =======> !theShield.isHit (theMissile, theBomb)) <=======
    		{
    			theMissile.move (0, SPEED);
    			pause (DELAY);
    		}
    		theMissile.removeFromCanvas ();
    		count--;
    	}
    }
    Okay so I put an arrow in the bomb missile and shield class where the I keep getting a null pointer exception.
    The weird thing is, is if I take isolate them in the shield class, they work properly, but if i include both, they don't work... so I'm not sure whats going on... If I do this:

    Java Code:
    	public synchronized boolean isHit (FilledRect missile, FilledRect bomb)
    	{
    		for (int row = 0; row < shield.length; row++)
    		{
    			for (int col = 0; col < shield[0].length; col++)
    			{
    				if ((!shield[row][col].isHidden () && missile.overlaps (shield[row][col])) 
    				{
    					shield[row][col].hide ();
    					missile.hide ();
    
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    }
    and take out the line that was getting the null pointer exception in the bomb class, the missile will create a hole in the shield, and vice versa for the bomb... I honestly have no clue as to why they work separately but not together...
    If anyone could help me out i would really really appreciate it!!

    Thanks again!

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

    Default Re: Need Help With Null Pointer Exception

    Can you print the full stack trace.
    Because if that is the line the NPE originates from then theShield is null (for the code in Missile).
    Can't tell for the first example, so you'll have to check for nulls and see which thing is causing the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kendraheartt is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need Help With Null Pointer Exception

    Exception in thread "Thread-35" java.lang.NullPointerException
    at objectdraw.Drawable2D.overlaps (Drawable2D.java:123)
    at Shield.isHit(Shield.java:103)
    at Missile.run(Missile.java:110)
    Exception in thread "Thread-37" java.lang.NullPointerException
    at objectdraw.Drawable2D.overlaps (Drawable2D.java:123)
    at Shield.isHit(Shield.java:103)
    at Bomb.run(Missile.java:87)

    is this what you are looking for?? Sorry I'm fairly new to java

  4. #4
    kendraheartt is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Need Help With Null Pointer Exception

    The only problem is that I don't get is when I run them separately, there are no null pointer exceptions, but when I run them together I get null pointers, so I don't understand how that is possible...

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

    Default Re: Need Help With Null Pointer Exception

    objectdraw.Drawable2D.overlaps (Drawable2D.java:123)

    Something on that line in that class is null.
    That's not a standard JRE class so I have no idea what is the problem.
    Check you are initialising the class correctly, which I'm guessing is a parent of ActiveObject (which all your stuff seems to extend).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. null pointer exception help
    By captain_turkiye in forum New To Java
    Replies: 17
    Last Post: 12-04-2011, 12:27 AM
  2. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:31 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  4. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  5. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 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
  •