0

我想创建一个对象数组,但我得到一个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;
}

追踪追踪

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)
4

2 回答 2

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.

如果你想确定,在构造函数中放置一个断点并序列化你的对象,然后反序列化它。如果反序列化调用构造函数(即通过反射或一些类似的机制),那么它将命中您的断点。如果没有,那么这就是你的问题。

于 2012-10-29T16:36:01.753 回答
0

您的Profile构造函数中可能有问题,是否有一些代码可以在NullPointerException那里抛出?

于 2012-10-29T16:23:40.303 回答