Using JDBC for a local sqlite database. Everytime I click login i get the java.lang.NullPointerException.
Things I have tried :
1.Verified the path to the database. (As the connection sucessfull window pops after running)
2.Verified the table name as well.
3.Tried moving the Database and updating the path.
4.Tried other method as well e.g(username='"+username.getText()"')
The Login code:
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class FXMLDocumentController implements Initializable {
Connection connection=null;
@FXML
private Label label;
@FXML
private TextField username;
@FXML
private PasswordField password;
@FXML
private Button login;
@FXML
private Button signup;
@FXML
private Button close;
public FXMLDocumentController()
{
connection=SQLconnection.dbConnector();
}
public void close(ActionEvent event)
{
System.exit(0);
}
public void login(ActionEvent event)
{
try
{
String query="select * from user where username=? and password=?";
PreparedStatement pst=connection.prepareStatement(query);
String uname=username.getText();
String pass=password.getText();
pst.setString(1, uname);
pst.setString(2, pass);
ResultSet rs=pst.executeQuery();
int count=0;
while(rs.next())
{
count++;
}
if(count==1)
{
System.out.print("Username password correct");
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect Username and
password");
}
rs.close();
pst.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
The Connection :
import java.sql.*;
import javax.swing.*;
public class SQLconnection {
Connection conn=null;
public static Connection dbConnector()
{
try
{
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Pushkar\\Documents\\NetBeansProjects\\Traveler\\traveler.db");
JOptionPane.showMessageDialog(null, "Connection Sucessfull");
return conn;
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}