Search code examples
javadatabasenullpointerexceptionsap-ase

NullPointerException when executing rs = stmt.executeQuery(queryData)


I'm trying to connect to a Sybase ASE database. When I retrieve data I get NullPointerException on rs = stmt.executeQuery(queryData);.
I tried the same query with dbVisualizer but everything is ok there.

static void retrieveData() {
   try {
       String queryData="SELECT * FROM med WHERE item_id like 'A00001'";
       // process the results.
       rs = stmt.executeQuery(queryData);
       while (rs.next()) {
            String s = rs.getString("item_naam");
            String n = rs.getString("item_mnemo");
            System.out.println(s + "   " + n);
       }
   }catch(SQLException ex) {
       System.err.println("RetrieveData: " + ex.getMessage()+ " sqlstate = " +
                ex.getSQLState());
       System.exit(1);
   }

Solution

  • The problem seem to be that stmt is not initialized, i assume that you at least have defined the variable stmt, cause if not, the developer environment would show an error.

    There are lot of NullpointerExceptions caused for not initializing a variable, i mean you define it and then you forget to initialize it, its a good advice (if you dont have memory problems and you are opften to forget to init the variables) to do both things simultaneously.

    yo can do:

     Statement s = c.createStatement();
      rs = s.executeQuery(queryData);
    

    and remember that c where previoulsy initialized and rs was defined before...