Aaron N. Aaron N. - 12 days ago 9
Java Question

Error in running Java FXML program

I'm getting an error on netbeans when trying to run my java fxml application.

Here is the error I am getting:


Caused by: java.lang.NullPointerException at apnc7dsimulator.UIController.handleStart(UIController.java:4‌​2) at apnc7dsimulator.UIController.handleNew(UIController.java:37) at apnc7dsimulator.UIController.initialize(UIController.java:31‌​) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 17 more


It appears the error is originating from my controller. Here's my code:

public class UIController implements Initializable {

@FXML
private TextArea screen;

@FXML
private TextField commandLine;

private Login login;

@Override
public void initialize(URL location, ResourceBundle resources) {
handleNew();
}

public void handleNew(){
screen.clear();
commandLine.clear();
handleStart();

}

public void handleStart(){
login.loginSys(screen);

}}


The code the controller references is this:

public class Login implements PrintToScreen{

@Override
public void print(String text, TextArea screen) {
screen.appendText(text);
}

@Override
public String read(TextArea screen) {
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
while(readString != null){

print(readString,screen);

if(scanner.hasNextLine()){
readString = scanner.nextLine();
}
else{
readString = null;
}
}
return readString;
}

public void loginSys (TextArea screen)
{
String user = "...";
String pass = "...";

print("Please Enter Username:",screen);
user = read(screen);

print("Please Enter Password:",screen);
pass = read(screen);

print("Check Credentials... Please Wait.",screen);
}}


I'm sure it's a simple mistake I'm making or some sort of convention I'm not following correctly but this has had me stumped for a while now.
Any help would be great.

Answer

Assuming you've posted all of your code, it appears that the login member variable is never initialized. By default it is initialized to null. The result is that when this line of code runs:

login.loginSys(screen);

Since login is null, the code is throwing a NullPointerException.

Somewhere in your code, before handleStart() is called, possibly in the UIController constructor, you need to initialize login, something like:

login = new Login();