gpt4 book ai didi

JavaFX RandomNameChooser 项目 - NullPointerException 错误(可能是一个简单的修复,但我仍然很困惑)

转载 作者:行者123 更新时间:2023-12-01 13:16:47 25 4
gpt4 key购买 nike

我正在制作一个程序,它获取文本输入并选择随机行。

但是,我的代码有问题。当通过与其关联的 GUI 按钮调用方法“randomizeButton”时,我的 ArrayList 出现 NullPointerException。

代码:

RandomNameChooser.java

package randomnamechooser;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
*
* @author Mally
*/
public class RandomNameChooser extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

Scene scene = new Scene(root);
stage.setTitle("Random Name Chooser");
stage.setScene(scene);
stage.show();
}

/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="200.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="randomnamechooser.FXMLDocumentController">
<children>
<Button layoutX="31.0" layoutY="46.0" mnemonicParsing="false" onAction="#loadButton" text="Load" />
<Button layoutX="98.0" layoutY="46.0" mnemonicParsing="false" onAction="#randomizeButton" text="Randomize" textAlignment="JUSTIFY" />
<Label layoutX="74.0" layoutY="92.0" text="Winner is:" />
<Label id="label" layoutX="84.0" layoutY="117.0" text="" />
</children>
</AnchorPane>

FXMLDocumentController.java

package randomnamechooser;

import java.awt.Label;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.stage.FileChooser;

/**
*
* @author Mally
*/
public class FXMLDocumentController implements Initializable {

List<String> list = new ArrayList<String>();

@FXML
private Label label;

@FXML
private void loadButton() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select File");
//Set extention filter
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Text Files", "*.txt"));
//Show open file dialog for one file
File file = fileChooser.showOpenDialog(null);

try {
//Read the file input
BufferedReader input = new BufferedReader(new FileReader(file.getPath()));
try {
String line = null;
//Using the readLine command and updating the variable each line
while ((line = input.readLine()) != null) {
list.add(line);
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}

//For loop that prints out each element of the List
for (String i : list) {
System.out.println(i);
}
}

@FXML
private void randomizeButton() {
Random rand = new Random();

int random = rand.nextInt(list.size() - 1);

label.setText(list.get(random));
}

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}

}

最佳答案

您的标签未初始化。您在 FXML 中使用了 id="label" 而不是 fx:id="label"

关于JavaFX RandomNameChooser 项目 - NullPointerException 错误(可能是一个简单的修复,但我仍然很困惑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22410269/

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