Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding obj in Jbeans & retriving into same servlet

 
Navin Pillu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

All these are now working fine.

Thanks for all valuable suggestions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic