aspose file tools*
The moose likes JDBC and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Null Pointer Exception" Watch "Null Pointer Exception" New topic
Author

Null Pointer Exception

ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
Hello friends ,
I am trying to develop a web app that will show flight timings of different airports...
but i am having the following error when running servlet...
the interface is basic from beer application in headfirst but i was trying a test run
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
AirportDD.getFlights(AirportDD.java:34)
AirportSelect.doPost(AirportSelect.java:16)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.28 logs.
here are the files
web.xml

Form.html

AirportSelect.java

AirportDD.java
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
if someone can help me further in the application ..
it will be appreciated.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Null pointer exceptions occur when you try to call a method on an object when the object is null.

Your error message told you that this occurred on line 34.
Do you have a variable on line 34?
Are you trying to call a method of the object pointed to by that variable?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

yeah it is the case the data you are trying to fetch from database might be null.you should check that what values it is getting by printing on console .

regards
Raza!


Good luck!!
A small leak can sink a Gigantic ship.>
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
i have used these class files from command prompt and they were returning values..
its only creating problems when i started using a servlet for retrieving values
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

You dint mention the AirportSelect.java file.
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
now i have added it...
Stephen McConnell
Greenhorn

Joined: Mar 23, 2010
Posts: 19
The initial exception occurs on Line 34 of your AirPortDD.java file.... that is in the general areas of the attempt to access the result set....

Did you get the output from line 96 here in AirPortDD.java? which is the error from an exception trying to retrieve the result set in the first place....

There is so much wrong with this code from a design point of view... no closing of connections, no closing of statements.... Bad handling of exceptions....

Stephen McConnell


Stephen McConnell

"Boredom is a personal defect"
-- Lamar Stephens
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
i called the function from another class and it is returning values..
check.java

results are fine...
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

some where object is not initialized.
best way is to run un debug mode or print incremental integers in the doubted line..by that you will get to know where exactly your code got struck.


Sudheer
SCWCD, SCJP 5
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
the problem is that nothing is being added to the ArrayList..
is there any problem with the logic??
the servlet creates an object of class AirportDD , having a varaible of type List code.
the function is called and the function instantiates code as an arraylist and adds some string to it...
will the servlet have the same list as the attribute of the object it created ???
can any one help....

assume missing things....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Please properly indent your code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
problem is that resultset is returning nothing when run from a servlet but it
returns the desired values when the same function is run from different class' main
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Look at my first reply to this question.

If the NPE is being thrown on a line that contains only a rs.next() statement, then the problem isn't that the resultSet is returning nothing.
It's that the resultSet variable "rs" is null.

This means that your problem is happening before you get to that line.

You've got print statements in all of your catch blocks.
I suggest you clear all of your logs, run it again until it breaks and go through them to see what else is being thrown before you try to call rs.next().
If I had to take a wild guess it would be that the app can't find your JDBC driver.
You might want to do something more in your catch block than just log errors.
If you can't make the database call, your app should be dying before you try to read from the resultSet.

ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
how can i make sure that the application can find the JDBC drivers??
please help i am new to all this and this is my first application..
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
i tried the following program to check the problem
and the exceptions were can anyone explain how to correct this??

result is
Good till now
SQLException is this: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified SQLState : IM002 VendorError: 0
Hello World. Sun... 1.4
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Where did you put your JDBC driver jar file?
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
the initial problem was removed by making a system DSN.
the servlet is getting values ..and page is generated.
but there is a new sort of problem now..
when i start the database and the tomcat and load the page , the values are shown just fine but if i refresh the page 4-5 times
the servlet starts giving the same exception all over again..
i suspect its because not clossing connections as mentioned by Ben

can someone suggest me how to close connection in respect to the code AirportDD.java
as i am using different functions...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

as i am using different functions...


Do you have a good reason for using different methods?

Typically a database call is laid out like this:


The finally block will always get executed, even if something blows up in the try or catch blocks.
This guarantees that your connections will always get closed.

If you're not closing them, your app will work for a while.
Then, when you've hit the maximum number of open connections, it will fail.
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
i am using functions because i am using nested queries, so i would need to paste the code twice ..
even then i tried doing it with this code..

but the result was that i only get the first column from the table
and nothing else and this time i was able to refresh it as many times i want...
Lawrence Xu
Greenhorn

Joined: Feb 11, 2004
Posts: 11
can you execute sql to see the record num in fact.
ankur tyagi
Ranch Hand

Joined: Apr 01, 2009
Posts: 49
do you mean running directly sql command in oracle...??
yes they run just fine...
they are running with servlet as well but due to not closing of connections, it gives NPE
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception