0

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

Here is a screenshot : SS

  • Where does the `NullPointerException` appear? – RamenChef Oct 03 '18 at 15:15
  • @RamenChef It appears in a Window after I enter username,password and click login – Pushkar Mahajan Oct 03 '18 at 15:17
  • Where is it being thrown from? You should be able to see this using the stack trace. – RamenChef Oct 03 '18 at 15:22
  • @RamenChef I am really sorry but there is no stack trace(You can refer to the ScreenShot). Apologies if u are annoyed – Pushkar Mahajan Oct 03 '18 at 15:33
  • 1
    A stack trace is available, you're just not capturing it in your `catch` block. You should write it to a log file so you can inspect it (and share it with us) after the error occurs. – Gord Thompson Oct 03 '18 at 16:54
  • 1
    If you ignore exceptions, and just let your program continue as if nothing happened, then there will likely be things not initialized (eg `connection` in `FXMLDocumentController`). Start logging exceptions, and fix your error handling. – Mark Rotteveel Oct 03 '18 at 17:36

0 Answers0