Null pointer exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iamdennisthomas
    New Member
    • Apr 2008
    • 1

    Null pointer exception

    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.str uts.action;

    import java.sql.*;
    import javax.servlet.h ttp.HttpServlet Request;
    import javax.servlet.h ttp.HttpServlet Response;
    import org.apache.stru ts.action.Actio n;
    import org.apache.stru ts.action.Actio nForm;
    import org.apache.stru ts.action.Actio nForward;
    import org.apache.stru ts.action.Actio nMapping;
    import com.radesan.str uts.form.NewAdm inForm;

    /**
    * MyEclipse Struts
    * Creation date: 03-09-2008
    *
    * XDoclet definition:
    * @struts.action path="/newAdmin" name="newAdminF orm" 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(ActionM apping mapping, ActionForm form,
    HttpServletRequ est request, HttpServletResp onse 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.J dbcOdbcDriver") ;
    cn=DriverManage r.getConnection ("jdbc:odbc:rad esan");
    s=cn.createStat ement();
    rs=s.executeQue ry("select max(staffnumber ) from newadmin");
    if(rs.next()) {
    staffnumber=rs. getInt(1);
    }
    rs.close();
    s.close();
    cn.close();
    }catch(Exceptio n e) {e.printStackTr ace();}

    String bloodgroup;
    String dateofjoining;
    try {
    Connection cnn=null;
    Statement cs=null;
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    cnn=DriverManag er.getConnectio n("jdbc:odbc:ra desan");
    String sno = Integer.toStrin g(++staffnumber );

    bloodgroup=newA dminForm.getB1( )+newAdminForm. getB2();
    dateofjoining=n ewAdminForm.get Dd()+"/"+newAdminForm. getMm()+"/"+newAdminForm. getYy();
    String insert="insert into newadmin values ('"+sno+"','"+n ewAdminForm.get Firstname()+"', '"+newAdminForm .getLastname()+ "','"+newAdminF orm.getPassword ()+"','"+newAdm inForm.getAutho risation()+"',' "+newAdminForm. getStreet()+"', '"+newAdminForm .getState()+"', '"+newAdminForm .getPincode()+" ','"+newAdminFo rm.getSex()+"', '"+bloodgroup+" ','"+newAdminFo rm.getAge()+"', '"+newAdminForm .getLandline()+ "','"+newAdminF orm.getMobile() +"','"+dateofjo ining+"','"+new AdminForm.getST ATUS()+"')";
    j=cs.executeUpd ate(insert);
    cs.close();
    cnn.close();


    }catch(Exceptio n e){e.printStack Trace();}
    return mapping.findFor ward("success") ;
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      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

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        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.

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

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

          good luck

          hsn

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            That's one wide line ;-)

            Comment

            Working...