我目前正在嘗試在java中創建一個tic-tac-toe遊戲作爲大學項目。到目前爲止,我所做的只是創建了一個gameBoard方法的框架代碼。我已經遇到錯誤說:什麼是NullPointerException?
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
我也不確定如何完成項目。我的基本想法是使用gameBoard方法每次打印板。我試圖將變量設置爲全局變量,以便在玩家移動時我可以改變它們。我該如何做到這一點,我如何解決我收到的錯誤?任何幫助將不勝感激,這裏是我當前的代碼:
import javax.swing.JOptionPane;
public class Assignment5
{
public static String top1 = " ";
public static String top2 = " ";
public static String top3 = " ";
public static String mid1 = " ";
public static String mid2 = " ";
public static String mid3 = " ";
public static String bot1 = " ";
public static String bot2 = " ";
public static String bot3 = " ";
//Generate the game board
public static void gameBoard()
{
System.out.println("+-----------+");
System.out.println("| " + top1 + " | " + top2 + " | " + top3 + " |");
System.out.println("+-----------+");
System.out.println("| " + mid1 + " | " + mid2 + " | " + mid3 + " |");
System.out.println("+-----------+");
System.out.println("| " + bot1 + " | " + bot2 + " | " + bot3 + " |");
System.out.println("+-----------+");
}
public void main(String args[])
{
gameBoard();
}
}
['java.lang.NullPointerException'](http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html) – 2013-05-02 04:22:15
我不認爲這是唯一的一塊你正在使用的代碼。首先,public static void main(String [] args)其次,你試圖訪問非靜態方法中靜態的gameBoard。 – asifsid88 2013-05-02 04:26:34
@ asifsid88看起來像OP從'main'中移除'static'關鍵字,以便乾淨地調用'gameboard'方法,但卻變成了這個錯誤。 – 2013-05-02 04:28:27