Note that there are some explanatory texts on larger screens.

plurals
  1. POSnake game, start
    primarykey
    data
    text
    <h2><strong>I have fixed the errors but I am still unable to get the game to start</strong></h2> <p>This is a snake game and I can not get it to start. When I try to run it on Eclipse it keeps giving me </p> <pre><code>Exception in thread "main" java.lang.NullPointerException at Snake.createSnake(Snake.java:110) at Snake.&lt;init&gt;(Snake.java:95) at Driver.main(Driver.java:6) </code></pre> <p>I just need someone to please explain and help me fix this. I have asked numerous friends and no one that I know can help me fix it. </p> <pre><code>public class Driver { public static void main(String[] args) { Snake game = new Snake(); } } </code></pre> <p>Below is this Snake class.</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; @SuppressWarnings("serial") class Snake extends JFrame implements KeyListener, Runnable{ JPanel gamePanel, scorePanel; JButton extraFood; JTextArea textAreaScore; int x = 500; int y = 250; int minSnake = 3; int directionx = 1; int directiony = 0; int speed = 50; int difference = 0; int oldx = 0; int oldy = 0; int score = 0; boolean food = false; boolean runLeft = false; boolean runRight = true; boolean runUp = true; boolean runDown = true; boolean bonusFlag = true; Random ran = new Random(); JButton[] bc = new JButton[200]; int[] bx = new int[300]; int[] by = new int[300]; Thread myThread; Point[] bp = new Point[300]; Point bonusp = new Point(); //initializing values public void Values() { minSnake = 3; directionx = 10; directiony = 0; difference = 0; score = 0; food = false; runLeft = false; runRight = true; runUp = true; runDown = true; bonusFlag = true; } //sets layout of game public Snake() { getContentPane().setBackground(Color.GRAY); getContentPane().setLayout(null); gamePanel = new JPanel(); gamePanel.setBackground(Color.LIGHT_GRAY); gamePanel.setBounds(6, 6, 438, 223); getContentPane().add(gamePanel); gamePanel.setLayout(new GridLayout(1, 0, 0, 0)); scorePanel = new JPanel(); scorePanel.setBackground(Color.GRAY); scorePanel.setBounds(6, 241, 438, 31); getContentPane().add(scorePanel); scorePanel.setLayout(null); textAreaScore = new JTextArea("Your score is:" + score); textAreaScore.setBackground(Color.LIGHT_GRAY); textAreaScore.setBounds(0, 0, 303, 31); scorePanel.add(textAreaScore); //Exit Game button JButton btnExitGame = new JButton("Exit Game"); btnExitGame.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnExitGame.setBounds(315, 0, 117, 29); scorePanel.add(btnExitGame); btnExitGame.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); setVisible(true); createSnake(); extraFood = new JButton(); extraFood.setEnabled(false); addKeyListener(this); //This starts the game myThread = new Thread(this); myThread.start(); } //Creates snake public void createSnake() { for (int i = 0; i &lt; 3; i++){ bc[i] = new JButton("b" + i); bc[i].setEnabled(false); gamePanel.add(bc[i]); bc[i].setBounds(bx[0], by[0], 10, 10); bx[i + 1] = bx[i] - 10; by[i + 1] = by[i]; } } @SuppressWarnings("deprecation") void reset(){ Values(); gamePanel.removeAll(); myThread.stop(); createSnake(); textAreaScore.setText("Your score is: " + score); myThread = new Thread(this); myThread.start(); } //As snake eats food, it grows void snakeGrow(){ bc[minSnake] = new JButton(); bc[minSnake].setEnabled(false); gamePanel.add(bc[minSnake]); int a = 10 + (10 * ran.nextInt(48)); int b = 10 + (10 * ran.nextInt(23)); bx[minSnake] = a; by[minSnake] = b; bc[minSnake].setBounds(a, b, 10, 10); minSnake++; } //Snake moving logic void moveForward() { for (int i = 0; i &lt; minSnake; i++) { bp[i] = bc[i].getLocation(); } bx[0] += directionx; by[0] += directiony; bc[0].setBounds(bx[0], by[0], 10, 10); for (int i = 1; i &lt; minSnake; i++) { bc[i].setLocation(bp[i - 1]); } if (bx[0] == x) { bx[0] = 10; } else if (bx[0] == 0) { bx[0] = x - 10; } else if (by[0] == y) { by[0] = 10; } else if (by[0] == 0) { by[0] = y - 10; } if (bx[0] == bx[minSnake - 1] &amp;&amp; by[0] == by[minSnake - 1]) { food = false; score += 1; textAreaScore.setText("Your score is: " + score); if (score % 50 == 0 &amp;&amp; bonusFlag == true) { gamePanel.add(extraFood); extraFood.setBounds((10 * ran.nextInt(50)), (10 * ran.nextInt(25)), 15, 15); bonusp = extraFood.getLocation(); bonusFlag = false; } } if (bonusFlag == false) { if (bonusp.x &lt;= bx[0] &amp;&amp; bonusp.y &lt;= by[0] &amp;&amp; bonusp.x + 10 &gt;= bx[0] &amp;&amp; bonusp.y + 10 &gt;= by[0]) { gamePanel.remove(extraFood); score += 100; textAreaScore.setText("Your score is: " + score); bonusFlag = true; } } if (food == false) { snakeGrow(); food = true; } else { bc[minSnake - 1].setBounds(bx[minSnake - 1], by[minSnake - 1], 10, 10); } gamePanel.repaint(); extracted(); } @SuppressWarnings("deprecation") private void extracted() { show(); } public void keyPressed(KeyEvent e) { //Move to the left when player presses the left arrow key if (runLeft == true &amp;&amp; e.getKeyCode() == 37) { directionx = -10; //Moves to the left by 10 pixels directiony = 0; runRight = false; runUp = true; runDown = true; } //Move up when player presses the up arrow key if (runUp == true &amp;&amp; e.getKeyCode() == 38) { directionx = 0; directiony = -10; //Moves up by 10 pixels runDown = false; runRight = true; runLeft = true; } //Move to the right when the player presses the right arrow key if (runRight == true &amp;&amp; e.getKeyCode() == 39) { directionx = +10; //Moves right by 10 pixels directiony = 0; runLeft = false; runUp = true; runDown = true; } //Move down when the player presses the down arrow key if (runDown == true &amp;&amp; e.getKeyCode() == 40) { directionx = 0; directiony = +10; //Moves down by 10 pixels runUp = false; runRight = true; runLeft = true; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void run() { for (;;) { //Moves the snake forward moveForward(); try { Thread.sleep(speed); } catch (InterruptedException ie) { } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload