1

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

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));
    }

让我感到困惑的是,对于“一些遍历”,我无法决定哪些遍历,当我进行遍历时我得到一个空异常。我在上述方法的以下行中得到了异常:

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)
4

1 回答 1

0

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

我还认为您使用的是“透明激活”而不是“注释”,因此您需要为每个 setter / getter 定义 ActivationPurpose。在我的项目中,我的操作与 db4o 透明激活完全一样,并且工作正常。

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

于 2013-07-02T19:44:40.450 回答