Forums Register Login
Null pointer exception
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()
InitialContext cont = new InitialContext();
DataSource ds = (DataSource) cont.lookup("jdbc/ds2");
return ds.getConnection("ess_adm", "adm_ess");
}catch (NamingException e)
} catch (SQLException e) {
return null;

Connection conn=koneksi.getConnection();

CallableStatement cs=conn.prepareCall("{call submitClockingData(?,?,?,?)}");
}catch(SQLException err)
System.out.println("Error "+err.getClass());
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.)
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.
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!
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Download Free Java APIs to Work with Office Files and PDF

Reply locked
This thread has been viewed 902 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 12, 2018 20:30:36.