I'm trying to create an application that requires user login. I've used SceneBuilder and FXML file for the interface. I now have to retrieve data from a SQLite database (I've named the table with the data as "Users"). When I run this program, it throws a NullPointerException (I already data stored on the table). How do I fix it?
//P.S. I use IntelliJ edit the database from the IDE itself.
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Controller {
@FXML TextField username;
@FXML TextField password;
@FXML Label warning;
Connection c;
PreparedStatement pst;
ResultSet rs;
public void loginButtonClicked() throws IOException {
try {
String query = "select * from Users where Username=? and Password=?";
pst = c.prepareStatement(query);
pst.setString(1, username.getText());
pst.setString(2, password.getText());
rs = pst.executeQuery();
if (rs.next()) {
warning.setText("Successful");
} else {
warning.setText("Not Successful");
}
} catch (Exception e) {
System.err.println(e);
}
}
public void cancelButtonClicked(ActionEvent event) throws IOException {
System.out.println("User has cancelled login...");
((Node)event.getSource()).getScene().getWindow().hide();
}
}