0

When trying to select data from a table in Access, I get a java.lang.NullPointerException errror. The code works for other tables though.

This is the code that works:

//cun is passed as a parameter

        try {
            ResultSet rs = db.queryTbl("SELECT * FROM tblCustomer WHERE CustomerUName = '" + cun + "';");
            while (rs.next()) {
                cusFName = rs.getString("CustomerFName");
                cusSName = rs.getString("CustomerSName");
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        }

This is the code that doesn't work:

//dun is passed as a parameter

        try {
            ResultSet rsDri = db.queryTbl("SELECT * FROM tblDriver WHERE DriverUName = '" + dun + "';");
            while (rsDri.next()) {
                driFName = rsDri.getString("DriverFName");
                driSName = rsDri.getString("DriverSName");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
        }

The error takes me to the line:

ResultSet rsDri = db.queryTbl("SELECT * FROM tblDriver WHERE DriverUName = '" + dun + "';");

Please can you help?

acarlstein
  • 1,799
  • 2
  • 13
  • 21
  • try to execute this query directly in access, to make sure it's valide for tblDriver Table **SELECT * FROM tblDriver WHERE DriverUName = '" + dun + "';** – DEV Aug 10 '20 at 20:28
  • First, your query might end a victim of SQL injection if the attacker use something like the trick `and 1 = 1 UNION ALL`. Second, you must ensure that the table and columns do exists. Third, it will be more efficient if you name the columns you wish to query instead of using `*` in your query. – acarlstein Aug 10 '20 at 20:29

0 Answers0