orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Accessing object's Map field results in NullPointerException when enum used as Map key

Open buzzkillington opened this issue 13 years ago • 3 comments

If an enum type is used as the key for a stored object's Map field, accessing that Map results in a NullPointerException in OObjectProxyMethodHandler.

Running the test program below, on OrientDB 1.5.1, results in the following Exception. (Note: if the key and value types are swapped - i.e., Map<String, MyEnum> instead of Map<MyEnum, String> - then the program runs as expected.)

Exception in thread "main" java.lang.NullPointerException
    at com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler.getValue(OObjectProxyMethodHandler.java:321)
    at com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler.manageGetMethod(OObjectProxyMethodHandler.java:271)
    at com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler.invoke(OObjectProxyMethodHandler.java:115)
    at orienttest.MapTest$TestObject_$$_javassist_0.getData(MapTest$TestObject_$$_javassist_0.java)
    at orienttest.MapTest.main(MapTest.java:41)

Test code:

package orienttest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabasePool;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;

public class MapTest
{
    public static void main(String[] args)
    {        
        try(OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/foo", "admin", "admin"))
        {
            try
            {
                db.command(new OCommandSQL("delete from TestObject")).execute();
            }
            catch(Exception e)
            { } 
            db.getEntityManager().registerEntityClass(MyEnum.class);
            db.getEntityManager().registerEntityClass(TestObject.class);

            for(Integer i = 0; i < 10; ++i)
            {
                TestObject t = new TestObject();
                t.setName("TO-" + i);
                t.getData().put(MyEnum.FOO, i.toString());
                t.getData().put(MyEnum.BAR, i.toString());
                t.getData().put(MyEnum.BAZ, i.toString());
                db.save(t);
            }

            List<TestObject> testObjects = ((List<TestObject>)db.command(new OSQLSynchQuery<TestObject>("select * from TestObject")).execute());
            for(TestObject t : testObjects)
            {
                System.out.print(t.getName() + ": ");
                for(MyEnum key : t.getData().keySet())
                {
                    System.out.print("(" + key + ", " + t.getData().get(key) + ") ");
                }
                System.out.println();
            }
        }
    }

    public static enum MyEnum
    {
        FOO,
        BAR,
        BAZ;
    }

    public static class TestObject
    {
        private String name;
        private Map<MyEnum, String> data = new HashMap<>();

        public void setName(String name)
        {
            this.name = name;
        }

        public String getName()
        {
            return name;
        }

        public Map<MyEnum, String> getData()
        {
            return data;
        }        
    }
}

buzzkillington avatar Sep 18 '13 23:09 buzzkillington

@MaDaPHaKa have you fixed it in 1.6?

lvca avatar Sep 18 '13 23:09 lvca

Hi,

right now the ObjectDatabase supports only Map<String,Object>. I created an issue https://github.com/orientechnologies/orientdb/issues/1692

2013/9/19 Luca Garulli [email protected]

@MaDaPHaKa https://github.com/MaDaPHaKa have you fixed it in 1.6?

— Reply to this email directly or view it on GitHubhttps://github.com/orientechnologies/orientdb/issues/1691#issuecomment-24708589 .

Luca Molino

MaDaPHaKa avatar Sep 19 '13 09:09 MaDaPHaKa

Not really a bug but a partial Map management in ObjectDatabase. Here the enhancement issue: https://github.com/orientechnologies/orientdb/issues/1692

MaDaPHaKa avatar Sep 19 '13 15:09 MaDaPHaKa