3 Replies - 1320 Views - Last Post: 12 May 2012 - 10:07 PM Rate Topic: -----

#1 Benzoate   User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

NULL POINTER EXCEPTION

Posted 12 May 2012 - 09:09 PM

:) I need some help fixing a Null Pointer Exception on line 81... I really have no clue what null pointer exception means... and for some reason my code is requiring it, but then won't run with it... Help would be appreciated :D

CODE:
import java.awt.event.KeyEvent;
import java.util.Random;

public class map {
	
	static Random rand = new Random();
	static int row = 10;
	static int col = 10;
	static int x = 0;
	static int y = 0;
	static char[][] field = {
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'},
		{'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a'}
	};
	

	
	public static void main(String[] args){
		int l = 1;
		while(l == 1){
			player(null);
			food();
			display();
			restore();
		}
	}
	
	static void display(){
		for(int a = 0; a< row; a++){
			for(int b =0; b<col; b++){
				System.out.print(" "+field[a][b]);
			}
			System.out.println("");
		}
	}
	
	static void food(){
		randomizer();
		field[x][y] = 'O';
		try{
			Thread.sleep(10000);
		}catch(InterruptedException ie){
			ie.printStackTrace();
		}
		for(int a = 0; a<= 10; a++){
			System.out.println("");
		}
	}
	
	public static void randomizer(){
		x = rand.nextInt(10);
		y = rand.nextInt(10);
	}
	
	static int getX(){
		return x;
	}
	static int getY(){
		return y;
	}
	
	public static void restore(){
		getX();
		getY();
		field[x][y] = 'a';
	}
	
	public static void player(KeyEvent e){
		char player = 'T';
		int px = 0;
		int py = 0;
		field[px][py] = player;
		int keyCode = e.getKeyCode();
		switch(keyCode){
		case KeyEvent.VK_UP:
			py = py + 1;
		case KeyEvent.VK_DOWN:
			py = py - 1;
		case KeyEvent.VK_LEFT:
			px = px - 1;
		case KeyEvent.VK_RIGHT:
			px = px + 1;
		}
		try{
			Thread.sleep(1);
		}catch(InterruptedException i){
			i.printStackTrace();
		}
	}

}


The ERROR:

Exception in thread "main" java.lang.NullPointerException
at map.player(map.java:81)
at map.main(map.java:29)

Also... My code is kind of disorganized :( my apologies.. normally I try to write cleaner but... again my apologies

Is This A Good Question/Topic? 0
  • +

Replies To: NULL POINTER EXCEPTION

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: NULL POINTER EXCEPTION

Posted 12 May 2012 - 09:36 PM

The stack track points you to the problem:

Line 29: player(null);
invokes the method player() with KeyCode e = null

Line 81: int keyCode = e.getKeyCode();
attempts to assign null.getKeyCode() to the variable keyCode.

That won't work.
Was This Post Helpful? 1
  • +
  • -

#3 Benzoate   User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: NULL POINTER EXCEPTION

Posted 12 May 2012 - 09:38 PM

Any suggestion on how to fix this?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: NULL POINTER EXCEPTION

Posted 12 May 2012 - 10:07 PM

Yes, don't call the player() method with a null KeyEvent parameter. The purpose of the player() method is to take the KeyEvent it is passed, translate that to a KeyCode, and then perform an action based on that value using a switch. player() should be called with a KeyListener that passes a valid KeyEvent. You can see how to write a KeyListener here.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1