我正在为学校项目制作Sudoku,需要打开将列出高分的新窗口,但是每次我单击MenuItem时,控制台都会抛出错误,我不知道该怎么做。 / p>
我已经试图在互联网上寻找东西,但是我尝试的所有事情都导致了相同的错误
package controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import model.GameBoard;
public class Controller implements Initializable {
@FXML
Button button_one;
@FXML
Button button_two;
@FXML
Button button_three;
@FXML
Button button_four;
@FXML
Button button_five;
@FXML
Button button_six;
@FXML
Button button_seven;
@FXML
Button button_eight;
@FXML
Button button_nine;
@FXML
Canvas canvas;
GameBoard gameboard;
int psr; // ausgewählte reihe
int psc; // ausgewählte spalte
@Override
public void initialize(URL location, ResourceBundle resources) {
gameboard = new GameBoard();
psr = 0;
psc = 0;
GraphicsContext context = canvas.getGraphicsContext2D();
try {
drawOnCanvas(context);
} catch (IOException e) {
e.printStackTrace();
}
}
public void drawOnCanvas(GraphicsContext context) throws IOException {
context.clearRect(0, 0, 450, 450);
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int posy = row * 50 + 2;
int posx = col * 50 + 2;
int width = 46;
context.setFill(Color.LIGHTGRAY);
context.fillRoundRect(posx, posy, width, width,10,10);
}
}
context.setStroke(Color.RED);
context.setLineWidth(2);
context.strokeRoundRect(psc * 50 + 2, psr * 50 + 2, 46, 46, 10, 10);
int[][] initial = gameboard.getInitial();
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int posy = row * 50 + 30;
int posx = col * 50 + 20;
context.setFill(Color.BLACK);
context.setFont(new Font(20));
if (initial[row][col] != 0) {
context.fillText(initial[row][col] + "", posx, posy);
}
}
}
int[][] player = gameboard.getPlayer();
for(int row = 0; row < 9;row++) {
for(int col = 0; col < 9; col++) {
int posy = row * 50 + 30;
int posx = col * 50 + 20;
context.setFill(Color.RED);
context.setFont(new Font(20));
if(player[row][col] != 0) {
context.fillText(player[row][col] + "", posx, posy);
}
}
}
//Die vier Linien für die 3x3 Trennung
context.setStroke(Color.BLACK);
context.strokeLine(450, 150, 0, 150);
context.strokeLine(450, 300, 0, 300);
context.strokeLine(150, 0, 150, 450);
context.strokeLine(300, 0, 300, 450);
context.setLineWidth(3);
if(gameboard.checkSuccess()) {
context.clearRect(0, 0, 450, 450);
context.setFill(Color.GREEN);
context.setFont(new Font(36));
context.fillText("SUPER!", 150, 200);
newWindow();
}
}
public void canvasMouseClick() throws IOException{
canvas.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int mousex = (int) event.getX();
int mousey = (int) event.getY();
psr = (int) (mousey / 50);
psc = (int) (mousex / 50);
// System.out.println(psr + "," + psc);
try {
drawOnCanvas(canvas.getGraphicsContext2D());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void button1pressed() throws IOException{
gameboard.modifyPlayer(1, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button2pressed() throws IOException{
gameboard.modifyPlayer(2, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button3pressed() throws IOException{
gameboard.modifyPlayer(3, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button4pressed() throws IOException{
gameboard.modifyPlayer(4, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button5pressed() throws IOException{
gameboard.modifyPlayer(5, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button6pressed() throws IOException{
gameboard.modifyPlayer(6, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button7pressed() throws IOException{
gameboard.modifyPlayer(7, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button8pressed() throws IOException {
gameboard.modifyPlayer(8, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void button9pressed() throws IOException {
gameboard.modifyPlayer(9, psr, psc);
drawOnCanvas(canvas.getGraphicsContext2D());
}
public void neuesSpiel() throws IOException {
gameboard = new GameBoard();
psr = 0;
psc = 0;
GraphicsContext context = canvas.getGraphicsContext2D();
drawOnCanvas(context);
}
public void newWindow() throws IOException {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/controller/highscore.fxml"));
VBox root1 = (VBox) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
StackTrace:
Mai 21, 2019 5:31:30 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.181
Mai 21, 2019 5:31:32 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.181
javafx.fxml.LoadException:
/C:/Users/Luka%20Lajic/eclipse-workspace/Sudoku/bin/controller/highscore.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at controller.Controller.newWindow(Controller.java:223)
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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at controller.Controller.initialize(Controller.java:58)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 56 more
答案 0 :(得分:0)
Caused by: java.lang.NullPointerException
at controller.Controller.initialize(Controller.java:58)
建议错误出现在Controller类的58行上。这似乎是这一行:
GraphicsContext context = canvas.getGraphicsContext2D();
,如果是这样,如果canvas
为空,则会发生这种情况。
您很可能可以通过确保正确实例化canvas
来解决该错误,但是如果您需要进一步的帮助,则可以阅读更多here。