Solved

Error java.lang.NullPointerException at mypackage.delete(Unknown Source)

Posted on 2004-04-27
12
1,084 Views
Last Modified: 2011-08-18
Hi to All,
I have a P021.jsp page through which I am calling my class which extends DispatchAction of struts.
While calling the first method (My method) select(...) its working fine but upon calling the next delete(...) it is giving error.
My class is like :::

public class SchoolAction extends org.apache.struts.actions.DispatchAction{
 public  ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
   
   
         String check0=request.getParameter("check0");
         System.out.println("Check0 id is %%%%%%%%%%%% "+check0);
    schoolForm sform=(schoolForm)form;
             
    String sch_code=sform.getSch_code();
    String sch_select=sform.getSch_select();
   
   
      javax.sql.DataSource dataSource;
      java.sql.Connection con=null;
      CommonBean_P common_p21=new CommonBean_P();
      
    StringBuffer sqlquery;
    String scode_p21="",skind_p21="",sname_p21="";
    int veci=0;// size of vector
    String buffquery;
   
       
    if(sch_code.equals(null) || sch_code.equals(""))
      buffquery="SELECT * FROM table WHERE SchoolKind='"+sch_select+"' and NOT RegistType = '2'" ;
    else
    {
    if(sch_code.length()<7)
    buffquery="SELECT * FROM Table WHERE SchoolKind='"+sch_select+"' and SchoolCode LIKE '"+sch_code+"%' and RegistType <> '2'";
    else
    buffquery="SELECT * FROM Table WHERE SchoolKind='"+sch_select+"' and SchoolCode='"+sch_code+"' and RegistType <> '2'";
    }
      sqlquery=new StringBuffer(buffquery);
  try {
            
              dataSource = getDataSource(request);
              con = dataSource.getConnection();
              ResultSet rs=null;

              Statement stmt=con.createStatement();
              rs=stmt.executeQuery(sqlquery.toString());
              
              while(rs.next())
              {
                      scode_p21=rs.getString("SchoolCode");
                      skind_p21=rs.getString("SchoolKind");
                      sname_p21=rs.getString("SchoolName");
                      
                      System.out.println("SchoolCode >> "+scode_p21+" SchoolKind >> "+skind_p21+" SchoolName >> "+sname_p21);
                      veci++;
                      common_p21.setNumber(veci);
                      common_p21.setSchoolCode(scode_p21);
                      common_p21.setSchoolKind(skind_p21);
                      common_p21.setSchoolName(sname_p21);                      
          }
              
              rs.close();
              HttpSession sess=request.getSession();
              sess.setAttribute("common_p21",common_p21);
              
             } catch (SQLException sqle) {
          getServlet().log("Connection.process", sqle);
          } finally {
             //to make sure the connection is closed
          try {
       con.close();
          } catch (SQLException e) {getServlet().log("Connection.close", e);}
      }
   ActionForward forward=mapping.findForward("P021");
      return forward;            
 }


 public ActionForward delete(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
    {
    String check=request.getParameter("check");
       
    schoolForm sform=(schoolForm)form;
   
    javax.sql.DataSource dataSource=null;
     System.out.println("Chek is DataSorce "+dataSource);
      java.sql.Connection con=null;
      
      HttpSession sess=request.getSession();
        CommonBean_P common_p21=(CommonBean_P)sess.getAttribute("common_p21");
        String sch_code=sform.getSch_code();
                String sch_select=sform.getSch_select();
   
      int selRow=sform.getSelRow();//Selected row from display
                int veci=0;// size of vector
      
        StringBuffer sqlquery;
       String buffquery="";
       String srow=common_p21.getSchoolCode(selRow);
        buffquery="UPDATE PresentSchoolMTR SET RegistType='2',SendKind='0',DelDate='NOW' WHERE SchoolCode='"+srow+"'";
        sqlquery=new StringBuffer(buffquery);
       
     try {
            
              dataSource = getDataSource(request);
              System.out.println("Chek  datasource "+dataSource);
              con = dataSource.getConnection();
              System.out.println("Chek  Connection "+con);
              ResultSet rs=null;

              Statement stmt=con.createStatement();
              System.out.println("Chek  Statement "+stmt);
              int uprows=stmt.executeUpdate(sqlquery.toString());
              System.out.println("Rows UPDATE BY THIS QUERY are ------------->"+uprows);
        
          rs.close();
              
              }catch (SQLException sqle) { getServlet().log("Connection.process", sqle);}
               finally {
             //to make sure the connection is closed
          try {
             con.close();
               } catch (SQLException e) {getServlet().log("Connection.close", e);}
      }
 
   return mapping.findForward("delete-success");
        }
}

And in my P021.jsp page

<html:form action ="/P021.do?check0=select" scope="session">
<html:submit >Select</html:submit>
</html:form>

<html:form action="/delete-action?check=delete">
 <html:submit property="del" onclick="deleteRow()">
      Delete
 </html:submit>
</html:form>

And in My struts-config.xml
<action input="/login/P021.jsp" name="schoolForm" parameter="check0"

   path="/P021" scope="session" type="package.schoolAction">

   <forward name="P021" path="/P021.jsp"/>

  </action>
 <action input="/login/P021.jsp" name="schoolForm" parameter="check"

   path="/delete-action" scope="session"

   type="package.schoolAction" validate="false">

   <forward name="delete-success" path="/login/P021.jsp"/>


After calling the delete action my url shows
http://localhost:8080/delete-action.do?check=delete

And giving the below error
===========================================================
javax.servlet.ServletException
      at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
      at java.lang.Thread.run(Thread.java:534)


root cause

java.lang.NullPointerException
      at package.schoolAction.delete(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
      at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
      at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
      at java.lang.Thread.run(Thread.java:534)


===========================================================
Please help me to find out the problem.

Any help is fine for me.

Thanks in advance.
0
Comment
Question by:m_sharma98
  • 7
  • 4
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 10926828
> at package.schoolAction.delete(Unknown Source)

Can you compile your code up with debug turned on (-g) ?

Then it will tell you what line the error is on...

I'll have a look and see if there's anything obvious ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10926835
> ResultSet rs=null;

then

> rs.close() ;

you never set rs to anything!!!

Get rid of both lines :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10926838
If that doesn't fix it, then

>  CommonBean_P common_p21=(CommonBean_P)sess.getAttribute("common_p21");

is returning null...
0
 
LVL 7

Expert Comment

by:searlas
ID: 10926842
Is common_p21 null when you get it out of the session.  Is getDataSource(request) returning null?

Was there a database issue, covered up by the fact you do a con.close() in a try/catch(SQLException) block, which could actually be throwing a NullPointerException if con is null...?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10926854
:-)
0
 

Author Comment

by:m_sharma98
ID: 10927967
No,
CommonBean_P common_p21=(CommonBean_P)sess.getAttribute("common_p21");

is not returning NULL i have checked it properly and my code is runing well and UPDATING the table also but after it ,I mean at forwarding it is giving Error.
It should forward the control to the package.schoolAction servlet by getting the mapping from "delete-success" via URL http://localhost:8080/delete-action.do?check=delete 
after it should redirect control to the package.schoolAction servlet  by mapping P021.do

one of my mistake is
<forward name="delete-success" path="/login/P021.jsp"/>
will become like
<forward name="delete-success" path="/P021.do"/>

But unfortunately not happening


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 10928011
what about like I said before:

------------

 > ResultSet rs=null;

then

> rs.close() ;

you never set rs to anything!!!

Get rid of both lines :-)

-------------

Did that help?
0
 

Author Comment

by:m_sharma98
ID: 10928048
No it didn't effected anything.
Getting same error
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10928135
Did you compile with debug turned on, so you can see which line is causing it?
0
 

Author Comment

by:m_sharma98
ID: 10934777
Yeah I compiled with -g but no errors.
0
 

Author Comment

by:m_sharma98
ID: 10934922
Thanks TimYates
Now it is working well.
I removed rs coding and changed the mapping also.
anyway thanks a lot
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10936608
Glad you got it working :-)

Tim
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
J2SE 5.0 metadata 1 51
reflection example 3 92
memory representation of int[][][] a=new int[2][][]; 10 59
JSONSerializer.toJSON in JSP generating error 8 64
Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now