java – 初始化本地数据存储异常:没有为...

codeday· 2019-10-08
本文来自 codeday ,作者 codeday
我想用一些数据初始化我的本地数据存储
常规Java程序(我不想启动开发服务器
并调用服务/ servlet),我得到以下异常

例外:

Exception in thread "main" java.lang.NullPointerException: No API environment is registered for this thread.
    at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:108)
    at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:118)
    at com.google.appengine.api.datastore.Key.(Key.java:104)
    at com.google.appengine.api.datastore.Key.(Key.java:88)
    at com.google.appengine.api.datastore.Key.(Key.java:84)
    at com.google.appengine.api.datastore.Entity.(Entity.java:122)
    at com.google.appengine.api.datastore.Entity.(Entity.java:103)
    at org.datanucleus.store.appengine.DatastoreFieldManager.(DatastoreFieldManager.java:187)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPreProcess(DatastorePersistenceHandler.java:338)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:251)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:240)
    at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
    at org.datanucleus.state.JDOStateManagerImpl.makePersistent(JDOStateManagerImpl.java:3161)
    at org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1298)
    at org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1175)
    at org.datanucleus.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:669)
    at org.datanucleus.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:694)
    at com.stuff.server.GreetingServiceImpl.saveContact(GreetingServiceImpl.java:25)
    at com.stuff.server.TestCase.testServerCallBack(TestCase.java:18)
    at com.stuff.server.TestCase.main(TestCase.java:13)

TestCase.java

public class TestCase {
    static GreetingServiceImpl greetingServiceImpl = new GreetingServiceImpl();
public static void main(String[] args) {
    testServerCallBack();//line9
}

private static void testServerCallBack() {
    Contacts contacts = new Contacts("this is", "awesome");
    greetingServiceImpl.saveContact(contacts);//line:14
}

}

问候….. impl.java

public class GreetingServiceImpl extends RemoteServiceServlet implements
        GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
    return "";
}

@Override
public void saveContact(Contacts contacts) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.makePersistent(contacts); //line:20
    pm.close();
}

}

有没有办法做到这一点?怎么样?

最佳答案
Google提供了一个帮助类,它可以完全按照您的需要运行 – 运行足够的代码来处理数据库,而无需启动整个开发服务器.请参阅http://code.google.com/appengine/docs/java/tools/localunittesting.html中的setUp和tearDown方法