Connecting Tech Pros Worldwide Forums | Help | Site Map

Null pointer exception

Newbie
 
Join Date: Apr 2008
Posts: 1
#1: Apr 5 '08
Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.radesan.struts.action;

import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.radesan.struts.form.NewAdminForm;

/**
* MyEclipse Struts
* Creation date: 03-09-2008
*
* XDoclet definition:
* @struts.action path="/newAdmin" name="newAdminForm" input="/newadmin.jsp" scope="request"
* @struts.action-forward name="success" path="/admin.jsp"
*/
public class NewAdminAction extends Action {
/*
* Generated Methods
*/
int j;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
NewAdminForm newAdminForm = (NewAdminForm) form;// TODO Auto-generated method stub
int staffnumber=0;
try {
Connection cn=null;
Statement s=null;
ResultSet rs=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:radesan" );
s=cn.createStatement();
rs=s.executeQuery("select max(staffnumber) from newadmin");
if(rs.next()) {
staffnumber=rs.getInt(1);
}
rs.close();
s.close();
cn.close();
}catch(Exception e) {e.printStackTrace();}

String bloodgroup;
String dateofjoining;
try {
Connection cnn=null;
Statement cs=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cnn=DriverManager.getConnection("jdbc:odbc:radesan ");
String sno = Integer.toString(++staffnumber);

bloodgroup=newAdminForm.getB1()+newAdminForm.getB2 ();
dateofjoining=newAdminForm.getDd()+"/"+newAdminForm.getMm()+"/"+newAdminForm.getYy();
String insert="insert into newadmin values ('"+sno+"','"+newAdminForm.getFirstname()+"','"+ne wAdminForm.getLastname()+"','"+newAdminForm.getPas sword()+"','"+newAdminForm.getAuthorisation()+"',' "+newAdminForm.getStreet()+"','"+newAdminForm.getS tate()+"','"+newAdminForm.getPincode()+"','"+newAd minForm.getSex()+"','"+bloodgroup+"','"+newAdminFo rm.getAge()+"','"+newAdminForm.getLandline()+"','" +newAdminForm.getMobile()+"','"+dateofjoining+"',' "+newAdminForm.getSTATUS()+"')";
j=cs.executeUpdate(insert);
cs.close();
cnn.close();


}catch(Exception e){e.printStackTrace();}
return mapping.findForward("success");
}
}

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 5 '08

re: Null pointer exception


Quote:

Originally Posted by iamdennisthomas

Hi Guys i was developing a action servlet which is actulally getting the data from a form and putting it in the db but while executing i am getting a null pointer exception

Don't make us guess; the JVM does not simply say "null pointer exception,
bad luck", it also gives you the line number and the text of the line where that
exception was thrown; on top of that it gives you the entire stack trace.

I don't think it is too much to ask if you reproduce all that information for us here
if you want us to solve your problem.

kind regards,

Jos
Needs Regular Fix
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 511
#3: Apr 5 '08

re: Null pointer exception


Quote:

Originally Posted by JosAH

Don't make us guess; the JVM does not simply say "null pointer exception,
bad luck", it also gives you the line number and the text of the line where that
exception was thrown; on top of that it gives you the entire stack trace.

I don't think it is too much to ask if you reproduce all that information for us here
if you want us to solve your problem.

kind regards,

Jos

And i agree........

sukatoa
Expert
 
Join Date: Nov 2006
Posts: 392
#4: Apr 7 '08

re: Null pointer exception


Please use code tags when posting code.

It looks like the Statement object is never initialized. So the executeUpdate() method is being called on a a null object.
hsn's Avatar
hsn hsn is offline
Familiar Sight
 
Join Date: Sep 2007
Location: Dubai-UAE
Posts: 237
#5: Apr 8 '08

re: Null pointer exception


can you print the code again using the CODE tags it will be much simpler to read.

good luck

hsn
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#6: Apr 8 '08

re: Null pointer exception


That's one wide line ;-)
Reply