- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的程序为我可以制作的游戏创建文件,它有一个名为 Update 的方法,该方法在程序启动时运行,读取“mob”文件夹中的所有文件,当没有文件时它运行良好,但是,如果在 mob 文件夹中创建一个包含内容的文件,则程序运行时会抛出 NullPointerException,尽管文件中的上下文不为空。
顺便说一句,这是一个 JavaFX FXML 程序
FXML文档 Controller
package fightwriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {
String content;
@FXML // fx:id="TFname"
private TextField TFname; // Value injected by FXMLLoader
@FXML // fx:id="TFhp"
private TextField TFhp; // Value injected by FXMLLoader
@FXML // fx:id="TFattMin"
private TextField TFattMin; // Value injected by FXMLLoader
@FXML // fx:id="TFattH"
private TextField TFattH; // Value injected by FXMLLoader
@FXML // fx:id="TFdex"
private TextField TFdex; // Value injected by FXMLLoader
@FXML // fx:id="TFpots"
private TextField TFpots; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt1Name"
private TextField TFspAtt1Name; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt1M"
private TextField TFspAtt1M; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt1H"
private TextField TFspAtt1H; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt2Name"
private TextField TFspAtt2Name; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt2M"
private TextField TFspAtt2M; // Value injected by FXMLLoader
@FXML // fx:id="TFspAtt2H"
private TextField TFspAtt2H; // Value injected by FXMLLoader
@FXML // fx:id="create"
private Button create; // Value injected by FXMLLoader
@FXML // fx:id="ErrorMsgs"
private TextArea ErrorMsgs; // Value injected by FXMLLoader
@FXML // fx:id="list"
private TextArea list; // Value injected by FXMLLoader
@FXML // fx:id="save"
private Button save; // Value injected by FXMLLoader
@FXML
void createOnAction(ActionEvent event) {
ErrorMsgs.setText("");
try {
File mobs = new File(System.getProperty("user.dir") + "\\mobs");
mobs.mkdir();
FileWriter fw = new FileWriter(new File(System.getProperty("user.dir") + "\\mobs\\" + TFname.getText() + ".mob"));
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write("name=" + TFname.getText() + "\r\nhp=" + TFhp.getText() + "\r\nattM=" + TFattMin.getText() + "\r\n"
+ "attH=" + TFattH.getText() + "\r\ndex=" + TFdex.getText() + "\r\npots=" + TFpots.getText() + "\r\n"
+ "spAtt1Name=" + TFspAtt1Name.getText() + "\r\nspAtt1M=" + TFspAtt1M.getText() + "\r\nspAtt1H=" + TFspAtt1H.getText() + "\r\n"
+ "spAtt2Name=" + TFspAtt2Name.getText() + "\r\nspAtt2M=" + TFspAtt2M.getText() + "\r\nspAtt2H=" + TFspAtt2H.getText());
}
} catch (IOException ex) {
ErrorMsgs.appendText(ex.getMessage());
}
Update();
}
@FXML
void saveOnAction(ActionEvent event) {
ErrorMsgs.setText("");
Update();
}
public void Update() {
File mobs = new File(System.getProperty("user.dir") + "\\mobs");
File[] lists = mobs.listFiles();
if (lists != null) {
for (File f : lists) {
try {
content = new Scanner(f).useDelimiter("\\Z").next();
if (content != null) {
***list.appendText(content + "\n\n\n\n\n\n");***
}
} catch (FileNotFoundException ex) {
ErrorMsgs.appendText(ex.getMessage());
}
}
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
FXML文档
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="759.0" prefWidth="1104.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fightwriter.FXMLDocumentController">
<children>
<TextField fx:id="TFname" layoutX="41.0" layoutY="35.0" promptText="Name" />
<TextField fx:id="TFhp" layoutX="41.0" layoutY="89.0" promptText="Health" />
<TextField fx:id="TFattMin" layoutX="41.0" layoutY="145.0" promptText="Minimum Attack" />
<TextField fx:id="TFattH" layoutX="41.0" layoutY="204.0" promptText="Maximun Attack" />
<TextField fx:id="TFdex" layoutX="41.0" layoutY="260.0" promptText="Dexterity" />
<TextField fx:id="TFpots" layoutX="41.0" layoutY="318.0" promptText="# of Health Potions" />
<TextField fx:id="TFspAtt1Name" layoutX="41.0" layoutY="373.0" promptText="Special Attack 1 Name" />
<TextField fx:id="TFspAtt1M" layoutX="41.0" layoutY="428.0" promptText="Special Attack 1 min dmg" />
<TextField fx:id="TFspAtt1H" layoutX="41.0" layoutY="486.0" promptText="Special Attack 1 max dmg" />
<TextField fx:id="TFspAtt2Name" layoutX="41.0" layoutY="543.0" promptText="Special Attack 2 Name" />
<TextField fx:id="TFspAtt2M" layoutX="41.0" layoutY="600.0" promptText="Special Attack 2 min dmg" />
<TextField fx:id="TFspAtt2H" layoutX="41.0" layoutY="658.0" prefHeight="31.0" prefWidth="187.0" promptText="Special Attack 2 max dmg" />
<Separator layoutX="604.0" orientation="VERTICAL" prefHeight="760.0" prefWidth="0.0" />
<Button fx:id="create" layoutX="42.0" layoutY="702.0" mnemonicParsing="false" onAction="#createOnAction" prefHeight="31.0" prefWidth="77.0" text="Create" />
<TextArea fx:id="ErrorMsgs" editable="false" layoutX="240.0" layoutY="35.0" prefHeight="698.0" prefWidth="351.0" promptText="ErrorMsgs" />
<TextArea fx:id="list" editable="false" layoutX="629.0" layoutY="21.0" prefHeight="711.0" prefWidth="448.0" />
<Button fx:id="save" layoutX="135.0" layoutY="702.0" mnemonicParsing="false" onAction="#saveOnAction" text="Update List" />
</children>
</AnchorPane>
FightWriter(主类)
package fightwriter;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FightWriter extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
FXMLDocumentController L = new FXMLDocumentController();
***L.Update();***
}
public static void main(String[] args) {
launch(args);
}
}
错误
Executing C:\Users\21114693\Documents\NetBeansProjects\FightWriter\dist\run803916352\FightWriter.jar using platform C:\Program Files\Java\jdk1.8.0_102\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at fightwriter.FXMLDocumentController.Update(FXMLDocumentController.java:103)
at fightwriter.FightWriter.start(FightWriter.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application fightwriter.FightWriter
Java Result: 1
Deleting directory C:\Users\21114693\Documents\NetBeansProjects\FightWriter\dist\run803916352
最佳答案
如果您查看堆栈跟踪,您会发现唯一可能为 null
的变量是 list
:
if (content != null) {
list.appendText(content + "\n\n\n\n\n\n"); // <-- line from stacktrace
}
这可能看起来令人困惑,因为您之前这样做过:
if (lists != null) {
但这是另一个变量(注意“s”)。 list
声明旁边的以下注释显然不正确:
@FXML // fx:id="list"
private TextArea list; // Value injected by FXMLLoader
我对 JavaFX 没有太多经验,但我认为您的代码是在 FXMLLoader 完成其工作之前调用的。
关于Java 文件上下文不为 null 会抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41871543/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!