我想创建一个对象数组,但我得到一个java.lang.NullPointerException
。此刻我不知道为什么也找不到错误。该类将保存在GAE数据存储区中。如果有人能告诉我出了什么问题,那就太好了。
我做了什么:
@Transient private int plc = 0;
@Transient private Profile[] players = new Profile[4];
public void setPlayers(Profile player) {
if (plc < 4) {
this.players[plc] = new Profile(); // Exception
this.players[plc] = player;
plc++;
}
}
我想念什么?
个人资料构造函数
public Profile() {
}
public Profile(String account, String name, int gender) {
this.account = account;
this.name = name;
this.gender = gender;
}
Stracktrace
java.lang.NullPointerException
at packgesname.Game.setPlayers(Game.java:40)
at packgesname.JsonServlet.joinParty(JsonServlet.java:115)
at packgesname.JsonServlet.doGet(JsonServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:110)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:380)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:0)
您的Profile
构造函数中可能存在问题,是否有某些代码可能会导致NullPointerException
?
答案 1 :(得分:0)
您已使用players
注释标记@Transient
。我不知道这个库来自哪个库,但它可能没有将它设置为值,因为它可能不是在调用构造函数(因此在那里初始化它),也不是设置值(因为它是瞬态的)。试着像这样懒洋洋地初始化它:
public void setPlayers(Profile player) {
if (this.players == null)
this.players = new Profile[4];
if (plc < 4) {
this.players[plc] = new Profile(); // Exception
this.players[plc] = player;
plc++;
}
}
请记住,序列化库不会以任何方式序列化瞬态值,并且新对象对于瞬态字段将具有null
,即使您在构造函数中初始化它们也是如此。这是因为对于大多数序列化库,库对象是已分配,而不是构造,然后在分配后设置其值。这会跳过您的构造函数,并且不会将瞬态players
初始化为任何内容,只留下null
。
如果你想确定,在构造函数中放置一个断点并序列化你的对象,然后反序列化它。如果反序列化正在调用构造函数(即通过反射或类似的机制),那么它将击中你的断点。如果没有,那么这就是你的问题。