gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.add(java.lang.Object)'

转载 作者:行者123 更新时间:2023-12-01 21:48:04 25 4
gpt4 key购买 nike

嘿伙计们,我尝试获取 json 格式的数据,然后在微调器中显示它们。我遇到了一个问题,你能帮我吗?这是我的代码。

public class MainActivity extends Activity {
Spinner selectcityspinner, selectareaspinner;
ArrayList<city> citylist;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectcityspinner = (Spinner)findViewById(R.id.selectcityspin);
selectareaspinner = (Spinner) findViewById(R.id.selectareaspin);
List<city> citylist = new ArrayList<>();
new cityAsynTask().execute("http://bund.webs.com/citylist.txt");

/*ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item,citylist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectcityspinner.setAdapter(adapter);
selectcityspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " selected",Toast.LENGTH_LONG).show();

String Country = String.valueOf(parent.getItemAtPosition(position));

if (Country.contentEquals("Ahmedabad"))
{
List<String> AreaList = new ArrayList<String>();
AreaList.add("Ghatlodia");
AreaList.add("Chandlodia");
AreaList.add("Others");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item,AreaList);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.notifyDataSetChanged();
selectareaspinner.setAdapter(adapter1);
}


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});*/

}
public class cityAsynTask extends AsyncTask<String,Void,Boolean>
{

@Override
protected Boolean doInBackground(String... params) {

try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jobjcity = new JSONObject(data);
JSONArray jarray = jobjcity.getJSONArray("actors");
for (int i = 0;i < jarray.length();i++){
city city = new city();
JSONObject jrealobject = jarray.getJSONObject(i);
city.setCity(jrealobject.getString("name"));
citylist.add(city);

}
return true;
}

} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}


return false;
}

@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(aBoolean == false){
Toast.makeText(MainActivity.this,"data isnot recieved",Toast.LENGTH_SHORT).show();
}

}
}

}

城市.java

public class city {
private String city;

public city(String city) {
this.city = city;
}

public city() {

}

public String getCity() {

return city;
}

public void setCity(String city) {
this.city = city;
}
}

这是我的错误

02-28 02:12:44.044 2863-2880/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.bhaumik.myparsesignup, PID: 2863
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.bhaumik.myparsesignup.MainActivity$cityAsynTask.doInBackground(MainActivity.java:115)
at com.example.bhaumik.myparsesignup.MainActivity$cityAsynTask.doInBackground(MainActivity.java:95)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

你们能帮帮我吗???真的很感激。

最佳答案

替换:

List<city> citylist = new ArrayList<>();

与:

citylist = new ArrayList<>();

这样,您就可以正确初始化 citylist 字段,而不是定义新的(未使用的)局部变量,因此以后它不会为 null

关于java - 尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.add(java.lang.Object)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35675344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com