$xwiki not always available?
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: #set($query = "select elements(prop.list) from BaseObject as obj, DBStringListProperty as prop where obj.className='XWiki.TagClass' and obj.id=prop.id.id and prop.id.name='tags'") #set( $allTags = $xwiki.search($query)) #set( $allTagsSorted = $xwiki.sort($allTags)) If the user is not logged in, and presses the login link in the top left corner (--> /xwiki/bin/login/XWiki/XWikiLogin), the panel returns the enclosed exception. After some debugging, it seems that $allTags is not assigned a value from $xwiki.search($query). This leads me to believe that $xwiki might not be available from this particular context. Has anyone else encountered this, or is it an artifact in my environment? I'm running xwiki v1.1-milestone2, mysql, winxp, tomcat, java5. Both my db and xwiki installs are pretty fresh and un-tampered with. best regards :-) Thomas 13:47:50,211 ERROR http-9080-2 http://myHost:9080/xwiki/bin/login/XWiki/XWikiLogin?xredirect=http%3A%2F%2Fl... SimpleLog4JLogSystem:logVelocityMessage:154 - 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 @ XWiki.XWikiLogin[6,31] at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:286) at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:203) at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:419) at org.apache.velocity.runtime.parser.node.ASTExpression.value(ASTExpression.java:73) at org.apache.velocity.runtime.parser.node.ASTSetDirective.render(ASTSetDirective.java:125) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:240) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:143) at com.xpn.xwiki.render.XWikiVelocityRenderer.render(XWikiVelocityRenderer.java:91) at com.xpn.xwiki.render.DefaultXWikiRenderingEngine.renderText(DefaultXWikiRenderingEngine.java:229) at com.xpn.xwiki.render.DefaultXWikiRenderingEngine.renderText(DefaultXWikiRenderingEngine.java:161) at com.xpn.xwiki.render.DefaultXWikiRenderingEngine.renderText(DefaultXWikiRenderingEngine.java:132) at com.xpn.xwiki.doc.XWikiDocument.getRenderedContent(XWikiDocument.java:366) at com.xpn.xwiki.objects.classes.TextAreaClass.displayView(TextAreaClass.java:165) at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:1265) at com.xpn.xwiki.doc.XWikiDocument.display(XWikiDocument.java:1245) at com.xpn.xwiki.api.Document.display(Document.java:864) at sun.reflect.GeneratedMethodAccessor90.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245) at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:203) at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:294) at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:74) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:107) at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:74) at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:88) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:194) at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:170) at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:74) at org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:448) at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:170) at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:74) at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:88) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:240) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:143) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1249) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1218) at com.xpn.xwiki.api.XWiki.parseTemplate(XWiki.java:491) at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245) at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:203) at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:294) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:194) at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:170) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:240) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:143) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1249) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1218) at com.xpn.xwiki.api.XWiki.parseTemplate(XWiki.java:491) at sun.reflect.GeneratedMethodAccessor59.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245) at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:203) at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:294) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at org.apache.velocity.runtime.directive.VelocimacroProxy.render(VelocimacroProxy.java:194) at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:170) at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:74) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:107) at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:318) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:240) at com.xpn.xwiki.render.XWikiVelocityRenderer.evaluate(XWikiVelocityRenderer.java:143) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1249) at com.xpn.xwiki.XWiki.parseTemplate(XWiki.java:1218) at com.xpn.xwiki.web.Utils.parseTemplate(Utils.java:98) at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:159) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.xpn.xwiki.web.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:118) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445) at java.lang.Thread.run(Thread.java:595) Caused by: java.lang.NullPointerException at java.util.Collections.sort(Collections.java:116) at com.xpn.xwiki.api.XWiki.sort(XWiki.java:1676) at sun.reflect.GeneratedMethodAccessor110.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245) ... 99 more
On 7/19/07, Thomas Drevon <[email protected]> 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
Erin Schnabel wrote:
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
Thanks for taking the time to answer me! I see the flaw that you pointing out.
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.
I ended up doing something similar: #set( $allTags = $xwiki.search($query)) #if ( $allTags.size() > 1) ...do stuff #end However, I'm absolutely positive that my list of tags isn't empty, so this can't the ultimate cause of the problem. This was confirmed by Sergiu Dumitriu; he recently updated the jira issue concerning the inclusion of the above mentioned tag cloud in xwiki. Check it out: http://jira.xwiki.org/jira/browse/XE-51 cheers :-) Thomaas
On 7/30/07, Thomas Drevon <[email protected]> wrote: [snip..]
Thanks for taking the time to answer me! I see the flaw that you pointing out.
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.
I ended up doing something similar:
#set( $allTags = $xwiki.search($query)) #if ( $allTags.size() > 1) ...do stuff #end
However, I'm absolutely positive that my list of tags isn't empty, so this can't the ultimate cause of the problem. This was confirmed by Sergiu Dumitriu; he recently updated the jira issue concerning the inclusion of the above mentioned tag cloud in xwiki. Check it out: http://jira.xwiki.org/jira/browse/XE-51
Well, I've observed some interesting behavior with what you've just mentioned. For example, if any other place in your script also uses $allTags, and $xwiki.search($query) barfs for some reason, the other/dirty value will stay in $allTags. Also, if $allTags is empty, $allTags.size() will be the text $allTags.size(), and the log will gripe that you can't compare that to be > 1. The likelihood of that, given that your list of tags isn't empty makes all of the above unlikely of course, but it's good to bear in mind that weird behavior that is almost impossible to figure out can happen when variables aren't checked for empty. The crappy part about that is using "$var" can end up rendering the content... not a perfect solution by any means, but one that is the safest if you want non-null and not empty. #set($allTags = '') #set( $allTags = $xwiki.search($query)) #if( "$!allTags" != '' && $allTags.size() > 1) ...do stuff #end that should work every time. ;) -- 'Waste of a good apple' -Samwise Gamgee
participants (3)
-
Erin Schnabel -
Erin Schnabel -
Thomas Drevon