Solved

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

Posted on 2004-04-27
12
1,085 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with LDAp login using jsp 6 115
love6 challenge java 31 133
countXY challenge 28 152
ejb on wildfly 5 20
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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