I’ve got a subclass of BeanItemContainer that has a simple usage flow:
Create BeanItemContainer child class
Get List of beans from data source.
Call addAll(…) to fill the container
Because of the flow of the application and some design goals, I don’t use the BeanItemContainer(Class, Collection) constructor.
My add method looks like this:[code]
public boolean add(List<?> items)
{
try {
if (null != items && ! items.isEmpty())
addAll((Collection<? extends T>) items) ;
return true;
}
catch (Exception e)
{
if (null != items)
System.err.println ("Unable to add " + items.size() + " items of type " + dataClass.getSimpleName() + " to " + getClass().getSimpleName()) ;
return false ;
}
}
[/code]
However, despite adding a valid List, which is not null and has one or more items, I still get Null Pointer exception.
This works in the cases where the full list of beans is returned. If I create a new container instance, and attempt to fill it with a valid list of partial entries, for example,
all issues for one company, then I get a NPE for no apparent reason.
The code is the same, the objects being processed are the same, the only difference is the query that fills the list is slightly different, but the resultant list is valid and
has the correct beans.
I’ve not seen any documentation to describe why this may be happening. If anyone knows, or can point me in the right direction, please let me know.
Thanks in advance,
Anthony
Hi, I don’t see any particular problem with your code. The addAll() method
works for me , but you could have some special problem. I wonder if the cast can make the list null if it’s wrong type… The stack trace of the NPE should tell more about where it occurs. Using a debugger would give even more details…
Thanks Marko,
I did debug extensively before posting and can’t seem to see any issues either. The objects are of the correct type. The odd thing is it works in one instance and the not in another.
Is the fact I’m sub-classing the BeanItemContainer a possible issue? Is there some internal state that could cause this?
Regards,
Anthony
Make sure your beans have suitable implementations of equals() and hashCode() - I suspect that is where the problem lies.
If you cannot or don’t want to modify those, use BeanContainer instead of BeanItemContainer and either manually selected item ids or a suitable BeanIdResolver.
Thanks Henri, my original post which the forum threw away with a database problem message noted that I have those done.
I’ve solve the problem. I had to attach the source jar (Vaadin 6.8.12) and dive into the place where the error occurred. I had made a small change to a method which I renamed to “isFiltered” which has a name clash with a prior method. The compiler didn’t catch it and neither did I. Renamed the method, problem solved.