File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Null pointer exception

elli dian
Greenhorn

Joined: May 18, 2006
Posts: 15
I want to call stored procedure using callablestatement, but i get error:null pinter exception.
please give a solution to solve it.

My program:

package absen_dexa;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Connection_Database {
private Connection koneksi;
public Connection_Database()
{
}
public Connection getConnection()
{
try
{
InitialContext cont = new InitialContext();
DataSource ds = (DataSource) cont.lookup("jdbc/ds2");
return ds.getConnection("ess_adm", "adm_ess");
}catch (NamingException e)
{
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}

Connection conn=koneksi.getConnection();

try
{
CallableStatement cs=conn.prepareCall("{call submitClockingData(?,?,?,?)}");
cs.setString(1,"DX");
cs.setString(2,"DX005010001");
cs.setString(3,date1);
cs.setInt(4,1);
cs.execute();
}catch(SQLException err)
{
System.out.println("Error "+err.getClass());
}finally
{
conn.close();
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As suggested in your last null pointer topic, it would be much easier for others to help if you posted the trace.

(Code Tags would also help.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, you don't say where the NPE happens -- the stack trace has a lot of useful information in it. But one thing I see here is that if the getConnection() method fails, it prints an exception message and returns null. Then the code that calls getConnection() fails to test the return value for null; so if getConnection() fails, you'll get an NPE in its client code.

If getConnection() fails, the routine that calls it should fail, too, so actually, getConnection() should not catch those exceptions; it should just let them go up and abort the caller.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Now that I've looked closer at your previous post, I see that this is a continuation of that topic. Please continue this discussion in that original thread, rather than starting a new thread. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer exception
 
Similar Threads
connection object
calling SQL Server stored procedures
Spring JDBCTemplate procedure
help: null pointer exception
LAST_INSERT_ID() with threads