Search code examples
jdbcinformixsqlexception

Using JDBC Driver for Informix + SQLException being thrown when NULL Value is encountered


I'm using the JDBC driver for Informix. I connect to my host just fine, but when the query is executed a null value is returned for one of the fields specified in my select. Instead of just retrieving that value, and SQLException gets thrown:

Column (colname) not found in any table in the query (or SLV is undefined).

I'm using the driver this way:

    try{
    PreparedStatement pstmtDist = conn.prepareStatement(query2);

    ResultSet rsDist = pstmtDist.executeQuery();

    while(rsDist.next()){
            int distCaseId = 0;
            String distCaseIdStr = new String();

            int distCaseDefNum = 0;
            String distCaseDefNumStr = new String();

            distCaseIdStr = rsDist.getObject("colname").toString();
            distCaseId = Integer.parseInt(distCaseIdStr.trim());

            distCaseDefNumStr = rsDist.getObject("colname2").toString();
            distCaseDefNum = Integer.parseInt(distCaseDefNumStr.trim());

            //System.out.println(String.format("distCaseId == %d  distCaseDefNum == %d\n",distCaseId,distCaseDefNum));

    }// end while district cases

    rsDist.close();
    pstmtDist.close();
    connDist.close();

    }
    catch (SQLException e){
            System.out.println("EXCEPTION: "+e.getMessage());


    }

Any tips are welcomed!

-TU


Solution

  • I think problem is in line:

     distCaseDefNumStr = rsDist.getObject("colname2").toString();
    

    and with similar lines where getObject() can return null.

    If your colname2 has null value then getObject() returns null and then Java tries to run toString() method on null object.

    Yes, the message about column not found in result is strange, but I think that you really observe null pointer exception. Use getString() or getInt() methods of ResultSet.