我正在制作一个获取文本输入并选择随机行的程序。
但是,我的代码出了问题。当方法“randomizeButton”通过与之关联的GUI按钮调用时,我得到了一个带有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
}
}
答案 0 :(得分:1)
您的标签未初始化。您在FXML中使用id="label"
代替fx:id="label"
。