2013-03-26 54 views
0

我已经管理阅读我的JSONData,现在我想显示“名称”键到组合框。 但我得到了我的字符串数组的nullpointerexception。 在我试图直接将JSON对象发送到defaultComboBoxModel之前。但它需要和字符串数组,所以当我转换它时,一切都变为空。 为什么我的数组变为空?如何在comboBox中显示我的JSON数据?

我的代码:

import java.util.ArrayList; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 


public class Planner { 

    private String startingLocation; 
    private String destination; 
    private static Planner planner = null; 
    private int id; 
    private JSONArray array; 
    ArrayList myList = new ArrayList(); 

    public static Planner getPlanner(){ 

     if (planner == null){ 

      planner = new Planner(); 

     } 

     return planner; 

    } 

    public void setStartingLocation(String from) { 

     this.startingLocation = from;  

    } 

    public void setDestination(String dest) { 

     this.destination = dest; 

    } 

    public String getStartingLocation() { 

     return startingLocation; 

    } 

    public String getDestination() { 

     return destination; 

    } 


    public void setID(int id) { 

     this.id = id; 

    } 

    public void setLocationName (JSONArray array){ 

     this.array = (JSONArray) array; 

    } 

    public String[] getLocationName(){ 

     System.out.println(this.array.size()); 

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

      myList = (ArrayList) this.array.get(i); 
      myList.add(i, array); 

     } 

     System.out.println(myList); 

    return (String[]) myList.toArray(); 

    } 

} 

public void parseJsonData() throws ParseException { 

     JSONParser parser = new JSONParser(); 
     Object obj = parser.parse(jsonData); 
     JSONObject topObject = (JSONObject) obj; 
     JSONObject locationList = (JSONObject) topObject.get("LocationList"); 
     JSONArray array = (JSONArray) locationList.get("StopLocation"); 
     Iterator<JSONObject> iterator = array.iterator(); 
     String name = null; 

     while (iterator.hasNext()) { 

      JSONObject jsonObj = (JSONObject) iterator.next(); 
      Planner.getPlanner().setLocationName(array); 

     } 

    } 

final DefaultComboBoxModel model = new DefaultComboBoxModel(Planner.getPlanner().getLocationName()); 
     comboBox = new JComboBox(model); 
+0

NPE在哪里发生?你调试了你的代码吗? – Thomas 2013-03-26 13:00:25

+0

它甚至没有工作来调试GUI的死亡,甚至打开它之前不是这样。 – Josef 2013-03-26 13:02:06

+0

final DefaultComboBoxModel model = new DefaultComboBoxModel(Planner.getPlanner()。getLocationName()); comboBox = new JComboBox(model);它永远不会将模型发送到组合框,而是跳过它并施加异常,并且应用程序甚至可以启动。 – Josef 2013-03-26 13:03:44

回答

0

我不会对代码风格或设计缺陷发表评论,但这里有一些线条看起来像一个错误:

for (int i=0; i<this.array.size(); i++){ 
     myList = (ArrayList) this.array.get(i); 
     myList.add(i, array); 
} 

myListArrayList和虽然我不知道array究竟是哪一个JSONArray实际包含的,但是如果它包含字符串或JSONObject的实例铸造array.get(i)ArrayList很可能会导致ClassCastException。 如果array包含其他JSONArray s,它可能不会发生。

下一行很奇怪。假设array.get(i)实际上返回一个列表,您将该列表分配给myList,然后将该数组添加到该列表中,这将导致循环引用(array包含列表,其中包含array等)。

接下来,您将在每次迭代中更换myList,这可能不是您想要的。

假设array包含JSONArray istances,JSONArray延伸ArrayList(因此没有ClassCastException)和内部数组包含字符串,则可能该方法改变这样的事情:

public String[] getLocationName(){ 

    System.out.println(this.array.size()); 

    List<String> resultList = new LinkedList<String>();   

    for (int i=0; i<this.array.size(); i++){ 
     //cast the inner array to a list of Strings (assuming it only contains Strings) 
     List<String> innerList = (ArrayList) this.array.get(i); 

     //add the inner list to the result list 
     resultList.addAll(innerList); 

    } 

    System.out.println(resultList); 

    return resultList.toArray(new String[resultList.size()]); 
} 

这样你会转换字符串列表的列表(即JSONArray包含JSONArray s,其中包含String s)转换为字符串的平面列表并将其作为数组返回。

+0

这没有效果在同一地点stil空指针 – Josef 2013-03-26 13:19:20

+0

数组是一个JSON数组与JSONObjects – Josef 2013-03-26 13:27:26

+0

请提供NPE发生的确切线(标记它,不要只提供行号!)。此外,如果数组包含JSONObjects,那么您必须不会将其转换为'ArrayList'。除此之外,您必须从当前元素中提取位置属性。 – Thomas 2013-03-26 15:11:19