0

The below code always generates a NullPointerException error.

The connection:

Connection con;
java.sql.PreparedStatement st;

public void Connect()
{
    try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        String user = "root";
        String password = "";

        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mpdb",user,password);

        String sql = new String("SELECT * FROM 'dati' WHERE user =? and pass =?");

        st = con.prepareStatement(sql);

    }catch(Exception ex){

        JOptionPane.showMessageDialog(null, "Connessione non riuscita");
        ex.printStackTrace();
    }
}

The login:

@SuppressWarnings("deprecation")
public void mouseClicked(MouseEvent arg0) {

    TextFields tf = new TextFields();
    Driver d = new Driver();

    try{

        String sql = new String("SELECT user,pass FROM 'dati' WHERE user =? and pass =?");

        ps = d.con.prepareStatement(sql);
        ps.setString(1,tf.ftf.getText()); //JFormattedTextField
        ps.setString(2,tf.pf.getText());  //JPasswordField

        rs = ps.executeQuery();

        if(rs.next()){

            JOptionPane.showMessageDialog(null, "User successfully logged");

        }else{

            JOptionPane.showMessageDialog(null, "User not found");
        }

        }catch(Exception ex){

            ex.printStackTrace();
        }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
  • 7
    You should post your error – Robin Ellerkmann Apr 19 '15 at 22:29
  • Specifically, please post your stack trace, indicating what code the line numbers in the stack trace refer to. Thanks! – Ken Y-N Apr 20 '15 at 00:57
  • Use a debugger to step through your code and locate the null variable calling the exception. Then you can add checks to prevent the issue. – JNYRanger Apr 20 '15 at 01:40
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Joel Apr 20 '15 at 14:58
  • @Joel no, I know what is. I only make a mistake with the diclaration. Can you answer to the question below? – DarkCrocodile Apr 21 '15 at 18:03

2 Answers2

0

The select statement is wrong. Single quotes (') denote string literals in SQL, not object names (in your case - the table name). Since the SQL is invalid, no result set can be created, and hence the exception.

To resolve this, you should remove the quotes around 'dati':

String sql = "SELECT user,pass FROM dati WHERE user =? and pass =?"
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

I resolved with this code:

Execute connection:

package main;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JOptionPane;

public class Driver {

   Connection con = null;

   public static Connection Join()
   {

    try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        String user = "root";
        String password = "";

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mpdb",user,password);

        return con;

    }catch(Exception ex){

        JOptionPane.showMessageDialog(null, "Connessione non riuscita");
        ex.printStackTrace();
        return null;

    }

  }

}

Login Button:

@SuppressWarnings("deprecation")
        public void mouseClicked(MouseEvent arg0) {

            TextFields tf = new TextFields();

            String sql = new String("SELECT * FROM `dati` WHERE user = ? and pass = ?");

            try{

                ps = con.prepareStatement(sql);
                ps.setString(1,tf.ftf.getText().trim());
                ps.setString(2,tf.pf.getText().trim());

                rs = ps.executeQuery();

                if(rs.next()){

                    JOptionPane.showMessageDialog(null, "Login effettuato");

                }else{

                    JOptionPane.showMessageDialog(null, "Utente o password non corretti");

                }

                }catch(Exception ex){

                    ex.printStackTrace();

                }

        }
        public void mousePressed(MouseEvent arg0) {

        }
        public void mouseReleased(MouseEvent arg0) {

        }
    });

But now I've a new problem: the program do its work but it doesn't compare correctly the password. How do I have to proceed?