This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes help: null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help: null pointer exception" Watch "help: null pointer exception" New topic
Author

help: null pointer exception

elli dian
Greenhorn

Joined: May 18, 2006
Posts: 15
I am new programmer in java.
Iwant to call a stored procedure, but I get error:class java.lang.NullPointerException
please help me 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();
System.out.println("start");
try
{
System.out.println("Calling a procedure");
CallableStatement cs=conn.prepareCall("{call submitClockingData(?,?,?,?)}");

cs.setString(1,"DX");
System.out.println("success.....");
cs.setString(2,"DX005010001");
cs.setString(3,date1);
cs.setInt(4,1);
cs.execute();

}catch(SQLException err)
{
System.out.println("Terjadi kesalahan1 "+err.getClass());
}catch(Exception er)
{
System.out.println("Terjadi kesalahan2 "+er.getClass());
}
finally
{
System.out.println("finally");
conn.close();
}
Calvin Smith
Greenhorn

Joined: Aug 05, 2005
Posts: 8
Your problem would be much easier to debug if you included the exact stack trace.

In general, NullPointerException occurs when you try to invoke a method of what you think is a reference to an object but is really a null reference.

For example, the following code would produce a NullPointerException:



In this case, the stack trace would have the line with println on it at the top of the trace, because that is where the NullPointerException is thrown. The null reference is not a reference to an object, so there are no methods that can be invoked on it; in particular, the name.length() in the above example is what triggers the NullPointerException here.

The Javadocs for NullPointerException have more information, and are at: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html
[ May 18, 2006: Message edited by: Calvin Smith ]
Calvin Smith
Greenhorn

Joined: Aug 05, 2005
Posts: 8
In your case, the problem is likely getConnection, which swallows the exception:



If anything inside that method throws an exception, your calling method will get the null returned, and you will get a NullPointerException when you try to invoke a method on that null reference.
elli dian
Greenhorn

Joined: May 18, 2006
Posts: 15
In my program my connection return null.
So what I will return in getConnection, because it's type is connection.

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;
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See response from EFH under duplicate post (now closed).


"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
abhishek jain
Greenhorn

Joined: Aug 18, 2004
Posts: 4
Hi,

Send your complete Stack Trace ... if possible with complete fraction of code.

Also, as you asked what you should return ... should rewrite your method as (a simple change) ..

public Connection getConnection() throws NamingException, SQLException {
InitialContext cont = new InitialContext();
DataSource ds = (DataSource) cont.lookup("jdbc/ds2");
return ds.getConnection("ess_adm", "adm_ess");
}

Also, now don't forget to do the Exception handling from the method you call this method.
 
GeeCON Prague 2014
 
subject: help: null pointer exception