Results 1 to 7 of 7
  1. #1
    barr94 is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Null Pointer error, need help

    Hey guys, I'm new here. I've started Computer Science 101 at university and I am in dire need of help.

    the problem :
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Lovely.<init>(Lovely.java:55)
    	at A2JPanel.actionPerformed(A2JPanel.java:64)
    	at javax.swing.Timer.fireActionPerformed(Unknown Source)
    	at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$200(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    the "lovely" class :

    Java Code:
    import java.awt.*;
    									
    public class Lovely {    
    	public static final Rectangle[] STRIP_AREAS = A2Constants.STRIP_AREAS;    
    	public static final int STRIP_AREAS_LHS = A2Constants.STRIP_AREAS_LHS;  
    	public static final int STRIP_AREAS_RHS = A2Constants.STRIP_AREAS_RHS;  
    									  
    	public static final int LOVELY_SIZE = A2Constants.LOVELY_SIZE; 
    	public static final Color LOVELY_COLOUR = new Color(184, 200, 255);
    									  
    	public static final Rectangle GAME_SCREEN_AREA = A2Constants.GAME_SCREEN_AREA;
    									  
    	public static final Font VERY_SMALL_FONT = A2Constants.VERY_SMALL_FONT;
    	public static final int VERY_SMALL_FONT_SIZE = A2Constants.VERY_SMALL_FONT_SIZE;
    									  
    	private Rectangle area;
    	private int moveX;
    	private int value;
    	private boolean isVisible;
    									  
    									  
    	public Lovely() { 
    	int range = 3;
    	int minimum = 0;
    	int whichStrip = (int) (Math.random() * range + minimum);
    	value = 3;
    	System.out.println(whichStrip);
    	
    	if(whichStrip == 0){
    		area.x = STRIP_AREAS[0].x;
    		area.y = STRIP_AREAS[0].y;
    		area.width = LOVELY_SIZE;
    		area.height = LOVELY_SIZE;
    		moveX = 4;	
    		
    	
    	}
    									    		   
    	if(whichStrip == 1){
    			area.x = STRIP_AREAS[whichStrip].x + GAME_SCREEN_AREA.width;
    			area.y = STRIP_AREAS[whichStrip].y + GAME_SCREEN_AREA.width;
    			area.width = LOVELY_SIZE;
    			area.height = LOVELY_SIZE;
    			moveX = -4;
    			
    			
    			
    	}
    	
    	if(whichStrip == 2){
    		area.x = STRIP_AREAS[2].x;
    		area.y = STRIP_AREAS[2].y;
    		area.width = LOVELY_SIZE;
    		area.height = LOVELY_SIZE;
    		moveX = 4;
    		
    	}
    	if(whichStrip == 3){
    		area.x = (int) STRIP_AREAS[whichStrip].x;
    		area.y = STRIP_AREAS[whichStrip].y + STRIP_AREAS[whichStrip].height;
    		area.width = LOVELY_SIZE;
    		area.height = LOVELY_SIZE;
    		moveX = -4;
    
    		
    		
    	}
    									    
    	}
    									  
    									  
    	public boolean getIsVisible() {
    		return isVisible;
    	}
    									  
    	public void setIsVisible(boolean visible) {
    		isVisible = visible;
    	}
    									  
    	public Rectangle getArea() {
    		return area;
    	}
    									  
    	public int getValue() {
    		return value;
    	}
    									  
    	public Point getCentrePt() {
    		int midOfX = (area.x) + (LOVELY_SIZE/2);
    	    int midOfY = (area.y) + (LOVELY_SIZE/2);
    									    
    	    return new Point(midOfX, midOfY);
    	}
    									  
        public boolean isInsideStripArea() {
    		if(area.x <= STRIP_AREAS_RHS || area.x >= STRIP_AREAS_LHS){
    			return true;
    									      
    		}
    		else{
    			return false;
    		}
    	}
    	public void move() {
    		if(moveX > 0){
    			area.x = area.x + moveX;  
    			if(! isInsideStripArea()){
    				setIsVisible(false);
    												
    			}
    		}
    		if(moveX < 0){
    			area.x = area.x - moveX;
    			if(! isInsideStripArea()){
    				setIsVisible(false);
    			}
    		}
    	}
    									
        public void draw(Graphics g) {
    		//if(isVisible){
    		g.setColor(Color.BLACK);
    	    g.drawRect(area.x, area.y, area.width, area.height);  
    	    
    	    //}
    										 
    									   
    	}
    	public void displayValue(Graphics g){
    		//getCentrePt();
    										 
    	}
    }
    Action preformed method :

    Java Code:
     
    public void actionPerformed(ActionEvent e){
    	  if(Math.random() < 0.3){
    		 lovlies[numberOfLovelies] = new Lovely();
    		  numberOfLovelies++;
    	  } 
    	  
    	  red.move();
    
    	  repaint();
    	  }
    could someone point me in the right direction to fixing this null pointer, i'm really stuck.

    thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Null Pointer error, need help

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at Lovely.<init>(Lovely.java:55)
        at A2JPanel.actionPerformed(A2JPanel.java:64)
    The stacktrace is saying that in actionPerformed() you are creating a new Lovely instance. That's what the "<init>" is getting at: the problem lies in the Lovely constructor or some other code that might be run when an instance is created.

    So, I guess line 64 of A2JPanel.java is "lovlies[numberOfLovelies] = new Lovely();" since that is where a new Lovely instance is created.

    But which is line 55 of Lovely.java? Because that is where you problem lies. Note it is not line 55 as displayed here. A NullPointerException occurs when you use a variable (or other expression) as if it had a value when it is really null. Nothing on the line displayed as 55 - "moveX = 4;" - could possibly be null.

    Look for yourself at the variables on line 55 and see if you can spot one that might be null. (Eg print the values of everything that *could* be null). If something is null and it shouldn't be, go back through your code to see where you thought you had assigend it a value and see why that didn't happen. If you get stuck, say which line 55 is.

  3. #3
    barr94 is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Null Pointer error, need help

    Line 55 is where I am trying to assign the X position of the 'lovely' object.

    located here:
    Java Code:
    if(whichStrip == 2){
    		area.x = STRIP_AREAS[2].x;    <------
    		area.y = STRIP_AREAS[2].y;
    		area.width = LOVELY_SIZE;
    		area.height = LOVELY_SIZE;
    		moveX = 4;
    I have no idea why this is happening, this happens to all of the assignments, ie:

    Java Code:
    if(whichStrip == 1){
    			area.x = STRIP_AREAS[whichStrip].x + GAME_SCREEN_AREA.width; <---- happens here too.
    			area.y = STRIP_AREAS[whichStrip].y + GAME_SCREEN_AREA.width;
    			area.width = LOVELY_SIZE;
    			area.height = LOVELY_SIZE;
    			moveX = -4;

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Null Pointer error, need help

    Quote Originally Posted by barr94 View Post
    Line 55 is where I am trying to assign the X position of the 'lovely' object.

    located here:
    Java Code:
    if(whichStrip == 2){
    		area.x = STRIP_AREAS[2].x;    <------
    		area.y = STRIP_AREAS[2].y;
    		area.width = LOVELY_SIZE;
    		area.height = LOVELY_SIZE;
    		moveX = 4;
    I have no idea why this is happening
    Perhaps I wasn't clear, but something on that line is null when you don't intend it to be. For instancearea might be null, or STRIP_AREAS[2] might be null, or even STRIP_AREAS might be null. If any one of those three is null then the assignment on that line won't make sense and you'll get a NullPointerException. Ask if that doesn't make sense.

    So, the first order of business is to find out which thing is null:

    Java Code:
    if(whichStrip == 2){
        System.out.println("About to assign STRIP_AREAS[2].x to area.x");
        System.out.println("area=" + area);
        System.out.println("STRIP_AREAS=" + STRIP_AREAS);
        System.out.println("STRIP_AREAS[2]=" + STRIP_AREAS[2]);
    
        area.x = STRIP_AREAS[2].x;    <------
        area.y = STRIP_AREAS[2].y;
        area.width = LOVELY_SIZE;
        area.height = LOVELY_SIZE;
        moveX = 4;
    If any of them is null, go back through your code and try and find where you thought you assigned it a value.

  5. #5
    barr94 is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Null Pointer error, need help

    Quote Originally Posted by pbrockway2 View Post
    Perhaps I wasn't clear, but something on that line is null when you don't intend it to be. For instancearea might be null, or STRIP_AREAS[2] might be null, or even STRIP_AREAS might be null. If any one of those three is null then the assignment on that line won't make sense and you'll get a NullPointerException. Ask if that doesn't make sense.

    So, the first order of business is to find out which thing is null:

    Java Code:
    if(whichStrip == 2){
        System.out.println("About to assign STRIP_AREAS[2].x to area.x");
        System.out.println("area=" + area);
        System.out.println("STRIP_AREAS=" + STRIP_AREAS);
        System.out.println("STRIP_AREAS[2]=" + STRIP_AREAS[2]);
    
        area.x = STRIP_AREAS[2].x;    <------
        area.y = STRIP_AREAS[2].y;
        area.width = LOVELY_SIZE;
        area.height = LOVELY_SIZE;
        moveX = 4;
    If any of them is null, go back through your code and try and find where you thought you assigned it a value.
    All Values are being assigned properally, however they are not the correct value and do not draw properly , the null pointer can be fixed by making
    Java Code:
    area = STRIP_AREAS[whichStrip];
    Thanks for the help, I've gotten rid of the null, but I've swapped one problem for another.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Null Pointer error, need help

    I take it that area was null.

    If there's some other problem, you'll have to explain what it is. Ie describe the specific cercumstances and what the "proper" value shpuld be and what the actual value is.

  7. #7
    barr94 is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Null Pointer error, need help

    Quote Originally Posted by pbrockway2 View Post
    I take it that area was null.

    If there's some other problem, you'll have to explain what it is. Ie describe the specific cercumstances and what the "proper" value shpuld be and what the actual value is.
    the area instance variable doesn't seem to be taking the values I try to give, therefore becoming null.

    So i guess problem solved :D

    Thanks a lot.

Similar Threads

  1. Unusual null pointer error
    By PRW56 in forum New To Java
    Replies: 8
    Last Post: 03-22-2012, 07:58 PM
  2. null pointer error help!!
    By fakepics500 in forum New To Java
    Replies: 1
    Last Post: 07-16-2011, 03:57 PM
  3. Error = Null pointer with inner class
    By jonytek in forum New To Java
    Replies: 3
    Last Post: 05-11-2011, 05:10 AM
  4. Null Pointer Exception error
    By tfitz666 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 08:20 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
  •