ResultSet在Android Studio Java中捕获NullPointerException

时间:2019-07-19 10:07:11

标签: java mysql jdbc nullpointerexception

为什么我的ResultSet抓住了NullPointerException甚至我都填满了所有需要的变量? 我试图从本网站的其他问题中寻找,但没有得到任何帮助 这是我的代码

login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final EditText edit1 = (EditText) findViewById(R.id.edit1);
            final EditText edit2 = (EditText) findViewById(R.id.edit2);
            String sql =  "SELECT * FROM user WHERE username='"+edit1.getText().toString()+"' AND password=MD5('" + edit2.getText().toString() + "')";
            try{
                Connector co = new Connector();
                ResultSet RUser = co.rs(sql);
                if (RUser.next())
                {
                    //do something
                }
            }catch (Exception e){
                Toast.makeText(LoginActivity.this, e, Toast.LENGTH_SHORT).show();
            }
        }
    });

我从co类中制作了Connector对象 这是Connector类的代码

public class Connector {

Connection con;
Statement st;
PreparedStatement stp;
public Connector()  {
    try{
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (Exception e) {
    }
    try{
        this.con = DriverManager.getConnection("jdbc:mysql://192.168.43.74/simprokan","fadli","fadli");
        this.st = con.createStatement();
    }
    catch (SQLException e) {
    }
}

public ResultSet rs(String sql){
    ResultSet rs = null;
    try {
        rs = this.st.executeQuery(sql);
    } catch (SQLException ex) {
        Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
    }
    return rs;
}}

这是错误代码

java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.ResultSet java.sql.Statement.executeQuery(java.lang.String)' on a null object reference

0 个答案:

没有答案