[xwiki-users] $xwiki not always available?

Erin Schnabel erin.xwiki at ebullientworks.com
Sat Jul 28 06:24:03 CEST 2007


On 7/19/07, Thomas Drevon <thomas.drevon at intermedia.uio.no> wrote:
> Hi!
>
> I've made a panel that displays tags in a tag cloud (size relative to
> occurrence). But on a few occasions I get an exception, and I've managed
> to pinpoint fairly accurately under what circumstance. The code
> involved, that usually works is this:

[snip...]

> Method sort threw exception for reference $xwiki in template XWiki.XWikiLogin at  [6,24]
> org.apache.velocity.exception.MethodInvocationException: Invocation of method 'sort' in
> class com.xpn.xwiki.api.XWiki threw exception java.lang.NullPointerException

[snip..]

> Caused by: java.lang.NullPointerException
>        at java.util.Collections.sort(Collections.java:116)
>        at com.xpn.xwiki.api.XWiki.sort(XWiki.java:1676)


I've seen a few of these, and it doesn't really have anything to do
with the $xwiki reference (though perhaps the code should be changed
to avoid that misleading message..). The original cause is an NPE
originating out of Collections.sort, which is called by XWiki.sort:

XWiki.sort:

    public List sort(List list)
    {
        Collections.sort(list);
        return list;
    }

Collections.sort doesn't appear to check for null:

    public static <T extends Comparable<? super T>> void sort(List<T> list) {
	Object[] a = list.toArray();
	Arrays.sort(a);

Seems to me like your panel needs to make sure your list is non-null
(not empty) before calling xwiki.sort

The recommended (from the velocity docs) way to check for non-null && not empty:

#if( "$!allTags" != '')
  #set( $allTagsSorted = $xwiki.sort($allTags))
#end

That should solve the problem.


-- 
'Waste of a good apple' -Samwise Gamgee




More information about the users mailing list