Thread: Fwd: Exception in thread "main"

Fwd: Exception in thread "main"

From
Mathew McBride
Date:
Hi,

    I wrote up some code to retrive a company name associated with a id from a
database. This is the code I used:

try {
    Connection db = DriverManager.getConnection("jbdc:postgresql:vapilot",
"postgres" , " ");
    PreparedStatement st = db.prepareStatement("SELECT \"va_vairline\" FROM
\"va_pilots\" WHERE va_id='1'");
    ResultSet rs = st.executeQuery();
    add(name);
    add(va);
    va.setText(rs.getString("va_vairline"));
    }
    catch (SQLException se) {
    System.out.println("ERROR: Cannot Execute SQL query");
    }

I compiled and ran it:
java -Djdbc.drivers=org.postgresql.Driver org/vaproject/vapilot/va_pilot
And it said this:
Exception in thread "main" java.lang.NullPointerException
        at org.postgresql.jdbc2.ResultSet.getString(ResultSet.java:168)
        at org.postgresql.jdbc2.ResultSet.getString(ResultSet.java:553)
        at org.vaproject.vapilot.infoPanel.<init>(va_pilot_gui.java:130)
        at org.vaproject.vapilot.va_pilot_gui.<init>(va_pilot_gui.java:37)
        at org.vaproject.vapilot.va_pilot.<init>(va_pilot.java:14)
        at org.vaproject.vapilot.va_pilot.main(va_pilot.java:27)

I used the file jdbc7.2dev-1.2.jar from the RedHat 7.3 RPM postgresql-jdbc.
Should I either download a new one from the JDBC driver website or compile
one myself?



Re: Fwd: Exception in thread "main"

From
Toby Doig
Date:
>     ResultSet rs = st.executeQuery();
>     add(name);
>     add(va);
>     va.setText(rs.getString("va_vairline"));

you need to do an rs.next() first to move rs to the first record.
i'd also test whether sexecuteQuery() did actually return a ResultSet and
not null.

toby