gpt4 book ai didi

java - JavaFX 应用程序中无法解释的 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 11:43:50 26 4
gpt4 key购买 nike

背景信息

为计算机科学创建 JavaFX 应用程序

我在做什么

我调用一个类并打开在该类中创建的场景

发生了什么

我得到一个NullPointerException堆栈跟踪;见下文。

我尝试过的

我曾经打印语句来查看它发生在哪里。它不会发生在创建类的地方,而是在类本身中,它像构造函数中的前两行代码一样存在。

错误

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.input.PickResultChooser.processOffer(PickResultChooser.java:185)
at com.sun.javafx.scene.input.PickResultChooser.offer(PickResultChooser.java:143)
at javafx.scene.Node.impl_intersects(Node.java:4945)
at javafx.scene.layout.Region.impl_pickNodeLocal(Region.java:2942)
at javafx.scene.Node.impl_pickNode(Node.java:4914)
at javafx.scene.Scene$MouseHandler.pickNode(Scene.java:3899)
at javafx.scene.Scene$MouseHandler.access$1600(Scene.java:3485)
at javafx.scene.Scene.pick(Scene.java:1942)
at javafx.scene.Scene.access$6700(Scene.java:159)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3711)
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:381)
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$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)

在应用程序崩溃之前会重复此堆栈跟踪。

我的代码

public class StudentClassView {

Scene back, scene;
Stage window;

public StudentClassView(Classroom classRoom, String student, Scene back, Stage window) {

// Prepare Interface
this.back = back;
this.window = window;

// Generate content
String title_s;
if (classRoom.getCourseAverage() != 0) {
title_s = classRoom.getCode() + ", " + classRoom.getCourseAverage() + "%";
} else {
title_s = classRoom.getCode();
}
Label title = new Label(title_s);

ArrayList<Label> assignments = new ArrayList<Label>();
ArrayList<Label> scores = new ArrayList<Label>();
if (classRoom.getAssignments() != null) {
for (Assignment a : classRoom.getAssignments()) {
assignments.add(new Label(a.getName()));
String scoreEntry = a.getScore(student) + " (worth " + a.getWeight() + "%)";
scores.add(new Label(scoreEntry));
}
}

Button exit_b = new Button("Back");

// Design layout
ScrollPane layout = new ScrollPane();
VBox content = new VBox();
GridPane grid = new GridPane();
int ind = 0;
for (Label a : assignments) {
grid.add(a, 0, ind);
ind += 1;
}
ind = 0;
for (Label s : scores) {
grid.add(s, 1, ind);
ind += 1;
}
content.getChildren().addAll(title, grid, exit_b);
layout.setContent(layout);
// Create Interface
scene = new Scene(layout, 800, 600);

// Handle events
exit_b.setOnAction(e->{
window.setScene(back);
});

}

public Scene getScene() {
return scene;
}

}

最佳答案

您不应使用 ScrollPane (或其任何祖先),因为它是 content 节点。改变

layout.setContent(layout);

类似于

layout.setContent(content);

否则,当在第一个布局期间创建 ScrollPane 的外观时,会在场景层次结构中创建一个圆圈,从而破坏 JavaFX。

关于java - JavaFX 应用程序中无法解释的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48353055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com