aspose file tools*
The moose likes Servlets and the fly likes Adding obj in Jbeans & retriving into same servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Adding obj in Jbeans & retriving into same servlet" Watch "Adding obj in Jbeans & retriving into same servlet" New topic
Author

Adding obj in Jbeans & retriving into same servlet

Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi All

I've doubt.May be very simple for all experts. And i tried to overcome it. But i could not. I have a servlet. Say ServCtrl. I've JavaBeans say MyBean.

Now Servlet ServCtrl calls javaBeans (MyBean's) method & passes request & response object to the same. In Javabeans i'm adding a vector to request object as req.setAttribute("List",vect); vector contains some String objects.

After calling to this beans i'm retriving this vector object as vct = (Vector)req.getAttribute("List"). After this i'm retriving String object as str=(String)vct.get(1); Now i want to print it. but this time servlet is throwing NullPointerException. I checked it that it is throwing exception at this line only.

So is it possible to add attributes to request object in javabeans & retrive it in the same calling servlet.

Thanks in advance
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Navin Pillu:
After calling to this beans i'm retriving this vector object as vct = (Vector)req.getAttribute("List"). After this i'm retriving String object as str=(String)vct.get(1); Now i want to print it. but this time servlet is throwing NullPointerException. I checked it that it is throwing exception at this line only.


It should work. Try printing just vct before printing the str. That will tell you if the attribute was available or not.


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


i'm retriving this vector object as vct = (Vector)req.getAttribute("List"). After this i'm retriving String object as str=(String)vct.get(1); Now i want to print it. but this time servlet is throwing NullPointerException. I checked it that it is throwing exception at this line only.


1. Did you check if the bean method does add String objects to the vector

2. Do not access the elements of a vector directly (atleast check for size). Better use an iterator.

3. Consider using an ArrayList (not that it would auto solve problems 1 & 2 above).

cheers,
ram.
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi All

It is not returning object of Vector class. code that i wrote in the JavaBean is as follows


Please can anybody tell me what might be the reason?

Thanks in Advance
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi All

All these are now working fine.

Thanks for all valuable suggestions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding obj in Jbeans & retriving into same servlet