You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by bhorvat <ho...@gmail.com> on 2013/03/01 00:12:08 UTC

NPE in Tree when I dont have any data

Hi all,

I have a a tree in my page and when I dont have any data Tree component
throws a NPE. Is this intentional or am I somehow implementing/using the
component in a bad way?

java.lang.NullPointerException
Filter stack frames Stack trace 
org.apache.tapestry5.corelib.components.Tree$3.render(Tree.java:151)
org.apache.tapestry5.internal.services.RenderQueueImpl.run(RenderQueueImpl.java:72)
org.apache.tapestry5.internal.services.PageRenderQueueImpl.render(PageRenderQueueImpl.java:124)
org.apache.tapestry5.internal.services.MarkupRendererTerminator.renderMarkup(MarkupRendererTerminator.java:37)
org.got5.tapestry5.jquery.services.js.JSModule$1.renderMarkup(JSModule.java:40)
org.apache.tapestry5.services.TapestryModule$31.renderMarkup(TapestryModule.java:1994)
org.apache.tapestry5.services.TapestryModule$30.renderMarkup(TapestryModule.java:1978)
org.apache.tapestry5.services.TapestryModule$29.renderMarkup(TapestryModule.java:1960)
org.apache.tapestry5.services.TapestryModule$28.renderMarkup(TapestryModule.java:1945)
org.apache.tapestry5.services.TapestryModule$27.renderMarkup(TapestryModule.java:1931)
org.apache.tapestry5.services.TapestryModule$26.renderMarkup(TapestryModule.java:1913)
org.apache.tapestry5.services.TapestryModule$25.renderMarkup(TapestryModule.java:1894)
org.apache.tapestry5.internal.services.PageMarkupRendererImpl.renderPageMarkup(PageMarkupRendererImpl.java:47)
org.apache.tapestry5.internal.services.PageResponseRendererImpl.renderPageResponse(PageResponseRendererImpl.java:67)
org.apache.tapestry5.internal.services.PageRenderRequestHandlerImpl.handle(PageRenderRequestHandlerImpl.java:64)
org.apache.tapestry5.services.TapestryModule$38.handle(TapestryModule.java:2222)
org.apache.tapestry5.internal.services.ComponentRequestHandlerTerminator.handlePageRender(ComponentRequestHandlerTerminator.java:48)
com.bomahabo.flow.services.security.RequiresProjectFilter.handlePageRender(RequiresProjectFilter.java:53)
org.apache.tapestry5.services.InitializeActivePageName.handlePageRender(InitializeActivePageName.java:47)
org.tynamo.security.SecurityComponentRequestFilter.handlePageRender(SecurityComponentRequestFilter.java:49)
org.apache.tapestry5.internal.services.PageRenderDispatcher.dispatch(PageRenderDispatcher.java:45)
org.apache.tapestry5.services.TapestryModule$RequestHandlerTerminator.service(TapestryModule.java:302)
org.apache.tapestry5.internal.services.RequestErrorFilter.service(RequestErrorFilter.java:26)


Checking in the page if there any data should be simple. However I am
confused by this behaviour. Is my implementation of the Nodes what is wrong?
I see that NPE is coming from Tree so I dont think this on my end? Should I
create a jira for this?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by Geoff Callender <ge...@gmail.com>.
Sounds like a bug to me - the documentation doesn't say anything about null or empty not being allowed, so throwing NPE from somewhere inside isn't cool.

On 01/03/2013, at 8:21 PM, Lance Java wrote:

> I'm going to guess that you're using DefaultTreeModel. There are 2
> constructors:
> 
> public DefaultTreeModel(ValueEncoder<T> encoder, TreeModelAdapter<T>
> adapter, T root)
> and
> public DefaultTreeModel(ValueEncoder<T> encoder, TreeModelAdapter<T>
> adapter, List<T> roots)
> 
> If your tree is null, then you have no roots. Don't pass null to the first
> constructor because this will create a list with 1 null element in it (which
> is probably causing your NPE). Instead, pass Collections.emptyList() (or
> possibly null) to the second constructor.
> 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720296.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by Lance Java <la...@googlemail.com>.
I'm going to guess that you're using DefaultTreeModel. There are 2
constructors:

public DefaultTreeModel(ValueEncoder<T> encoder, TreeModelAdapter<T>
adapter, T root)
and
public DefaultTreeModel(ValueEncoder<T> encoder, TreeModelAdapter<T>
adapter, List<T> roots)

If your tree is null, then you have no roots. Don't pass null to the first
constructor because this will create a list with 1 null element in it (which
is probably causing your NPE). Instead, pass Collections.emptyList() (or
possibly null) to the second constructor.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720296.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by bhorvat <ho...@gmail.com>.
I guess it cant hurt to show you my root class just to see that I really am
not returning a null for the list 

public class AssetGroupRootValueWrapper implements ValueWrapper {

    private List<ValueWrapper> children;

    public AssetGroupRootValueWrapper(List<AssetGroup> assetGroups) {

        children = new LinkedList<ValueWrapper>();
        for (AssetGroup assetGroup : assetGroups) {
            children.add(new AssetGroupValueWrapper(assetGroup));
        }
    }

    @Override
    public boolean isLeaf() {
        return false;
    }

    @Override
    public boolean hasChildren() {
        return !getChildren().isEmpty();
    }

    @Override
    public List<ValueWrapper> getChildren() {
        return children;
    }

    @Override
    public String getLabel() {
        return "assets";
    }

    @Override
    public ValueWrapper seek(String clientValue) {
        if (clientValue.equals("assets")) {
            return this;
        }
        for (ValueWrapper assetValueWrapper : getChildren()) {
            ValueWrapper seek = assetValueWrapper.seek(clientValue);
            if (seek != null) {
                return seek;
            }
        }
        return null;
    }

    @Override
    public String getId() {
        return "assets";
    } 
}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720306.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by bhorvat <ho...@gmail.com>.
Hi, 

I would say that this is a bug as the NPE is thrown deep under the code. 

I have something like a categories and items under that. If I have a
categories that dont have any items or if I dont have any categories this
exception is thrown. 

I call the code as follows 

<t:tree model="assetGroupValueWrapperModel" value="valueWrapper" /> 

public TreeModel<ValueWrapper> getAssetGroupValueWrapperModel() {
        ValueEncoder<ValueWrapper> encoder = new
ValueEncoder<ValueWrapper>() {
            @Override
            public String toClient(ValueWrapper value) {
                return value.getId().toString();
            }

            @Override
            public ValueWrapper toValue(String clientValue) {
                return assetGroupRootNode.seek(clientValue);
            }
        };
        assetGroupRootNode = new
AssetGroupRootValueWrapper(systemManager.getAssetGroups(user.getSelectedProject()));
        return new DefaultTreeModel<ValueWrapper>(encoder, new
ValueWrapperTreeModelAdapter(), assetGroupRootNode.getChildren());
    }

The methods getChildren() always return an empty list if not data is
present. 

The error happens in the following place of the Tree class

private RenderCommand toRenderCommand(final TreeNode node, final boolean
isLast)
    {
        return new RenderCommand()
        {
            public void render(MarkupWriter writer, RenderQueue queue)
            {
                // Inform the component's container about what value is
being rendered
                // (this may be necessary to generate the correct label for
the node).
                Tree.this.node = node;

                value = node.getValue();

.......

when node.getValue is called it throws an exception as node is null. This
method seems to be called even though lists are empty...

Please let me know if you need more code.

Cheers



--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720305.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by Lance Java <la...@googlemail.com>.
Can you post your TreeModel? Are you returning null from getRootNodes()? If
so, I suggest returning an empty list instead.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720295.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NPE in Tree when I dont have any data

Posted by bhorvat <ho...@gmail.com>.
I have manged to fix this problem partially.

It turns out that my getChildlren methods where hard coded to return
true/false. This in turn triggered a NPE. It seems that if we getChildren
method returns true it means that there has to be some data so not even
empty list will work. 

Creating DefaultTreeModel as such

DefaultTreeModel<ValueWrapper>(encoder, new ValueWrapperAdapter(),
assetGroupRootNode.getChildren());

would require a list to be present and even a empty list will throw a NPE. 

To me this looks like a bug.

I have fixed my code so that inner nodes return appropriate value in
getChildren method and also I only display the list when I have some data in
the list. 

Please not that using the first constructore would not requre for such check
to exist but then I would have one root entry and I dont want that.

I guess this should be reported as bug then? Can someone please try out this
and double check my findings? 

Cheers and thanks everyone for help. 




--
View this message in context: http://tapestry.1045711.n5.nabble.com/NPE-in-Tree-when-I-dont-have-any-data-tp5720289p5720308.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org