2016-08-05 6 views
0

jframeで動作するテキストベースの冒険をしようとしていますが、プログラムを実行すると97 (first time i append console in the game method)のエラーNullPointerが発生し、修正方法がわかりません。私は比較的新しいjavaになっているので、私はちょうど私が知らない単純なものです。JTextAreaでNullPointerExceptionを修正できません

私のコードはここにあなたのグローバル変数console

package window; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 

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

import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class Gui extends JFrame { 

public JPanel contentPane; 
public static JTextField input; 
public static JButton send; 
public static JTextArea console; 
public static JTextArea invintory; 
public static JTextArea stats; 
static String i = ""; 

/** 
* Launch the application. 
*/ 



/** 
* Create the frame. 
*/ 
public Gui() { 
    //variables 

    int Gold = 20; 
    int Health = 100; 
    int MaxHealth = 100; 


    //variables end 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JTextArea console = new JTextArea(); 
    console.setBounds(10, 11, 281, 214); 
    contentPane.add(console); 

    input = new JTextField(); 
    input.setBounds(10, 236, 281, 20); 
    contentPane.add(input); 
    input.setColumns(10); 

    JTextArea stats = new JTextArea(); 
    stats.setBounds(301, 11, 123, 53); 
    contentPane.add(stats); 

    JTextArea invintory = new JTextArea(); 
    invintory.setBounds(301, 75, 128, 137); 
    contentPane.add(invintory); 

    JButton send = new JButton("Send"); 
    send.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String i = input.getText(); 
      input.setText(""); 
      stats.setText(""); 
      stats.append("Health: " + Health + "/" + MaxHealth + "\n"); 
      stats.append("Gold: " + Gold); 
     } 
    }); 
    send.setBounds(301, 224, 128, 32); 
    contentPane.add(send); 

    stats.append("Health: " + Health + "/" + MaxHealth + "\n"); 
    stats.append("Gold: " + Gold); 

} 

public static void game (JButton send, JTextArea console, JTextArea stats, JTextArea invintory, JTextField input, String i) { 




    //start 
    //START 
    //START 
    //START 
    //START 
    while (true) {   
     console.append("You wake up open feild, with vast amounts of wheet in every direction"); 
     console.append("There is a path going in either direction what do you want to do"); 
     console.append("\t1. Go left."); 
     console.append("\t2. Go right."); 
     while (i == "") { 
     } 
     if (i == "1") { 
      console.append("1"); 
      break; 
     } 
     else if (i == "2") { 
      console.append("2"); 
      break; 
     } 



    } 
    //END 
    //END 
    //END 
    //END 
    } 

public static void main(String[] args) { 
      try { 
       Gui frame = new Gui(); 
       frame.setVisible(true); 
       Gui.game(send, console, stats, invintory, input, i); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

} 
+0

この方法を編集し、エラーログをしてください示して –

答えて

1

であることはあなたがそうここGui方法であなたのグローバルconsoleGui方法で

JTextArea console = new JTextArea(); 

それを初期化し、ヌル

public static JTextArea console; 

です変数は私ではないnitialized、これはGuiメソッドの下にローカル変数コンソールを作成します。あなたがこの方法を初期化する必要がグイ方法でグローバル変数を初期化する

console = new JTextArea(); 

あなたはすべての変数にこの間違いをしたので、あなたのコードは

public Gui() { 
    //variables 

    int Gold = 20; 
    int Health = 100; 
    int MaxHealth = 100; 


    //variables end 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    console = new JTextArea(); 
    console.setBounds(10, 11, 281, 214); 
    contentPane.add(console); 

    input = new JTextField(); 
    input.setBounds(10, 236, 281, 20); 
    contentPane.add(input); 
    input.setColumns(10); 

    stats = new JTextArea(); 
    stats.setBounds(301, 11, 123, 53); 
    contentPane.add(stats); 

    invintory = new JTextArea(); 
    invintory.setBounds(301, 75, 128, 137); 
    contentPane.add(invintory); 

    send = new JButton("Send"); 
    send.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String i = input.getText(); 
      input.setText(""); 
      stats.setText(""); 
      stats.append("Health: " + Health + "/" + MaxHealth + "\n"); 
      stats.append("Gold: " + Gold); 
     } 
    }); 
    send.setBounds(301, 224, 128, 32); 
    contentPane.add(send); 

    stats.append("Health: " + Health + "/" + MaxHealth + "\n"); 
    stats.append("Gold: " + Gold); 

    } 
関連する問題