db4o透明激活 - 检索节点的子值时的Null异常

时间:2013-04-26 20:14:05

标签: java db4o activation

我创建了一个节点树,并将其存储在面向对象的数据库(db4o)中。因为我想从数据库高效加载节点,所以我使用透明激活,并实现了Activatable接口。我的Node类如下:

package directory_service_server;

import com.db4o.activation.ActivationPurpose;
import com.db4o.activation.Activator;
import com.db4o.ta.Activatable;
import java.util.ArrayList;


public class Node implements Activatable{

    private String name;
    private String attribute;
    private ArrayList<Node> children;
    private transient Activator _activator; 
    //Node father;

    public Node()
    {
        this.name="";
        this.attribute="";
        this.children= new ArrayList<Node>();
      //  this.father= new Node ();
    }
    public Node(String name, String attribute, ArrayList<Node> children)
    {
        this.name=name;
        this.attribute=attribute;
        this.children=children;

    }

    void setName(String name)
    {

        this.name=name;
    }

    void setAttribute(String attribute)
    {
        this.attribute=attribute;
    }

    void setChildren(ArrayList<Node> children)
    {
       this.children=children; 

    }


    String getName()
    {
        this.activate(ActivationPurpose.READ);
        return name;
    }

    String getAttribute()
    {
        this.activate(ActivationPurpose.READ);
        return this.attribute;
    }

    String getAttributeWithoutActivation()
    {
        return this.attribute;
    }

    ArrayList<Node> getChildren()
    {
        this.activate(ActivationPurpose.READ);
        return children;
    }


    @Override
    public void activate(ActivationPurpose purpose) {
        if(_activator!=null)
            _activator.activate(purpose);
    }

    @Override
    public void bind(Activator activator) {

        if(_activator==activator)
        {
            return;
        }


        if(activator!=null && _activator!=null )
        {
            throw new IllegalStateException();
        }

         _activator=activator;   
    }




}

当我从数据库加载根(某个节点)并开始遍历图形时,我的遍历方法(也打印节点值)如下:

 void displayTree(Node node)
    {
        System.out.print(node.getName()+"-");
        for(int i=0; i<node.getChildren().size(); i++)
            displayTree(node.getChildren().get(i));
    }

让我感到困惑的是,对于“一些遍历”,我无法确定哪些,当我进行遍历时,我得到一个Null Exception。我在上面的方法中的以下行中得到了例外:

for(int i=0; i<node.getChildren().size(); i++)

我的解释是节点的子节点是NULL,因此访问size()方法会给我NULL。我想也许我在Activatable类的实现中遗漏了一些东西。我使用了以下的源代码来实现,但我没有找到其他来源。 http://docs.huihoo.com/db4o/db4o-7.0-java-tutorial.pdf

有人能告诉我我失踪了吗?下面是堆栈跟踪的一部分,

ava.lang.NullPointerException
    at directory_service_server.Tree.displayTree(Tree.java:88)
    at directory_service_server.Tree.displayTree(Tree.java:89)
    at directory_service_server.Tree.displayTree(Tree.java:89)
    at directory_service_server.Tree.displayTree(Tree.java:89)
    at directory_service_server.ClientHandler.registerService(ClientHandler.java:187)
    at directory_service_server.ClientHandler.run(ClientHandler.java:59)
    at java.lang.Thread.run(Thread.java:722)
com.db4o.events.EventException
    at com.db4o.internal.events.EventRegistryImpl.withExceptionHandlingInCallback(EventRegistryImpl.java:283)
    at com.db4o.internal.events.EventRegistryImpl.triggerObjectInfoEventInCallback(EventRegistryImpl.java:270)
    at com.db4o.internal.events.EventRegistryImpl.objectOnNew(EventRegistryImpl.java:89)
    at com.db4o.internal.ObjectReference.objectOnNew(ObjectReference.java:194)
    at com.db4o.internal.ObjectReference.continueSet(ObjectReference.java:180)
    at com.db4o.internal.ObjectContainerBase.stillToSet(ObjectContainerBase.java:1902)
    at com.db4o.internal.ObjectContainerBase.store2(ObjectContainerBase.java:1698)
    at com.db4o.internal.ObjectContainerBase.storeAfterReplication(ObjectContainerBase.java:1618)
    at com.db4o.internal.ObjectContainerBase$10.apply(ObjectContainerBase.java:1604)
    at com.db4o.internal.ObjectContainerBase$10.apply(ObjectContainerBase.java:1602)
    at com.db4o.internal.ObjectContainerBase.asTopLevelCall(ObjectContainerBase.java:427)
    at com.db4o.internal.ObjectContainerBase.asTopLevelStore(ObjectContainerBase.java:413)
    at com.db4o.internal.ObjectContainerBase.storeInternal(ObjectContainerBase.java:1602)
    at com.db4o.internal.marshall.MarshallingContext.writeObject(MarshallingContext.java:252)
    at com.db4o.internal.OpenTypeHandler.writeObject(OpenTypeHandler.java:266)
    at com.db4o.internal.OpenTypeHandler.write(OpenTypeHandler.java:251)
    at com.db4o.internal.marshall.MarshallingContext.writeObjectWithCurrentState(MarshallingContext.java:272)
    at com.db4o.internal.marshall.MarshallingContext.writeObject(MarshallingContext.java:260)
    at com.db4o.typehandlers.CollectionTypeHandler.writeElements(CollectionTypeHandler.java:56)

1 个答案:

答案 0 :(得分:0)

您是否尝试使用Activatable collections而不是普通的Collection?每个标准java集合都有可激活的集合。 Thoses集合已经定义了ActivationPurpose.READ/WRITE。

我还认为您使用的是“透明激活”而不是“注释”,因此您需要为每个setter / getter定义ActivationPurpose。 在我的项目中我完全如此 versant db4o transparent activation及其工作正常。

您还应该查看 transparentpersistence + * ta_enhanced_example *以获得正确的注释过程,但它最终会有效。