2013-03-26 66 views
0

我開發GWT應用程序,我有一個方法: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; 
} 

看來,我進入週期,但創建票據對象沒有後發生的事情。可能是對象甚至沒有初始化。這裏是門票類:

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來獲取值,但結果是一樣的。該方法的參數字符串不是空的。如果有人知道爲什麼它不工作,請告訴我。

+0

你也應該張貼JSON數據,這是閱讀,如ticketsJSONString' – 2013-03-26 13:28:36

回答

1

我猜想,你的代碼是在一個你的JSON獲得價值崩潰,比如(int)ticketJSON.get("complexity").isNumber()

如果這些值均爲空或丟失,會發生什麼?您的代碼會因空指針異常而崩潰 - NPE。

放入一些行來檢查NPE。

一般來說,你可能會考慮使用JavaSsript覆蓋類型來處理這種事情。您只需創建票的覆蓋,而不是複製每個值:http://googlewebtoolkit.blogspot.ca/2008/08/getting-to-really-know-gwt-part-2.html

+0

使用覆蓋類型的'內容的一個例子是比第一種方式更好的解決方案這個極其複雜的JSON解析....我經常使用它們,它們工作得很好! – Stefan 2013-03-26 11:19:24

+0

意味着你只能在GWT上使用你的代碼,儘管:( – funkybro 2013-03-26 11:29:42

+0

這一切都很好,但沒有空值,而邏輯是說代碼應該可以正常工作,但實際上它並沒有, t甚至可以完成一個單一的循環... – malkoto1 2013-03-26 11:57:54