-2

I'm using MS Access .mdb as my database. I don't really understand this code, I got copy of it from my lecture.

Years ago I used this code and it used to work, now I got an error and I don't know why. What's wrong?

public class Connect {
    public ResultSet rs;
    Connection con;
    java.sql.Statement st;
    ResultSetMetaData rsm;

public Connect(){
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Db.mdb";
        con = DriverManager.getConnection(url,"","");
        st = con.createStatement(1004,1008);
    } catch (Exception e){
    }
}
public ResultSet selectData(String query){
    ResultSet rset;
    try {
        rs = st.executeQuery(query);
        rsm = rs.getMetaData(); 
        if(rs.next()){
            return rs;
        }else{
            return null;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
  }

}

Im using a query in class Login

if(con.selectData("select * from MsUser where Username='"+textUsername.getText()+"' and Password='"+textPassword.getText()+"'")==null){
                JOptionPane.showMessageDialog(null, "Invalid username or password!", "Error", JOptionPane.ERROR_MESSAGE);

but it doesn't work.

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Connect.selectData(Connect.java:27)
    at Login.actionPerformed(Login.java:98)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
irfan
  • 9
  • 1
    You don't know how to solve a NullPointerException? first of all, this: catch (Exception e){ } is very bad to do. you are not avoiding crashes, you are merely hiding them. secondly, if the stacktrace you provided is not enough for you to find where the problem is (line 27 of your code), you should not be attempting to connect to a db yet. first learn the basics – Stultuske Jan 05 '16 at 08:23
  • Possible duplicate of [Find out what variable is throwing a NullPointerException programatically](http://stackoverflow.com/questions/2667908/find-out-what-variable-is-throwing-a-nullpointerexception-programatically) – Stultuske Jan 05 '16 at 08:24

1 Answers1

0

Please make sure that the table "MsUser" is exists. Did you instantiated 'con' from 'Connect' class to call con.selectData() method?

I think you missed to instantiate any object.

Kishor Jadhav
  • 196
  • 2
  • 16