Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot Find Bean in Any Scope?

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:


There is a property threadLastPostDate in the ThreadBean and the value of that property is not a null. And the threadLastPostDate is saved in the database in the GMT.

Now, I want to display the time.

The OnlineUser is an interface. And the OnlineUserImpl is an implementation of that interface. The getGMTTimestampFormat is a method.

In OnlineUser.java, the method looks like:

In OnlineUserImpl.java, the method looks like:


In my JSP,


Nonetheless, I got an error message saying that there is a null:

----- Root Cause -----
java.lang.NullPointerException
at org.apache.artimus.authorization.OnlineUserImpl.getGMTTimestampFormat(OnlineUserImpl.java:128)
at org.apache.artimus.authorization.OnlineUserImpl.getGMTTimestampFormat(OnlineUserImpl.java:118)

[ June 12, 2004: Message edited by: JiaPei Jen ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your useBean tag - what did you set the scope at?
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, the problem is with my JavaBean. Let me explain the problem I am dealing with:

1. I have a JavaBean, which is named as ThreadBean with lots of properties.

2. I have a ListThread.java class, which is a servlet . In that class, I created a collection of beans. Each of those beans is a ThreadBean.

3. By the end of the ListThread.java, I passed those beans to my JSP in the request scope this way:

request.setAttribute( "ThreadBeans", beans );

4. In my JSP (I did import the ThreadBean), I have

5. when I tried to write out the properties; for example:


I got the error message:

----- Root Cause -----
javax.servlet.ServletException: Cannot find bean threadBean in any scope
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what did your <jsp:useBean statement declare the scope to be in your JSP?
Also, it looks to me like your threadBean variable only has scope inside the while loop.
ThreadBean is in a package, right?
Bill
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ThreadBean is in a package. And yes, I try to write out the properties of threadBean inside the "while" loop.

My JSP receives a collection of ThreadBean(s) from a request scope. I do not know why my request scope is lost in the "while" loop.

1. Where should I put <jsp:useBean .....> ?
2. Why should I put scope inside the <jsp:useBean ....> ?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>1. Where should I put <jsp:useBean .....> ?
I normally try to do that right after the <%@ page declaration.
As far as I know, if you don't declare that a particular variable is a bean, JSP won't use bean methods with it.

2. Why should I put scope inside the <jsp:useBean ....> ?
Because if you don't specify scope, you get the default which is "page" meaning that the bean has the shortest possible lifetime - possibly not what you want.

I suggest:
A. download the JSP API from Sun - note that there is a cool reference card at http://java.sun.com/products/jsp/docs.html - I keep a copy handy at all times. Also see the tutorial at that location.
B. look at the translated servlet java code resulting from your JSP to see what it is actually trying to execute.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!