0

I'm trying to create a save file but I get a NullPointerException when I try to call the method "fileUserName". can somebody please exsplain why I get this error and how to fix it?

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);
}
}

so, I get the error when the method "fileUserName" in the class "DataSaving" is called bei clicking the Butto "button_ok" inside the class "Register"

Any help would be appreciated, thanks.

Drashti Pandya
  • 348
  • 1
  • 5
  • 15
caro
  • 1
  • 1
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zephyr Jun 26 '18 at 14:02
  • My assumption is that `Gdx.files.local` does not actually exist... – Zephyr Jun 26 '18 at 14:03
  • You've got a space in your path (setOnAction if statement). – TVASO Jun 29 '18 at 14:36
  • Is this before or after you've initialized the game by calling `new LwjglApplication`? If not, it is because the `Gdx.files` object has not been initialized. – JoseRivas1998 Jul 30 '18 at 21:51

0 Answers0