You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Balkan Guler <ce...@gmail.com> on 2005/08/15 15:59:22 UTC

null pointer exception

hello,
 I have a action form operForm and List operMenus field. operMenu is an 
other class that has some field.
I am listing all of the operMenu objects and their fields.
At the end of the each row I have an edit link that sends, its row's primary 
key to an action. 
operMenu class has two primary key and I instantiate a hashmap and put the 
primary keys into hashmap.
In action class I want to get the hashmap but I am taking null pointer 
exception. 
 <logic:iterate id="omenu" indexId = "count" name="operForm" 
property="operMenus">
<tr >
<td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property 
=" compositeId.projectname"/></td>
<td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property 
="compositeId.seqno"/></td>
<td class ='row<%= count.intValue() %2 %>'> <bean:write name="omenu" 
property ="parentseqno"/></td>
<td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property 
="groupseqno"/></td> 
<bean:define id="projectName" name="omenu" property="compositeId.projectname
"/>
<bean:define id="seqNo" name="omenu" property="compositeId.seqno "/> 
<%java.util.HashMap key = new java.util.HashMap(); 
key.put("projectname", projectName);
key.put("seqno", seqNo);
pageContext.setAttribute("key",key);%>
<td class = 'row<%=count.intValue() %2 %>'><html:link action ="edit" 
paramId="index" paramName="key">Edit</html:link></td> 
</tr>
</logic:iterate>
-*************************************************************
editAction:::::::::::::::
  public ActionForward execute(ActionMapping mapping, ActionForm form, 
HttpServletRequest request, HttpServletResponse response) throws Exception {

java.util.HashMap keymap = (java.util.HashMap)request.getAttribute("key");

String projectname = (String)keymap.get("projectname");
String seqno = (String)keymap.get("seqno"); 

System.out.println(projectname);
return mapping.findForward(FORWARD_detail);
}
 And the error message:
 java.lang.NullPointerException
at actions.editAction.execute(editAction.java:26)
 What is the problem??

Re: null pointer exception

Posted by Michael Rasmussen <ra...@gmail.com>.
I'm really not sure I see what it is you are trying to do.  I don't
see any submit buttons in your jsp...so I don't see what is being
submitted on a form click.  Also I don't see you writing to any fields
that will get submitted to your bean.  There is also no attempt to
pull anything out of your form bean in the Action.  I don't mean to
sound condescending, but I am wondering if you have had much
experience with Struts?  If not...you should find a good book (there
are many good recommendations in the list archives) and if so...you
may be a little confused about how a form bean works (you should take
another look at this).  I only say this because the list is not the
quickest way to get answers about the basics of Struts.  There are
many good tutorials/books out there that will get you up to speed much
faster than we can.

As far as why you get the last seqNo...your loop creates a new
instance of HashMap every time through and assigns it to the "key"
key.  Every time you do that you overwrite it, so the only one that
actually stays in the session is the last one.  Then you pull it out
of session in your action and it is the only one there, so it is the
one you get.

razmaspaz

On 8/16/05, Balkan Guler <ce...@gmail.com> wrote:
> Thanks
> I changed it to session.setAttribute and fixed it,
>  But now I have another problem even I push the edit link of the second or
> third or any row,
>  the "projectname" and "seqno" of the last row is sent to the action.
>  What can I do?
> 
>  On 8/15/05, Michael Rasmussen <ra...@gmail.com> wrote:
> >
> > Balkan,
> >
> > which line is line 26 in edit action? Can you provide the entire stack
> > trace?
> >
> > Off hand I would guess that your call to request.getAttribute("key")
> > is returning null...I don't see any other likely suspects.
> >
> > Your JSP does this:
> >
> > pageContext.setAttribute("key",key); what is pageContext?
> >
> > if you are putting a variable in page scope, it will not be available
> > for the action when you repost to the server...you have a new page
> > scope (which is different from request scope) when you come back to
> > the server. If you want a variable to live across multiple trips to
> > the server, try putting it in the session.
> >
> > On 8/15/05, Balkan Guler <ce...@gmail.com> wrote:
> > > hello,
> > > I have a action form operForm and List operMenus field. operMenu is an
> > > other class that has some field.
> > > I am listing all of the operMenu objects and their fields.
> > > At the end of the each row I have an edit link that sends, its row's
> > primary
> > > key to an action.
> > > operMenu class has two primary key and I instantiate a hashmap and put
> > the
> > > primary keys into hashmap.
> > > In action class I want to get the hashmap but I am taking null pointer
> > > exception.
> > > <logic:iterate id="omenu" indexId = "count" name="operForm"
> > > property="operMenus">
> > > <tr >
> > > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu"
> > property
> > > =" compositeId.projectname"/></td>
> > > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu"
> > property
> > > ="compositeId.seqno"/></td>
> > > <td class ='row<%= count.intValue() %2 %>'> <bean:write name="omenu"
> > > property ="parentseqno"/></td>
> > > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu"
> > property
> > > ="groupseqno"/></td>
> > > <bean:define id="projectName" name="omenu" property="
> > compositeId.projectname
> > > "/>
> > > <bean:define id="seqNo" name="omenu" property="compositeId.seqno "/>
> > > <%java.util.HashMap key = new java.util.HashMap();
> > > key.put("projectname", projectName);
> > > key.put("seqno", seqNo);
> > > pageContext.setAttribute("key",key);%>
> > > <td class = 'row<%=count.intValue() %2 %>'><html:link action ="edit"
> > > paramId="index" paramName="key">Edit</html:link></td>
> > > </tr>
> > > </logic:iterate>
> > > -*************************************************************
> > > editAction:::::::::::::::
> > > public ActionForward execute(ActionMapping mapping, ActionForm form,
> > > HttpServletRequest request, HttpServletResponse response) throws
> > Exception {
> > >
> > > java.util.HashMap keymap = (java.util.HashMap
> > )request.getAttribute("key");
> > >
> > > String projectname = (String)keymap.get("projectname");
> > > String seqno = (String)keymap.get("seqno");
> > >
> > > System.out.println(projectname);
> > > return mapping.findForward(FORWARD_detail);
> > > }
> > > And the error message:
> > > java.lang.NullPointerException
> > > at actions.editAction.execute(editAction.java:26)
> > > What is the problem??
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Fwd: null pointer exception

Posted by Balkan Guler <ce...@gmail.com>.
Thanks 
I changed it to session.setAttribute and fixed it,
 But now I have another problem even I push the edit link of the second or 
third or any row,
 the "projectname" and "seqno" of the last row is sent to the action.
 What can I do?

 On 8/15/05, Michael Rasmussen <ra...@gmail.com> wrote: 
> 
> Balkan,
> 
> which line is line 26 in edit action? Can you provide the entire stack 
> trace?
> 
> Off hand I would guess that your call to request.getAttribute("key")
> is returning null...I don't see any other likely suspects.
> 
> Your JSP does this:
> 
> pageContext.setAttribute("key",key); what is pageContext?
> 
> if you are putting a variable in page scope, it will not be available
> for the action when you repost to the server...you have a new page
> scope (which is different from request scope) when you come back to
> the server. If you want a variable to live across multiple trips to
> the server, try putting it in the session.
> 
> On 8/15/05, Balkan Guler <ce...@gmail.com> wrote:
> > hello,
> > I have a action form operForm and List operMenus field. operMenu is an
> > other class that has some field.
> > I am listing all of the operMenu objects and their fields.
> > At the end of the each row I have an edit link that sends, its row's 
> primary
> > key to an action.
> > operMenu class has two primary key and I instantiate a hashmap and put 
> the
> > primary keys into hashmap.
> > In action class I want to get the hashmap but I am taking null pointer
> > exception.
> > <logic:iterate id="omenu" indexId = "count" name="operForm"
> > property="operMenus">
> > <tr >
> > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" 
> property
> > =" compositeId.projectname"/></td>
> > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" 
> property
> > ="compositeId.seqno"/></td>
> > <td class ='row<%= count.intValue() %2 %>'> <bean:write name="omenu"
> > property ="parentseqno"/></td>
> > <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" 
> property
> > ="groupseqno"/></td>
> > <bean:define id="projectName" name="omenu" property="
> compositeId.projectname
> > "/>
> > <bean:define id="seqNo" name="omenu" property="compositeId.seqno "/>
> > <%java.util.HashMap key = new java.util.HashMap();
> > key.put("projectname", projectName);
> > key.put("seqno", seqNo);
> > pageContext.setAttribute("key",key);%>
> > <td class = 'row<%=count.intValue() %2 %>'><html:link action ="edit"
> > paramId="index" paramName="key">Edit</html:link></td>
> > </tr>
> > </logic:iterate>
> > -*************************************************************
> > editAction:::::::::::::::
> > public ActionForward execute(ActionMapping mapping, ActionForm form,
> > HttpServletRequest request, HttpServletResponse response) throws 
> Exception {
> >
> > java.util.HashMap keymap = (java.util.HashMap
> )request.getAttribute("key");
> >
> > String projectname = (String)keymap.get("projectname");
> > String seqno = (String)keymap.get("seqno");
> >
> > System.out.println(projectname);
> > return mapping.findForward(FORWARD_detail);
> > }
> > And the error message:
> > java.lang.NullPointerException
> > at actions.editAction.execute(editAction.java:26)
> > What is the problem??
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

Re: null pointer exception

Posted by Michael Rasmussen <ra...@gmail.com>.
Balkan,

which line is line 26 in edit action? Can you provide the entire stack trace?  

Off hand I would guess that your call to request.getAttribute("key")
is returning null...I don't see any other likely suspects.

Your JSP does this:

pageContext.setAttribute("key",key);  what is pageContext?

if you are putting a variable in page scope, it will not be available
for the action when you repost to the server...you have a new page
scope (which is different from request scope) when you come back to
the server.  If you want a variable to live across multiple trips to
the server, try putting it in the session.

On 8/15/05, Balkan Guler <ce...@gmail.com> wrote:
> hello,
>  I have a action form operForm and List operMenus field. operMenu is an
> other class that has some field.
> I am listing all of the operMenu objects and their fields.
> At the end of the each row I have an edit link that sends, its row's primary
> key to an action.
> operMenu class has two primary key and I instantiate a hashmap and put the
> primary keys into hashmap.
> In action class I want to get the hashmap but I am taking null pointer
> exception.
>  <logic:iterate id="omenu" indexId = "count" name="operForm"
> property="operMenus">
> <tr >
> <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property
> =" compositeId.projectname"/></td>
> <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property
> ="compositeId.seqno"/></td>
> <td class ='row<%= count.intValue() %2 %>'> <bean:write name="omenu"
> property ="parentseqno"/></td>
> <td class ='row<%=count.intValue() %2 %>'> <bean:write name="omenu" property
> ="groupseqno"/></td>
> <bean:define id="projectName" name="omenu" property="compositeId.projectname
> "/>
> <bean:define id="seqNo" name="omenu" property="compositeId.seqno "/>
> <%java.util.HashMap key = new java.util.HashMap();
> key.put("projectname", projectName);
> key.put("seqno", seqNo);
> pageContext.setAttribute("key",key);%>
> <td class = 'row<%=count.intValue() %2 %>'><html:link action ="edit"
> paramId="index" paramName="key">Edit</html:link></td>
> </tr>
> </logic:iterate>
> -*************************************************************
> editAction:::::::::::::::
>   public ActionForward execute(ActionMapping mapping, ActionForm form,
> HttpServletRequest request, HttpServletResponse response) throws Exception {
> 
> java.util.HashMap keymap = (java.util.HashMap)request.getAttribute("key");
> 
> String projectname = (String)keymap.get("projectname");
> String seqno = (String)keymap.get("seqno");
> 
> System.out.println(projectname);
> return mapping.findForward(FORWARD_detail);
> }
>  And the error message:
>  java.lang.NullPointerException
> at actions.editAction.execute(editAction.java:26)
>  What is the problem??
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org