• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp:useBean and jsp:setProperty question

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have the following code:


According to this there is a conditional statement when putting the <jsp:setProperty/> inside the body of <jsp:useBean/>

In my mind this means that if I don't set this property in the request like this:



The resulting value should be "Fred", but I get a nullpointer exception...

Why?

[ January 05, 2007: Message edited by: Henrik Engert ]
[ January 05, 2007: Message edited by: Henrik Engert ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "code" between of the <jsp:useBean> and </jsp:useBean> tags only gets evaluated if there is no bean of the specified class in the specified scope. This may not be the best analogy, but I tend to think of it as sort of a constructor for the bean if it isn't there. I suspect that in your case the bean is there, but that particular property of the bean is null. So the code to populate that property does not get evaluated (the bean already exists, so the conditional "creation" code between the jsp:useBean tags isn't executed).

I recommend looking into Expression Language (EL) and the JSTL core library tags for conditionals that you can use to see whether the property has been populated or not. There may be other ways to do it as well (especially if you're using Struts), but I'm a huge EL fan (it generally seems much tidier and more intuitive to me as a programmer) so that's what I'm recommending
[ January 05, 2007: Message edited by: Joel Jorgensen ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you testing this?

In your useBean tag, you've set the scope to "page".
In the Java code, you're binding the person object to "request".
 
Henrik Engert
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

I guess what I am asking is the following:

If I use the following code:



and I don't create a person to set with request.setAttribute("person", p);

I would think I would get Fred as a result.....

[ January 08, 2007: Message edited by: Henrik Engert ]
[ January 08, 2007: Message edited by: Henrik Engert ]
 
Joel Jorgensen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henrik Engert:
Hi again,

I guess what I am asking is the following:

If I use the following code:



and I don't create a person to set with request.setAttribute("person", p);

I would think I would get Fred as a result.....

[ January 08, 2007: Message edited by: Henrik Engert ]

[ January 08, 2007: Message edited by: Henrik Engert ]



In this case, the problem is that you are not specifying a 'class' attribute for the new bean to have. It tries to cast it to be type com.example.model.Person, but without a 'class' attribute it doesn't know what class to use to create the new bean. If you add a class attribute of com.example.model.Person, it should resolve your null pointer exception problem (or at least get you closer
[ January 08, 2007: Message edited by: Joel Jorgensen ]
reply
    Bookmark Topic Watch Topic
  • New Topic