tags:

views:

98

answers:

1

I am new to Java (coming from c#) and I am trying to figure out the problem with this code. It should just be inserting a record into a SQL Server database.

try
{
    getConnection(true);
    cstmt = conn.prepareCall("{ call dbo.usp_insert_LeadSubmissionDistributor(?, ?, ?, ?, ?, ?, ?) }");

    if (distRepName == null)
    {
        cstmt.setNull(1, Types.NVARCHAR);
    }
    else
    {
        cstmt.setString(1, distRepName);
    }

    if (distRepPhone == null)
    {
        cstmt.setNull(2, Types.NVARCHAR);
    }
    else
    {
        cstmt.setString(2, distRepPhone);
    }

    if (distributorName == null)
    {
        cstmt.setNull(3, Types.NVARCHAR);
    }
    else
    {
        cstmt.setString(3, distributorName);
    }

    if (emailAddress == null)
    {
        cstmt.setNull(4, Types.NVARCHAR);
    }
    else
    {
        cstmt.setString(4, emailAddress);
    }

    cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
    cstmt.registerOutParameter(6, java.sql.Types.INTEGER);
    cstmt.registerOutParameter(7, java.sql.Types.NVARCHAR);

    cstmt.execute(); //null pointer exception thrown here

    cookieId = cstmt.getString(5);
}
catch(Exception e)
{
    LogUtility.error(className, e.getMessage(), e);
}

When I call the proc manually it functions perfectly. All of the input parameters are present and within limits. All the parameters match their types in the sproc.

Why do I get a null pointer exception when cstmt.execute() is called?

Here is the stack trace:

java.lang.NullPointerException
    at com.microsoft.sqlserver.jdbc.AppDTVImpl$SetValueOp.executeDefault(Unknown Source)
    at com.microsoft.sqlserver.jdbc.DTV.executeOp(Unknown Source)
    at com.microsoft.sqlserver.jdbc.AppDTVImpl.setValue(Unknown Source)
    at com.microsoft.sqlserver.jdbc.DTV.setValue(Unknown Source)
    at com.microsoft.sqlserver.jdbc.Parameter.getTypeDefinition(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.buildParamTypeDefinitions(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildPreparedStrings(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PreparedStatementExecutionRequest.executeStatement(Unknown Source)
    at com.microsoft.sqlserver.jdbc.CancelableRequest.execute(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeRequest(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(Unknown Source)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:169)
    at com.welchAllyn.persistence.LeaderDefaultDataDao.saveData(LeaderDefaultDataDao.java:64)
    at com.welchAllyn.application.LeaderSubmissionHandler.getDefaultData(LeaderSubmissionHandler.java:449)
    at org.apache.jsp.apps.partners.leader_005fsubmission_jsp._jspService(leader_005fsubmission_jsp.java:159)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)
A: 

if you are doing an insert you have to use executeUpdate(), instead of execute().

punkers
even if its the stored proc doing the inserting? I mean for what its worth the stored proc could be doing anything it wants
Mr Bell
I tried changing the execute to executeUpdate as you suggested and it has no effect
Mr Bell
sorry my bad it wouldn't make a difference, you are right. did u manage to get the stack trace?
punkers
yup. just added it
Mr Bell
the trace doesn't really help. My guess is that there is a problem with the connection settings for your java code. you can debug that class and check if you have a valid connection object.
punkers