For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > NullPointerException error...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author NullPointerException error...
Frances Del Rio

2005-02-20, 3:58 pm

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);
}

thank you..

Frances

Frances Del Rio

2005-02-20, 3:58 pm

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);
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



Roland

2005-02-20, 3:58 pm

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

> Frances Del Rio wrote:
>
>
>
> 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_/ /__/
/ \ /_/ / \
Steve Horsley

2005-02-20, 3:58 pm

Frances Del Rio wrote:
> Frances Del Rio wrote:
>
>
>
> 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);


Here's your problem. You should be using the ResultSet that this
query returns, like this:
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
>
>
>


Steve
PerfectDayToChaseTornados

2005-02-20, 3:58 pm

"Frances Del Rio" <fdr58@yahoo.com> wrote in message
news:37rrduF57rd4kU1@individual.net...
| 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);
| }
|
| thank you..
|
| Frances

Hi Frances,

You are getting a NullPointerException because rs is null. You never assign
anything to it!!

Another problem with this code is that you have no sql statement to execute!

I'm sorry, but you really do need to start reading some books & tutorials.
Looking at this code & your inability to figure out the
NullPointerException, I would suggest that you get your basic Java skills up
to scratch & then come back to things like JDBC & Servlets. And when you do,
follow some proper tutorials through, so that you have an understanding of
what you are trying to do with your code. You are trying to run before you
have learned to walk.

Sorry if this sounds a bit harsh, but if you can get a better understanding
of the basics, it will save you a lot of hassle in the end :-)
--
-P
"Programs that are hard to read are hard to modify.
Programs that have duplicated logic are hard to modify.
Programs with complex conditional logic are hard to modify"

( Kent Beck)


Bryce

2005-02-23, 4:02 pm

On Sun, 20 Feb 2005 11:55:37 -0500, Frances Del Rio <fdr58@yahoo.com>
wrote:

>Frances Del Rio wrote:
>
>
>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();
>


Instead of this
> stmt.executeQuery(query);


do this:
rs = stmt.executeQuery(query);

You are getting a NPE here because rs is null (you never assigned it
the results from the executeQuery method.

> 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
>
>



--
now with more cowbell
Frances Del Rio

2005-02-23, 4:02 pm

Bryce wrote:

> On Sun, 20 Feb 2005 11:55:37 -0500, Frances Del Rio <fdr58@yahoo.com>
> wrote:
>
>
>
>
> Instead of this
>
>
>
> do this:
> rs = stmt.executeQuery(query);
>

Bryce, thank you very much for your response... I'm embarrassed to
admit what happened is I sipmly copied wrong code from book...
I finally got my first little db up and running.. since it's a bit
involved to post raw code for a jsp, I have zipped it (consists of just
index.jsp) and posted here..
www.francesdelrio.com/movies/index.zip
can you look and critique code, pls..

app is running here... www.francesdelrio.com/movies/index.jsp..

only thing I still I have to figure out how to do is the hardest: namely
tell app to warn user if user attempts to insert a title in list that
already exists in db.. need to do in app, not from db.. Bryce, again,
your help is always very much appreciated.. :) Frances


Frances Del Rio

2005-02-24, 4:01 pm



Bryce wrote:
> On Sun, 20 Feb 2005 11:55:37 -0500, Frances Del Rio <fdr58@yahoo.com>
> wrote:
>
>
>
>
> Instead of this
>
>
>
> do this:
> rs = stmt.executeQuery(query);
>
> You are getting a NPE here because rs is null (you never assigned it
> the results from the executeQuery method.


the frustrating thing about NPE errors is that it doesn't tell on what
var the error is.. on my webhosting the error logs don't tell you
anything (they all say something like 'file not found -- robot.txt' or
something like that..) thanks again.. Frances


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com