2012-04-14 4 views
-1

現在、Hangman GUIでHW割り当てとして作業していますが、すべてのコードが完了しています(私は思うが)、nullpointerexceptionを取得しています。それが起源である私の人生は私をかなり怒らせる。私は、私が間違いを犯した場所と、それを訂正するために何をする必要があるのか​​を理解するのに役立つ2番目の目が必要です。前もって感謝します!Hangman GUIがnullポインタ例外を受け取りましたが、どこにも見つかりません

NPEがで発生している:

alphabet = "abcdefghijklmnopqrxtuvwxyz"; 
      numLetters = 26; 
      for (int count = 0; count < numLetters; count++) { 
       letterChoice[count] = new JButton(Character.toString(alphabet 
         .charAt(count))); 
       letterChoice[count].addActionListener(new CharacterListener(
         alphabet.charAt(count))); 
       letterChoice[count].setMnemonic(65 + count); 
       add(letterChoice[count]); 
      } 

そして、ここですべての私のコードです。

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 

import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.ImageIcon; 
import javax.swing.SwingConstants; 

public class HangmanPanel extends JPanel { 
    private JLabel imageLabel, numberLetters, gameOver, youWin; 
    private JLabel[] spaces; 
    private ImageIcon[] images; 
    private JButton exitProgram, newGame, nextImage; 
    private JButton[] letterChoice; 
    private int imageNumber, letterNumber, numLetters, guesses; 
    private WordList wordRand; 
    private String word, alphabet; 

    public HangmanPanel() { 

     newGame = new JButton("New Game"); 
     newGame.setEnabled(true); 
     newGame.setToolTipText("Press to restart game."); 
     newGame.addActionListener(new NewGame()); 

     exitProgram = new JButton("Exit"); 
     exitProgram.setEnabled(true); 
     exitProgram.setToolTipText("Press to close the program."); 
     exitProgram.addActionListener(new ExitGame()); 

     wordRand = new WordList(); 
     word = wordRand.getWord(); 

     images = new ImageIcon[8]; 
     // Populating the array 
     { 
      images[0] = new ImageIcon("hangman0.png"); 
      images[1] = new ImageIcon("hangman1.png"); 
      images[2] = new ImageIcon("hangman2.png"); 
      images[3] = new ImageIcon("hangman3.png"); 
      images[4] = new ImageIcon("hangman4.png"); 
      images[5] = new ImageIcon("hangman5.png"); 
      images[6] = new ImageIcon("hangman6.png"); 
      images[7] = new ImageIcon("hangman7.png"); 
     } 

     setBackground(Color.white); 
     imageLabel = new JLabel(images[imageNumber]); 
     imageNumber++; 
     add(imageLabel); 

     alphabet = "abcdefghijklmnopqrxtuvwxyz"; 
     numLetters = 26; 
     for (int count = 0; count < numLetters; count++) { 
      letterChoice[count] = new JButton(Character.toString(alphabet 
        .charAt(count))); 
      letterChoice[count].addActionListener(new CharacterListener(
        alphabet.charAt(count))); 
      letterChoice[count].setMnemonic(65 + count); 
      add(letterChoice[count]); 
     } 

     spaces = new JLabel[word.length()]; 
     while (letterNumber < spaces.length) { 
      numberLetters = new JLabel("___"); 
      add(numberLetters); 
      letterNumber++; 
     } 

     add(nextImage); 
     add(newGame); 
     add(exitProgram); 

    } 

    private class NewGame implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      imageLabel.setIcon(images[0]); 
      imageNumber = 0; 
      imageNumber++; 
      imageLabel.repaint(); 
     } 
    } 

    private class ExitGame implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    } 

    private class CharacterListener implements ActionListener { 
     public CharacterListener(char charAt) { 

     } 

     public void actionPerformed(ActionEvent e) { 
      while (guesses < images.length) { 
       int count = 0; 
       while (count < word.charAt(count)) { 
        if (letterChoice[count].equals(word.charAt(count))) { 
         spaces[count] = new JLabel("" + letterChoice[count] 
           + ""); 
         count++; 
        } else 
         imageLabel.setIcon(images[imageNumber]); 
        imageNumber++; 
        imageLabel.repaint(); 
        guesses++; 

       } 

       if (guesses == 7) { 
        gameOver = new JLabel(
          "You lose! Press New Game to try again!"); 
        add(gameOver); 
       } else 
        youWin = new JLabel(
          "You win! Press New Game to play again or press Exit to remain Victorious!"); 
       add(youWin); 
      } 

     } 

    } 

} 
+1

ご自身で足踏みをしてください。 NPEはどこから由来していますか?デバッグ中に何を学びましたか? –

+0

私はそれがここに由来していると信じています。 alphabet = "abcdefghijklmnopqrxtuvwxyz"; numLetters = 26; for(int count = 0; count Structures

+0

質問に追加してください。 –

答えて

3

NullPointerExceptionは、問題の原因となっている行を示します。あなたは例外を投稿してください。私たちはあなたが特定の行でなぜそれが起こっているのかを理解するのを手伝ってくれます。

この情報がなければ、いくつかの可能なエラーがある..

第一の可能なエラー... あなたはletterChoiceの配列を作成していない - あなたは、アルファベットの前にどこかに次のことを逃しているすなわち。 ..

letterChoice = new JButton[26]; 

第二の可能なエラー... あなたはnextImage作成していない - あなたは、以下の...

が欠落している、すなわち、あなたは word.charAt(count)のようなコマンドを実行している場合、あなたはカウントが最初の単語の長さ未満であることを確認してください:

public void actionPerformed(ActionEvent e) { 
     while (guesses < images.length) { 
      int count = 0; 
      while (count < word.charAt(count)) { // probably here 
       if (letterChoice[count].equals(word.charAt(count))) { // maybe here too 
        spaces[count] = new JLabel("" + letterChoice[count] 
          + ""); 
        count++; 
       } else 
        imageLabel.setIcon(images[imageNumber]); 
       imageNumber++; 
       imageLabel.repaint(); 
       guesses++; 

      } 

理由

newImage = new JButton("Blah"); 

第三の可能なエラー...。ラインへ...

while (count < word.length && count < word.charAt(count)) { 

第四の可能なエラーを変更...文字の あなたのリストは、その中に「s」を持っていませんが、むしろそれは2「S」の文字を持っています。

+0

スレッド "main"の例外java.util.NullPointerException \t at HangmanPanel。 (HangmanPanel.java:59) \t at Hangman.main(Hangman。java:16) NPEの取得は – Structures

+0

です letterChoice [count] =新しいJButton(Character.toString(アルファベット .charAt(count))); 何も作成していませんか? – Structures

+0

アルファベットの前には 'letterChoice = new JButton [26];が必要です - 作成する前に26個のボタンが配列にあると言う必要があります。 – wattostudios

関連する問題