This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getAttributes return NullPointerException  RSS feed

 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have the following situation:
JSP page with form with checkboxes showing items from some ArrayList (call this aList1). When submit, servlet checks if it is this particular form and check if the checkboxes are selected. If so assign a "1" into an array list of type ArrayList<Integer> (call this aList2), else assign "0". After assigning values to aList2, setAttribute for aList1 and aList2 and forward it to the JSP that edit the items in aList1.

The problem is given my servlet is fine, in the JSP page I getAttribute for aList1 and aList2. If aList2 is ignored, everything works fine - meaning I can see the items of aList1 in the edit form. Once I put in aList2, it prompts NullPointerException. I also checked that inside the servlet that aList2 indeed has the values 1 or 0. So whatever getAttribute returns is null.

My code for getAttribute for aList1 and aList2 is


I understand that request.getAttribute() returns a Object and need casting to in my case ArrayList. If the aList1 works, why doesn't aList2? Does it have anything to do the Integer wrapper class?

Also assuming aList2 isn't null, I also need to check value of each index for "1" or "0". I use aList2.get(i) inside for looping aList1's size to get the particular item. But if I use toString(), it won't gives the value 1 or 0 but some weird stuff.

So in this case, do I need to do a class to extend the Integer to get the value so I can do something like aList2.get(i).getIndex() to check for 1 or 0 or is there a simpler way? By the way, is aList2.get(i) is an Integer object right?

Help is greatly appreciated? Thanks
 
Narendra Kumar Pinnaka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess you can use request.getAttribute only to get single object. as it returns a value as object.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Narendra Kumar Pinnaka:
i guess you can use request.getAttribute only to get single object. as it returns a value as object.

Not correct. A List is an object and can be used as a scoped variable (attribute) without problem. The choice of a List is not the poster's issue.
[ April 22, 2008: Message edited by: Bear Bibeault ]
 
K. Tsang
Bartender
Posts: 3628
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem it was a TYPO. It works now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!