在 App Engine Java Servlet 中查询数据存储区统计信息时出现 NullpointerException
NullpointerException while querying for the Datastore statistics in App Engine Java Servlet
在我的应用程序的 Servlet 中,我想控制它的数据存储是否为空(我第一次 运行 servlet 它将是空的,但现在情况并非如此,因为我已经填充了它并使其持久化),然后再继续其余的代码。在这个 Datastore Docs 中,我发现了一种查询数据存储和获取不同类型统计信息的方法。
但是在执行时:
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");
当我尝试存储两个 Long 变量时,我得到一个 NullPointerException
。
为什么查询后Entity globalStat
对象为null?
编辑 1:附加信息
我试图在用 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();
}`
}
}
仅此说明实体 globalstat 对象在调试工具中仍然显示为空:
当我用多个实体填充数据存储时,为什么 new Query("__Stat_Total__")).asSingleEntity();
没有产生任何结果?
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. They only work when deployed in App Engine Server
.
在我的应用程序的 Servlet 中,我想控制它的数据存储是否为空(我第一次 运行 servlet 它将是空的,但现在情况并非如此,因为我已经填充了它并使其持久化),然后再继续其余的代码。在这个 Datastore Docs 中,我发现了一种查询数据存储和获取不同类型统计信息的方法。 但是在执行时:
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");
当我尝试存储两个 Long 变量时,我得到一个 NullPointerException
。
为什么查询后Entity globalStat
对象为null?
编辑 1:附加信息
我试图在用 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();
}`
}
}
仅此说明实体 globalstat 对象在调试工具中仍然显示为空:
当我用多个实体填充数据存储时,为什么 new Query("__Stat_Total__")).asSingleEntity();
没有产生任何结果?
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. They only work when deployed in
App Engine Server
.