我已经管理阅读我的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);
NPE在哪里发生?你调试了你的代码吗? – Thomas 2013-03-26 13:00:25
它甚至没有工作来调试GUI的死亡,甚至打开它之前不是这样。 – Josef 2013-03-26 13:02:06
final DefaultComboBoxModel model = new DefaultComboBoxModel(Planner.getPlanner()。getLocationName()); comboBox = new JComboBox(model);它永远不会将模型发送到组合框,而是跳过它并施加异常,并且应用程序甚至可以启动。 – Josef 2013-03-26 13:03:44