*
The moose likes JDBC and the fly likes how to slove nullpointerexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to slove nullpointerexception" Watch "how to slove nullpointerexception" New topic
Author

how to slove nullpointerexception

saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi, i hardly trying to read xls file i getting null pointer Exception please guide me please see below code

String imp=request.getParameter("F1");
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

//Below line i getting java.lang.NullPointerException

conn = DriverManager.getConnection("jdbcdbcriver={Microsoft Excel Driver (*.xls)};DBQ="+imp);

st = conn.createStatement();
String query = "Select * from [Sheet1$]" ;
rs = st.executeQuery( query );
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


conn = DriverManager.getConnection("jdbcdbcriver={Microsoft Excel Driver (*.xls)};DBQ="+imp);

I'd be suprised if you could get a NullPointerException on this line. Is it really the line where the exception occurs?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi thanks for yr reply. yes i getting exception that line only before that line i given display statements its all printing but after that line no display statement working

//here i getting parameter value
String imp=request.getParameter("F1");
out.println(imp);//its displaying that value

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
out.println("welcome");//this one displaying

//Below line i getting java.lang.NullPointerException

conn = DriverManager.getConnection("jdbcdbcriver={Microsoft Excel Driver (*.xls)};DBQ="+imp);//
out.println("welcome2");//this line not printing

st = conn.createStatement();
String query = "Select * from [Sheet1$]" ;
rs = st.executeQuery( query );
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, the NullPointerException will tell you the line number and the class this is happening in. The line you say it is happening on can't cause a NullPointerException itself, since it is a static methos you are calling. So it must be something else. Can you post the stacktrace?
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi thanks for your guide. when i compiling that code not showing any error.when i calling that file in web browser up to that line all print statement displaying after that line no display statement working i thinking that line only having problem please see Below code i trying to get and read xls file.please guide me i trying lot

code:
try
{
//getting parameter value
input of F1="C:/asia/book1.xls"
String imp=request.getParameter("F1");

out.println("welcome5");//printing
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

out.println("welcome to peopletech23999884");//printing
out.println(imp);printing C:/asia/book1.xls

//after imp value printed its printing java.lang.NullPointerException

conn = DriverManager.getConnection("jdbcdbcriver={Microsoft Excel Driver (*.xls)};DBQ=" + imp);
out.println("welcome to peopletech1");//not printing here onwards no print statement displaying

st = conn.createStatement();
String query = "Select * from [Sheet1$]" ;
rs = st.executeQuery( query );
while( rs.next() )
{
String name=rs.getString(1);
String loc=rs.getString(2);
try
{

Class.forName
("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "meetme", "meetme");
st = conn.createStatement();
st.executeUpdate("Insert INTO sample1(`name` , `location`)VALUES('"+ name +"' ,'"+ loc +"')");
rs1=st.executeQuery("select * from sample1");
if(rs1.next())
{
out.println(name);
}
}
catch(Exception e)
{
out.println(e);
}
}//while
}//try
catch( Exception e )
{
out.println(e);
}
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


when i compiling that code not showing any error

A NullPointerException is a runtime exception. You'll only see it when you run the code.


catch( Exception e )
{
out.println(e);
}


Can you change thsi line to:


rerun your applciation and post the out put. As I said before, NullPointerExceptions tell you the class and line that they are occuring on. The line you say this is happening on is a call to a static method, so its not the line that is causing the problem (though another problem may be occuring when you call it). Post the stacktrace and we can help more.
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi, thanks for your guide. now null pointer exception not showing but after that line no print statement displaying.what the value i read that also not stored in database please guide me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by saravanan rajendran:
hi, thanks for your guide. now null pointer exception not showing but after that line no print statement displaying.what the value i read that also not stored in database please guide me


Not showing? Is nothing appearing in your logs?
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi thank you, up to that line what are the print statement i given its displaying after that line no print statement displaying but its not showing null pointer exception also please guide me
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi friend i eagerly waiting for you reply. After that line no other code executing directly coming to that catch statement and its came out from program.please guide me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, so you must have done somethign to where stderr is logged. Which servlet container are you using? If its Tomcat, and a pre-version 6 version, stderr goes to the stderr.log, not localhost.log. Check your log files. It will be fr easier to fix this if we have some proper exception messages.
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi, thanks for your reply, In my log file does not contain stderr.log file

It contain
host-manager.log
manger.log
local host-manager.log
catalina.log
admin.log

those file only contain please guide me
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well, I'd check how you've set up your logging (see this). No sense trying to fix this blindly in the dark. Get the stacktrace then we can do something to fix it.

Also, does the file C:/asia/book1.xls exist on the server?
saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi, thank you very much for your reply.yes i have C:/asia/book1.xls exist in my server.but i don't know about stderr.log file how to get and put that please guide me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
"stderr" isn't usually a log file. It is shorthand for the standard error stream which is represented by a java.io.PrintStream object usually called java.lang.System.err. You are doubtless familiar with it as System.err; it is similar to System.out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to slove nullpointerexception