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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Java SnakeGame

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java SnakeGame

    Hi, im new to this and i tried to make a runnable snake game. When i try to run my code i get this:

    Exception in thread "main" java.lang.NullPointerException
    	at Game.Move(Game.java:85)
    	at Game.run(Game.java:244)
    	at Game.main(Game.java:38)

    The game runs, but the snake doesn't move, and apples are spawned everywhere.
    I can't post all the code, so here are Move, run and main code:
    public static void main(String[] args) {
    		JFrame frame = new JFrame("Snake");
    		Game game = new Game();
    		frame.add(game);
    		frame.setSize(406, 430);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.run();
    	}
    public void run() {
    		while (true) {
    		Move();
    		Draw(globalGraphics);
    		repaint();
     
    			try
    			{
    				Thread.currentThread();
    				Thread.sleep(100);				
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}	
    		}
     
    	}
    public void Move()
    	{
    		Point head = snake.peekFirst();
    		Point newPoint = head;
    		switch (direction) {
    		case Direction.NORTH:
    			newPoint = new Point(head.x, head.y - 1);
    			break;
    		case Direction.SOUTH:
    			newPoint = new Point(head.x, head.y + 1);
    			break;
    		case Direction.WEST:
    			newPoint = new Point(head.x - 1, head.y);
    			break;
    		case Direction.EAST:
    			newPoint = new Point(head.x + 1, head.y);
    			break;
    		}
     
    		snake.remove(snake.peekLast());
     
    		if (newPoint.equals(fruit))
    		{
    			score+=10;
     
    			Point addPoint = (Point) newPoint.clone();
     
    			switch (direction) {
    			case Direction.NORTH:
    				newPoint = new Point(head.x, head.y - 1);
    				break;
    			case Direction.SOUTH:
    				newPoint = new Point(head.x, head.y + 1);
    				break;
    			case Direction.WEST:
    				newPoint = new Point(head.x - 1, head.y);
    				break;
    			case Direction.EAST:
    				newPoint = new Point(head.x + 1, head.y);
    				break;
    			}
     
    			snake.push(addPoint);
    			PlaceFruit();
     
    		}
    		else if (newPoint.x < 0 || newPoint.x > (GRID_WIDTH - 1))
    		{
     
    			GenerateDefaultSnake();
    			return;
    		}
    		else if (newPoint.y < 0 || newPoint.y > (GRID_HEIGHT - 1))
    		{
     
    			GenerateDefaultSnake();
    			return;
    		}
    		else if (snake.contains(newPoint))
    		{
     
     
    			GenerateDefaultSnake();
    			return;
    		}
     
     
    		snake.push(newPoint);
    	}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java SnakeGame

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    Something in line 85 of Game's move() method is null. 'move() is improperly named 'Move()'. Add print statements or other indicators to determine which item is null and then fix it. (We can't tell which line is line 85 or which item is null even if we knew which line was line 85 from the code you've posted, so there's not much more we can do.)

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    So line 85 is:
    Point head = snake.peekFirst();

    The declaration of the variable:
    private LinkedList<Point> snake;

    The initialization of the snake variable in paint method + snake generator:
    public void paint(Graphics g)
    	{
    		this.setPreferredSize(new Dimension(640, 480));
    		snake = new LinkedList<Point>();
    		GenerateDefaultSnake();
    		PlaceFruit();
    		globalGraphics = g.create();
    		this.addKeyListener(this);
    		if (runThread == null)
    		{
    			runThread = new Thread(this);
    			runThread.start();
    		}
    	}
     
    	public void GenerateDefaultSnake()
    	{
    		score = 0;
    		snake.clear();
     
    		snake.add(new Point(0,2));
    		snake.add(new Point(0,1));
    		snake.add(new Point(0,0));
    		direction = Direction.NO_DIRECTION;
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    initialization of the snake variable in paint method
    The paint() method is NOT a good place to initialize variables. paint() can be called for various reasons at different ttimes.
    If the variable should be initialized one time, it better to do it in the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Quote Originally Posted by Norm View Post
    The paint() method is NOT a good place to initialize variables. paint() can be called for various reasons at different ttimes.
    If the variable should be initialized one time, it better to do it in the constructor.
    Same thing hapens

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    Please copy the full text of the error message and paste it here.
    Also post the new code that causes the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    Also post the new code that causes the error.
    It's the same error message as above.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    It can't be if you changed the program.
    If you didn't change the program, nothing will change and it will be the same.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    public void paint(Graphics g)
    	{
    		this.setPreferredSize(new Dimension(640, 480));
    		GenerateDefaultSnake();
    		PlaceFruit();
    		globalGraphics = g.create();
    		this.addKeyListener(this);
    		if (runThread == null)
    		{
    			runThread = new Thread(this);
    			runThread.start();
    		}
    	}
     
    	public void GenerateDefaultSnake()
    	{	
    		snake = new LinkedList<Point>();
    		score = 0;
    		snake.clear();
     
    		snake.add(new Point(0,2));
    		snake.add(new Point(0,1));
    		snake.add(new Point(0,0));
    		direction = Direction.NO_DIRECTION;
    	}

    Exception in thread "main" java.lang.NullPointerException
    	at Game.move(Game.java:85)
    	at Game.run(Game.java:244)
    	at Game.main(Game.java:38)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    The code STILL has the call to the method: GenerateDefaultSnake() INSIDE of the paint() method.
    Code that is used one time to initialize variables should NOT be in the paint() method.
    Move the call to that method out of paint().
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Ok so now the problem is with the draw method:
    public void draw(Graphics g)
    	{
    		g.clearRect(0, 0, BOX_WIDTH * GRID_WIDTH + 10, BOX_HEIGHT * GRID_HEIGHT + 20);
     
    		BufferedImage buffer = new BufferedImage(BOX_WIDTH * GRID_WIDTH + 10, BOX_HEIGHT * GRID_HEIGHT + 20, BufferedImage.TYPE_INT_ARGB);
    		Graphics bufferGraphics = buffer.getGraphics();
     
    		drawFruit(bufferGraphics);
    		drawGrid(bufferGraphics);
    		drawSnake(bufferGraphics);
    		drawScore(bufferGraphics);
     
     
    		g.drawImage(buffer, 0, 0, BOX_WIDTH * GRID_WIDTH + 10,  BOX_HEIGHT * GRID_HEIGHT + 20, this);
    	}

    Exception in thread "main" java.lang.NullPointerException
    	at Game.draw(Game.java:68)
    	at Game.run(Game.java:245)
    	at Game.main(Game.java:38)

    The line in cause is:
    g.clearRect(0, 0, BOX_WIDTH * GRID_WIDTH + 10, BOX_HEIGHT * GRID_HEIGHT + 20);

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    Does g have a null value? How is the draw() method called? Where is the args passed to draw() given a value?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    public void run() {
    		while (true) {
    		GenerateDefaultSnake();
    		move();
    		draw(globalGraphics);
    		repaint();
     
    			try
    			{
    				Thread.currentThread();
    				Thread.sleep(100);				
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}	
    		}
     
    	}

    public void paint(Graphics g)
    	{
    		this.setPreferredSize(new Dimension(640, 480));
    		placeFruit();
    		globalGraphics = g.create();
    		this.addKeyListener(this);
    		if (runThread == null)
    		{
    			runThread = new Thread(this);
    			runThread.start();
    		}
    	}
    I did not initialize g.
    I only use repaint().
    Last edited by RigaCrypto; April 21st, 2014 at 11:39 AM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    The draw() method should be called from the paint() method because that is where the Graphics object is.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    I have a globalGraphics variable.

    Declaration:
    private Graphics globalGraphics;
    Initialization(see in paint()):
    globalGraphics = g.create();
    draw(globalGraphics);

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Yeah, i called the method before the initialization of global graphics so that gave me massiv eror. Now all is good. No erors, but still apples spawn every where and snake dose not move...

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    snake dose not move
    Is the snake's x,y location being changed?

    Make a small, complete program that compiles, executes and shows the problem. what is posted can't be used for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Quote Originally Posted by Norm View Post
    Is the snake's x,y location being changed?

    Make a small, complete program that compiles, executes and shows the problem. what is posted can't be used for testing.
    the program runs, the snake is there but it dose not move when i pres key, and apples are spawning everywhere

    Here is move method:
    public void move()
    	{
    		if (direction == Direction.NO_DIRECTION)
    			return;
     
    		Point head = snake.peekFirst();
    		Point newPoint = head;
    		switch (direction) {
    		case Direction.NORTH:
    			newPoint = new Point(head.x, head.y - 1);
    			break;
    		case Direction.SOUTH:
    			newPoint = new Point(head.x, head.y + 1);
    			break;
    		case Direction.WEST:
    			newPoint = new Point(head.x - 1, head.y);
    			break;
    		case Direction.EAST:
    			newPoint = new Point(head.x + 1, head.y);
    			break;			
    		}
     
    			snake.remove(snake.peekLast());
     
    		if(newPoint.equals(fruit))
    		{	
    			score+=10;
     
    			Point addPoint = (Point) newPoint.clone();
     
    			switch (direction){
    			case Direction.NORTH:
    				newPoint = new Point(head.x, head.y - 1);
    				break;
    			case Direction.SOUTH:
    				newPoint = new Point(head.x, head.y + 1);
    				break;
    			case Direction.WEST:
    				newPoint = new Point(head.x - 1, head.y);
    				break;
    			case Direction.EAST:
    				newPoint = new Point(head.x + 1, head.y);
    				break;			
    			}
    				snake.push(addPoint);
    				placeFruit();							
     
    		}
    			else if(newPoint.x < 0 || newPoint.x > (GRID_WIDTH - 1))
    			{						
    				return;						
    			}
    			else if(newPoint.y < 0 || newPoint.y > (GRID_HEIGHT - 1))
    			{				
    				return;					
    			}
    			else if(snake.contains(newPoint))
    			{				
    				return;		
    			}
    			else if(snake.size() == (GRID_WIDTH * GRID_HEIGHT))
    			{				
    				return;
    			}						
     
    		snake.push(newPoint);
     
    	}

    And placeFruit method:
    public void placeFruit()
    	{
    		Random rand = new Random();
    		int randomX = rand.nextInt(GRID_WIDTH);
    		int randomY = rand.nextInt(GRID_HEIGHT);
    		Point randomPoint = new Point(randomX, randomY);
    		while (snake.contains(randomPoint))
    		{
    			randomX = rand.nextInt(GRID_WIDTH);
    			randomY = rand.nextInt(GRID_HEIGHT);
    			randomPoint = new Point(randomX, randomY);
    		}
    		fruit = randomPoint;
    	}

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    Can you Make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    If you mean a runnable jar, here it is:SnakeGame.jar

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    I need a source for debugging.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Any luck?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java SnakeGame

    I need a source file for debugging. I do NOT download from other sites.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java SnakeGame

    Direction.txt
    Game.txt

Page 1 of 2 12 LastLast