Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception

 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if someone can help me further in the application ..
it will be appreciated.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dint mention the AirportSelect.java file.
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i have added it...
 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i called the function from another class and it is returning values..
check.java

results are fine...
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64608
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please properly indent your code.
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you put your JDBC driver jar file?
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you execute sql to see the record num in fact.
 
ankur tyagi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic