JSONData를 읽을 권한이 있고 이제는 "이름"키를 콤보 상자에 표시하려고합니다. 하지만 내 문자열 Array nullpointerexception 얻을. JSON 객체를 바로 defaultComboBoxModel에 보내려고했습니다. 하지만 그것은 String 배열이 될 필요가 있기 때문에 모든 것을 변환하면 null이됩니다. 배열이 null이되는 이유는 무엇입니까?내 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
GUI를 디버깅하는 데에도 작동하지 않습니다. 심지어 전에 열리지도 않습니다. – Josef
최종 DefaultComboBoxModel 모델 = 새 DefaultComboBoxModel (Planner.getPlanner(). getLocationName()); comboBox = 새 JComboBox (모델); 모델을 콤보 박스에 보내지는 않지만 절대 점프하여 예외를 던지며 시작도 시작하지 않습니다. – Josef