循环的GWT不循环

时间:2013-03-26 10:00:26

标签: java json gwt for-loop

我正在开发一个GWT应用程序,我有这个方法:

    private static List<Ticket> getTicketsFromJSON(String ticketsJSONString) {
    JSONValue ticketsArrayValue = JSONParser.parseLenient(ticketsJSONString);
    JSONArray ticketsArray = ticketsArrayValue.isArray();
    List<Ticket> ticketsList = new LinkedList<Ticket>();

    for (int i = 0; i < ticketsArray.size(); i++) {

        JSONObject ticketJSON = ticketsArray.get(i).isObject();
        Ticket ticket = new Ticket((int)ticketJSON.get("id").isNumber().doubleValue(), 
                ticketJSON.get("name").isString().stringValue(),
                ticketJSON.get("description").isString().stringValue(),
                (int)ticketJSON.get("complexity").isNumber().doubleValue(),
                (int)ticketJSON.get("priority").isNumber().doubleValue(),
                (long)ticketJSON.get("started").isNumber().doubleValue(),
                (long)ticketJSON.get("finished").isNumber().doubleValue(),
                (long)ticketJSON.get("timespent").isNumber().doubleValue());
        ticketsList.add(ticket);
    }
    Window.alert("Out of the cicle");
    return ticketsList;
}

似乎我进入了循环但是在创建Ticket对象之后没有发生任何事情。可能是对象甚至没有初始化。这是Ticket类:

    package org.kanbanizer.client.beans;

public class Ticket {
    private int id;
    private String name;
    private String description;
    private int complexity;
    private int priority;
    private long started;
    private long finished;
    private long timespent;

    public Ticket() {
    }

    public Ticket(int id, String name, String description, int complexity,
            int priority, long started, long finished, long timespent) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.complexity = complexity;
        this.priority = priority;
        this.started = started;
        this.finished = finished;
        this.timespent = timespent;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getComplexity() {
        return complexity;
    }

    public void setComplexity(int complexity) {
        this.complexity = complexity;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public long getStarted() {
        return started;
    }

    public void setStarted(long started) {
        this.started = started;
    }

    public long getFinished() {
        return finished;
    }

    public void setFinished(long finished) {
        this.finished = finished;
    }

    public long getTimespent() {
        return timespent;
    }

    public void setTimespent(long timespent) {
        this.timespent = timespent;
    }

}

我尝试使用和Integer.parseInt获取值,但结果是一样的。作为方法参数的字符串不为空。如果有人知道为什么不能正常工作,请告诉我。

1 个答案:

答案 0 :(得分:1)

我猜你的代码会在你的JSON获取值上崩溃,例如(int)ticketJSON.get("complexity").isNumber()

如果这些值为null或缺失会发生什么?您的代码将因空指针异常(NPE。

)而崩溃

添加一些行来检查NPE。

通常,您可以考虑使用JavaSsript Overlay Types来实现此类操作。您只需为故障单创建叠加层,而不是复制每个值:http://googlewebtoolkit.blogspot.ca/2008/08/getting-to-really-know-gwt-part-2.html