1

I have the following code:

Object backingBean = facesContextHandler.getBackingBean("UserCredentialsBean");
UserCredentialsBean userCredentBean = (UserCredentialsBean) backingBean;

While I'm debugging it I have the following in my expressions view in Eclipse:

backingBean.getClass() -> myPackage.UserCredentialsBean

backingBean instanceOf myPackage.UserCredentialsBean -> false

So casting above fails...

How can this be?

Update: Additional "symptom": I get this issue after session timeout

Any ideas?

2
  • I reckon it could be helpful to have a stack trace. It's possible the OP missed something in his analysis, or perhaps there is more detail somewhere... There more info the better chance that someone can help Commented Apr 18, 2011 at 15:04
  • If "UserCredentialsBean" is stored in the session scope and your session times out, then it will be null. Is this what is happening? Commented Apr 18, 2011 at 15:16

2 Answers 2

4

Interesting question. I can only think of two possibilities.

1- Null Object. instanceOf usually fails for null object. Just make sure that the bean is initialized.

2- Class Loader Issue. If two objects of same class are loaded by two different class loaders, then instanceOf will fail.

This is not an all inclusive list, just two things I could think of.

Sign up to request clarification or add additional context in comments.

6 Comments

backingBean is not null. Eclipse shows its attributes as of UserCredentialsBean object with data populated.
Regarding Class Loader Issue... I should think how these could happen any ClassLoader issue.
Then you might have to look in to the class loader issue. This is JSF application right and you are trying to get a backing bean within your applcation. ??
The class loader issue is interesting, although a cast... hmm... What happens if you do a dynamic cast (using reflection). Get the Class object and then call the cast() method
Yes, correct. And one point... I get this issue after session timeout
|
2

ClassLoader issue for sure. Most probably the application got redeployed (thus new classloader instance was created), but the old object remained either in (disk-serialized?) session or in memory.

Class name is the same, but class loader instance is different. Instanceof looks at complete class name AND equality of classloaders.

P.S. This is actually a quite common issue. It is often visible when a background thread wakes up only to find out that the application was redeployed, the thread's classloader is gone and then it throws either NoClassDefFound or ClassCast to the big amusement of developers who not always realize this is actually a zombie from the previous deployment, and trying to find a bug in their code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.