我想通過Java圖形程序製作四個井字棋板的簡單圖像。 我的程序編譯並可以運行,但不會生成四個板的圖像。我得到了我的相互作用窗格此錯誤(我使用DrJava代碼):NullPointerException錯誤:如何解決這個問題?
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中進行調試而無法找到錯誤。有人可以幫忙嗎? :)
哪一行是第38行? – Catherine 2013-04-22 03:18:31