Deddy Feryanto
Deddy Feryanto

Reputation: 47

Result Set return null value

I'm having a trouble when retrieving in a very simple SELECT query. I'm trying to retrieve all information from table 'db_con_type' and put them in array String of dbChoices.

Database:

The Select query works fine in report designer. Sorry i can't upload the images since i'm new here.

Code:

public void setDbTypeChoices()
    {
       try
        {
            con2db connect2db = new con2db();
            con = connect2db.aksesDatabase();
            stmt = con.createStatement();
            String sql = "SELECT * FROM db_con_type";

            rs = stmt.executeQuery(sql);

            int i = 0;
            while(rs.next())
            {
                this.dbChoices[i] = rs.getString("DB_Type");
                i++;
            }

            rs.close();
            stmt.close();
            ps.close();
            con.close();

            //Close Connection
            connect2db.tutupKoneksi();
        }
       catch(Exception e)
       {
           System.out.println("SQL ERROR 1 = " + e.getMessage());
           System.out.println("SQL ERROR 2 = " + e.toString());
       }
    }

Error:

SQL ERROR 1 = null

SQL ERROR 2 = java.lang.NullPointerException

Question:

Any idea what is happening? Any Solution?

Thanks

Upvotes: 1

Views: 9990

Answers (2)

HJW
HJW

Reputation: 23443

By the way, it is a good practice that if you are handing ResultSet that you first check if the result set returned is not null before operating on it e.g. rs.next() which will give you a NPE if rs is null.

if(rs != null) {
    while(rs.next()) {
        this.dbChoices[i] = rs.getString("DB_Type");
        i++;
    }
}

Upvotes: 1

Deddy Feryanto
Deddy Feryanto

Reputation: 47

Found the problem. it's because of the ps.close() code..

since i don't use it here but i close it and it rises a problem..

thanks to the printStackTrace idea by Luiggi. thanks for all answers. i really appreciate it.. :D

Upvotes: 0

Related Questions