Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help fixing NullPointerException
    primarykey
    data
    text
    <p>This is hangman. When I run the code I get a couple of null pointer exceptions and have no idea how to fix this. </p> <p>This is what the compile error says exactly:</p> <pre><code> Exception in thread "main" java.lang.NullPointerException at hangmanrevised.createGame(hangmanrevised.java:66) at hangmanrevised.&lt;init&gt;(hangmanrevised.java:35) at hangmanrevised.main(hangmanrevised.java:202) </code></pre> <p>Any help is appreciated.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.Random; import java.lang.reflect.Array; import javax.swing.*; public class hangmanrevised extends JFrame implements ActionListener { private int wrongs; private String right; private StringBuffer guess; private TextField Letter; private String message; private String information; private JButton button; private final int hung = 11; public hangmanrevised() { setSize(600,400); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); ImageIcon background = new ImageIcon("nightsky.png"); TextField Letter = new TextField(); JLabel label = new JLabel("pick a Letter"); Button button = new Button("Enter"); add(label); add(button); add(Letter); button.addActionListener(this); createGame(); } public void createGame(){ wrongs = 0; String word = "school|java|programming|science|computer"; String[] temp; String delimiter = "\\|"; temp = word.split(delimiter); Random randomPicker = new Random(); int randomWord = randomPicker.nextInt(temp.length); right = new String(temp[randomWord]); char positions[] = new char[right.length()]; for (int i = 0; i &lt; right.length(); i++) { positions[i] = '-'; } String s = new String(positions); guess = new StringBuffer(s); Letter.setText(""); message=""; information = ""; repaint(); } public void paint(Graphics g) { super.paint(g); //g.drawImage(background, 0, 156, Color.green, Enter); int baseY = 350; g.setColor(Color.lightGray); g.drawRect(0, baseY,400,baseY); g.fillRect(0, baseY,400,baseY); g.setColor(Color.darkGray); //g.drawRect(250,50,125,50); //g.fillRect(250,50,125,50); if (wrongs &gt; 0){ g.drawLine(125,baseY,125,baseY-100); g.drawLine(123,baseY,123,baseY-100); } if (wrongs &gt; 1){ g.drawLine(110,baseY,125,baseY-15); g.drawLine(108,baseY,122,baseY-15); } if (wrongs &gt; 2){ g.drawLine(140,baseY,125,baseY-15); g.drawLine(138,baseY,122,baseY-15); } if (wrongs &gt; 3){ g.drawLine(125,baseY-100,175,baseY-100); g.drawLine(125,baseY-98,175,baseY-98); } if (wrongs &gt; 4){ g.drawLine(175,baseY-100,175,baseY-75); } if (wrongs &gt; 5){ g.drawOval(170,baseY-75,10,12); } if (wrongs &gt; 6){ g.drawOval(160,baseY-65,15,25); g.fillOval(160,baseY-65,15,25); } if (wrongs &gt; 7){ g.drawLine(150,baseY-65,170,baseY-60); } if (wrongs &gt; 8){ g.drawLine(173,baseY-60,193,baseY-65); } if (wrongs &gt; 9){ g.drawLine(155,baseY-30,170,baseY-45); } if (wrongs &gt; 10){ g.drawLine(173,baseY-45,193,baseY-30); } g.drawString( message, 60, baseY+25 ); g.drawString( information, 25, baseY+45 ); g.drawString( new String (guess), 110, 60); //listener(); } public void actionPerformed(ActionEvent e){ if (e.getSource() == button){ processTurn(); Letter.setText(""); repaint(); } } private void processTurn(){ String s, t; char a; s = Letter.getText(); a = s.charAt(0); if (!Character.isLetter(a)){ message="Letters only"; return; } if (s.length()&gt;1){ message="Enter 1 letter only"; return; } t = new String(guess); if (t.indexOf(s) != -1){ JOptionPane.showMessageDialog(null, "LETTER USED ALREADY"); return; } if (right.indexOf(s) == -1){ message=""; wrongs++; if (wrongs == hung){ JOptionPane.showMessageDialog(null, "YOU LOST!"); } return; } for (int i = 0; i &lt; right.length(); i++){ if (right.charAt(i) == a){ guess.setCharAt(i, a); } } t = new String(guess); if (t.indexOf('-') == -1){ JOptionPane.showMessageDialog(null, "YOU WIN!"); message="You win!"; return; } message=""; repaint(); } public static void main(String[] args) { hangmanrevised f = new hangmanrevised(); f.setVisible(true); } } </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