Re: NullPointerException error...

From: Roland (roland_at_phony.biz)
Date: 02/20/05


Date: Sun, 20 Feb 2005 19:22:59 +0100

On 20-2-2005 17:55, Frances Del Rio wrote:

> Frances Del Rio wrote:
>
>> try {
>> String url = <url>;
>> Statement stmt; // no problem here...
>>
>> ResultSet rs; // get error says this var should be initialized..
>> // if I do
>> ResultSet rs = null; // get java.lang.NullPointerException error..
>>
>>
>> Class.forName("com.mysql.jdbc.Driver").newInstance();
>> con = DriverManager.getConnection(url);
>>
>> stmt = con.createStatement();
>> while (rs.next()) {
>> String themovies = rs.getString("title");
>> // out.print(themovies);
>> }
>
>
> had posted wrong code.. here it is again..
>
> try {
> String url = <url>;
> Statement stmt;
>
> ResultSet rs; // get error says this var should be initialized..
> // if I do
> ResultSet rs = null; // compiles fine, but get NullPointerException
> // when try to run.. (this is a servlet..)
> // (would love to know why all examples in this guy's book
> // are stand-alone's instead of either jsp's or servlets..
> // adapted from www.francesdelrio.com/java/oreillycode.html..)
>
>
> Class.forName("com.mysql.jdbc.Driver").newInstance();
>
> // ******* open connection ***********
> con = DriverManager.getConnection(url);
> String query = "select * from movies";
> stmt = con.createStatement();
>
> stmt.executeQuery(query);
           ^^^^
           rs = stmt.executeQuery(query);

> while (rs.next()) {
> String themovies = rs.getString("title");
> // out.print(themovies); // don't know if this is right,
> // but can't find out before I solve
> // above-mentioned error..
> }
>
> hope this is not too muddled.. thank you very much.. Frances
>
>
>

The ResultSet never got assigned with the result of the query (hence, it
still was null, causing the NPE in the while test).

-- 
Regards,
Roland de Ruiter
   ___      ___
  /__/ w_/ /__/
/  \ /_/ /  \


Relevant Pages

  • Problem with actualization
    ... When I show these strings on the strings I got: ... public static void executeQuery(String query) { ... Statement stmt; ... public static ResultSet getQueryResult{ ...
    (comp.lang.java.databases)
  • Re: Problem with actualization
    ... database or database driver error. ... public static void executeQuery(String query) { ... Statement stmt; ... public static ResultSet getQueryResult{ ...
    (comp.lang.java.databases)
  • Re: NullPointerException error...
    ... > Frances Del Rio wrote: ... You should be using the ResultSet that this ... Steve ...
    (comp.lang.java.help)
  • Re: data type error in my query
    ... the resultset returned the same value for comments. ... FROM dbo.tblWeblog as W1 LEFT OUTER JOIN dbo.tblUserComments as C ... > If blogID is a unique identifier for rows of tblWeblog, ... If I strip out W.blogComment the query runs fine. ...
    (microsoft.public.sqlserver.programming)
  • Re: When do selects execute for multiple resultsets
    ... SQL Server executes each query and stops when its ... When the first resultset is ... The CommandTimeout is measured from the time you execute the query until the ...
    (microsoft.public.dotnet.framework.adonet)