我目前正在尝试使用Java作为大学项目来创建井字游戏。到目前为止,我所做的只是创建了gameBoard方法的框架代码。我已经遇到一个错误,说:
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中的相应类: java.lang.NullPointerException
java.lang.NullPointerException
关于您的代码,我建议您使用像Eclipse这样的IDE,这样您可以在键入时指出编译错误,如下所示
public void main(String args[])
它应该是
public static void main(String args[])