0

I have two different classes with a method inside each. Classes are 'DBConnect' and 'Controller'. Since I try to build an fx application I have a combobox named as 'privilageBox' and a generic type named 'privilageList' for its choices (inside Controller class).

ObservableList<String> privilageList = FXCollections.observableArrayList("Admin","Employee");

There is a function called 'login' inside the same class and there is some coding inside it. I have an if statement as well and what I am trying to do inside the requirements section is I try to call 'checkUser' method that is located inside the DBConnect class and set its parameters.

DBConnect lg = new DBConnect();
if(lg.checkUser(textField1.getText(),passwordField1.getText(),(privilageBox.getValue()).toString()))

This is where I get the NullPointerException. I am setting the paremeters wrong and I ran out of solutions.

Here is the checkUser function's parameters inside DBConnect class:

public boolean checkUser(String username, String password,String privilage) throws SQLException

I have tried to set the parameters for 'checkUser' and if statement inside 'login' as below:

public boolean checkUser(String username, String password,ObservableList privilage) throws SQLException

if(lg.checkUser(textField1.getText(),passwordField1.getText(),privilageList.toString))

It did not work also. By the way, this is a runtime error. I get no errors at compile time. Error message (discarded unnecessary parts):

Caused by: java.lang.NullPointerException
at sample.DBConnect.checkUser(DBConnect.java:70)
at sample.Controller.login(Controller.java:44)
Clay
  • 747
  • 7
  • 12
  • The nullpointer is probably caused by "(privilageBox.getValue()).toString()" – vc73 Apr 23 '19 at 12:51
  • @JonK I could not find an answer to my problem there. – Clay Apr 23 '19 at 12:51
  • What is the value of `privilageBox.getValue()`? If it's null, then the thrown NPE is expected as you are trying to call `null.toString()` – anasmi Apr 23 '19 at 13:00
  • @anasmi it is a combobox, user selects what it is. In this case, I am also the user and i select the right criterias – Clay Apr 23 '19 at 13:04
  • But what is the value you get? Can you put the `System.out.println` before calling the method to check? – anasmi Apr 23 '19 at 13:13

1 Answers1

0

privilageBox.getValue() can return null value, if you try to use toString() on null object, java throws the NullPointerException.

You can use that to never have a null value :

 privilageBox.getValue() != null ? privilageBox.getValue().toString() : "other..."

"other..." is the default privilege (you can put "Employee")

anasmi
  • 2,562
  • 1
  • 13
  • 25
  • Nice approach to the problem but that did not work either. – Clay Apr 23 '19 at 13:18
  • Then you need to figure out which thing on that line is null. Using a debugger would be the preferred approach. Alternatively, you could put each variable assignment on a separate line so you know exactly which one is causing the exception. – Jordan Apr 23 '19 at 14:04
  • @Jordan lg.checkUser returns null – Clay Apr 23 '19 at 14:46