简体   繁体   English

在App Engine Java Servlet中查询数据存储区统计信息时发生NullpointerException

[英]NullpointerException while querying for the Datastore statistics in App Engine Java Servlet

Within my app's Servlet, I want to control wether its Datastore is empty or not (the first time I run the servlet it will be empty, but that's not the case right now as I already have populated it and made it persistent) before continuing with the rest of the code. 在我的应用程序的Servlet中,我想控制它的数据存储区是否为空(第一次运行servlet时它将为空,但是现在不是这种情况,因为我已经填充了它并使其持久化),然后继续其余的代码。 In this Datastore Docs I discovered a way of querying the datastore and fetch different kind of statistics. 在此数据存储区文档中,我发现了一种查询数据存储区并获取不同类型的统计信息的方法。 But while executing: 但是在执行时:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");

I get a NullPointerException when I try to store the two Long variables. 当我尝试存储两个Long变量时,出现NullPointerException Why is the Entity globalStat object null after the query? 为什么查询后Entity globalStat对象为null?

EDIT 1 : Additional information 编辑1 :附加信息

I'm trying to get the Datastore statistics just after populating it with a List<Entity> of Entities: 我试图在使用List<Entity>List<Entity>填充数据存储区后获取数据存储区统计信息:

public class MyServlet extends HttpServlet {
    ArrayList<Tour> m_tours = new ArrayList<Tour>();
    Key tourKey;
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    //.... some code
    private DatastoreService populateDatastore(){
        //... some other code ...
        List<Entity> List = Arrays.asList(tour,tour1,tour2,tour3,
                                     tour4,tour5,tour6,tour7,tour8);
        datastore.put(List);
        Entity globalStat = datastore.prepare(
                             new Query("__Stat_Total__")).asSingleEntity();

        try{
            Long totalEntities = (Long) globalStat.getProperty("count");
            Long totalBytes = (Long) globalStat.getProperty("bytes");
        }catch (NullPointerException e){
            e.printStackTrace();
        }` 
    }
}

and just this instructions the Entity globalstat object still shows to be null in the debugging tool: 仅此一条指令,实体globalstat对象在调试工具中仍然显示为null:

实体globalstat

how come that the new Query("__Stat_Total__")).asSingleEntity(); 为什么会有new Query("__Stat_Total__")).asSingleEntity(); doesn't produce any result when I just filled the datastore with several entities? 我只用几个实体填充数据存储区时不会产生任何结果?

ANSWER : as reported here: Using App Engine Datastore Low Level API with Java , "Stat_Total" and "Stat_Kind" don't work in a local development server. 解答 :如此处报道: 将App Engine数据存储库低级API与Jav​​a配合使用“ Stat_Total”“ Stat_Kind”在本地开发服务器中不起作用。 They only work when deployed in App Engine Server . 它们仅在部署到App Engine Server时才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM