我正在尝试通过 Java 图形程序制作四个井字游戏板的简单图像。我的程序编译可以运行,但不产生四个板的图像。我在交互窗格中收到此错误(我使用 DrJava 进行编码):
Welcome to DrJava. Working directory is C:\Users\Daniel\Desktop\CS Programs
> run TicTacToeBoards
java.lang.NullPointerException
at TicTacToeBoards.drawTicTacToeBoard(TicTacToeBoards.java:38)
at TicTacToeBoards.<init>(TicTacToeBoards.java:18)
at TicTacToeBoards.main(TicTacToeBoards.java:52)
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)
这是我试图运行的程序:
import java.awt.Color;
public class TicTacToeBoards {
private NsccWindow win;
public TicTacToeBoards() {
NsccWindow win;
win = new NsccWindow(10, 10, 330, 300);
win.setTitle("Tic-Tac-Toe Boards");
drawTicTacToeBoard(40, 40);
drawTicTacToeBoard(40, 170);
drawTicTacToeBoard(170, 40);
drawTicTacToeBoard(170, 170);
}
public void drawTicTacToeBoard(int x, int y) {
NsccLine vertLineL;
NsccLine vertLineR;
NsccLine horizLineU;
NsccLine horizLineD;
vertLineL = new NsccLine((x + 30), y, (x + 30), (y + 90));
vertLineR = new NsccLine((x + 60), y, (x + 60), (y + 90));
horizLineU = new NsccLine(x, (y + 30), (x + 90), (y + 30));
horizLineD = new NsccLine(x, (y + 60), (x + 90), (y + 30));
win.add(vertLineL);
win.add(vertLineR);
win.add(horizLineU);
win.add(horizLineD);
win.repaint();
}
public static void main(String[] args) {
TicTacToeBoards test = new
TicTacToeBoards();
}
}
我对 Java 编程相当陌生,我一直坚持试图通过在 DrJava 中调试来查找错误,但无济于事。有人可以帮忙吗?:)