我正在尝试创建一个保存文件,但是当我尝试调用方法“ fileUserName”时出现NullPointerException。有人可以说明为什么我会收到此错误以及如何解决该错误吗?
public class Register {
GridPane grid;
。 。 。
public Pane getPane() {
TextField text_name = new TextField();
TextField text_password = new TextField();
TextField text_reenterpassword = new TextField();
Button button_ok = new Button("OK");
button_ok.setOnAction(e -> {
String userName=text_name.getText();
String password=text_password.getText();
String rePassword=text_reenterpassword.getText();
DataSaving dataSave=new DataSaving(userName,password);
if (! (new File("src/data/user/ "+userName).exists())) {
FileHandle fileUser= dataSave.fileUserName("src/data/user/"+userName);
dataSave.saveUser(fileUser);
}
new LevelEstimation();
stage.close();
});
return grid;
}}
public class DataSaving {
String userName,password;
int userId;
public static String userPath;
public DataSaving(String username,String pass)
{
this.userName=username;
this.password=pass;
}
public static FileHandle fileUserName(String userName)
{
return Gdx.files.local(userName+"/"+"loginData.json");
}
public void saveUser(FileHandle loginFile) // saving the user name and password in a file
{
User user=new User(userName,password);
Json json=new Json();
json.setOutputType(OutputType.json); //to set quotation in json file to each string
loginFile.writeString(json.prettyPrint(user), false);
}
}
因此,在类“ DataSaving”中调用“ fileUserName”方法时,单击类“ Register”中的Butto“ button_ok”,就会收到错误消息
谢谢您的任何帮助。