我是新的Android开发人员,我有一个类,我正在调用webservices,我得到json响应,并希望将此rsponce保存在db和hashmap中。,但我收到以下错误..
java.lang.NullPointerException
at com.hb.hungryhub.DownloadReservations$LoadReservation.doInBackground(DownloadReservations.java:192)
At com.hb.hungryhub.DownloadReservations$LoadReservation.doInBackground(DownloadReservations.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
我的代码在这里。
public class DownloadReservations {
public final static String KEY_RESERVATION_ACTIVE = "active";
public final static String KEY_RESERVATION_CREATED_AT = "created_at";
public final static String KEY_RESERVATION_DATE = "date";
public final static String KEY_RESERVATION_END_TIME = "end_time";
public final static String KEY_RESERVATION_ID = "id";
public final static String KEY_RESERVATION_NAME = "name";
public final static String KEY_RESERVATION_PARTY_SIZE = "party_size";
public final static String KEY_RESERVATION_PHONE = "phone";
public final static String KEY_RESERVATION_RESTAURANT_ID = "restaurant_id";
public final static String KEY_RESERVATION_SPECIAL_REQUEST = "special_request";
public final static String KEY_RESERVATION_START_TIME = "start_time";
public final static String KEY_RESERVATION_TABLE = "table";
public final static String KEY_RESERVATION_UPDATED = "updated_at";
public final static String KEY_RESERVATION_USER_ID = "user_id";
public final static String KEY_RESERVATION_RESTAURANT_NAME = "rest_name";
ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
private ProgressDialog pDialog;
DbHelper db;// = new DbHelper(getActivity());
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> restaurantsList = new ArrayList<HashMap<String, String>>();
boolean isParsingCompleted = false;
Context context;
JSONArray restaurants = null;
DownloadReservationListener downloadReservationListener;
boolean showProcessing = false;
private static String url_reservation = "http://hungryhub.com:80/api/reservations.json";
boolean isUserAuth = true;
//Users user;
Restaurants restaurant;
ArrayList<HashMap<String, String>> bookingList = new ArrayList<HashMap<String, String>>();
String email,password, userId;
DownloadReservations(Context context, DownloadReservationListener downloadReservationListener,String email, String password, boolean showProcessing) {
this.context = context;
this.downloadReservationListener = downloadReservationListener;
this.email = email;
this.password = password;
this.showProcessing = showProcessing;
}
public void startDownloading() {
db = new DbHelper(context);
beforeSync();
}
public void beforeSync(){
cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
alert.showAlertDialog(context,"Internet Connection Error","Please connect to a working Internet connection",reservationListener,null, false);
return;
}
LoadReservation lr = new LoadReservation();
lr.execute();
}
class LoadReservation extends AsyncTask<String, String, String> {
int res_code = -1;
String errorMsg = "Cannot get your reservations at this time, please try later.";
@Override
protected void onPreExecute() {
super.onPreExecute();
if (showProcessing) {
pDialog = new ProgressDialog(context);
pDialog.setTitle("Processing");
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
}
@Override
protected String doInBackground(String... args) {
JSONObject obj = new JSONObject();
JSONObject objUser = new JSONObject();
try {
objUser.put("email", email);
objUser.put("password", password);
obj.put("user", objUser);
res_code = jParser.postJSONWithUrl(obj,url_reservation);
String response = JSONParser.getJson();
if (response.contains("ERR:")) {
int startIndex = response.indexOf("ERR:");
if (startIndex > -1) {
isUserAuth = false;
errorMsg = response.substring(startIndex + "ERR:".length()).trim();
return null;
}
}
String errorMsg = JSONParser.getJson();
if (errorMsg.contains("Incorect login/pass")) {
isUserAuth = false;
isParsingCompleted = false;
return null;
}
System.out.println(JSONParser.getJson());
JSONArray jarr = new JSONArray(JSONParser.getJson());
System.out.println(jarr.length());
Reservations reservation = new Reservations();
// HashMap<String, String> map = new HashMap<String, String>();
for(int i = 0; i < jarr.length(); i++){
JSONObject booking = jarr.getJSONObject(i);
//map.put(KEY_RESERVATION_ACTIVE, booking.getString(KEY_RESERVATION_ACTIVE));
reservation.set_active(booking.getString(KEY_RESERVATION_ACTIVE));
//map.put(KEY_RESERVATION_CREATED_AT, booking.getString(KEY_RESERVATION_CREATED_AT));
reservation.set_created_at(booking.getString(KEY_RESERVATION_CREATED_AT));
//map.put(KEY_RESERVATION_DATE, booking.getString(KEY_RESERVATION_DATE));
reservation.set_date(booking.getString(KEY_RESERVATION_DATE));
//map.put(KEY_RESERVATION_END_TIME, booking.getString(KEY_RESERVATION_END_TIME));
reservation.set_end_time(booking.getString(KEY_RESERVATION_END_TIME));
//map.put(KEY_RESERVATION_ID, booking.getString(KEY_RESERVATION_ID));
reservation.set_id(booking.getInt(KEY_RESERVATION_ID));
//map.put(KEY_RESERVATION_NAME, booking.getString(KEY_RESERVATION_NAME));
reservation.set_rest_name(booking.getString(KEY_RESERVATION_NAME));
//map.put(KEY_RESERVATION_PARTY_SIZE, booking.getString(KEY_RESERVATION_PARTY_SIZE));
reservation.set_party_size(booking.getString(KEY_RESERVATION_PARTY_SIZE));
//map.put(KEY_RESERVATION_PHONE, booking.getString(KEY_RESERVATION_PHONE));
reservation.set_phone(booking.getString(KEY_RESERVATION_PHONE));
//map.put(KEY_RESERVATION_RESTAURANT_ID, booking.getString(KEY_RESERVATION_RESTAURANT_ID));
reservation.set_restaurant_id(booking.getString(KEY_RESERVATION_RESTAURANT_ID));
//map.put(KEY_RESERVATION_SPECIAL_REQUEST, booking.getString(KEY_RESERVATION_SPECIAL_REQUEST));
reservation.set_special_request(booking.getString(KEY_RESERVATION_SPECIAL_REQUEST));
//map.put(KEY_RESERVATION_START_TIME, booking.getString(KEY_RESERVATION_START_TIME));
reservation.set_start_time(booking.getString(KEY_RESERVATION_START_TIME));
//map.put(KEY_RESERVATION_TABLE, booking.getString(KEY_RESERVATION_TABLE));
reservation.set_table(booking.getString(KEY_RESERVATION_TABLE));
//map.put(KEY_RESERVATION_UPDATED, booking.getString(KEY_RESERVATION_UPDATED));
reservation.set_updated_at(booking.getString(KEY_RESERVATION_UPDATED));
//map.put(KEY_RESERVATION_USER_ID, booking.getString(KEY_RESERVATION_USER_ID));
reservation.set_user_id(booking.getString(KEY_RESERVATION_USER_ID));
userId = booking.getString(KEY_RESERVATION_USER_ID);
//map.put(AllLabels.KEY_RESTAURANT_MISC, booking.getString(AllLabels.KEY_RESTAURANT_MISC));
//reservation.set_table(booking.getString(AllLabels.KEY_RESTAURANT_MISC));
//map.put(AllLabels.KEY_RESTAURANT_ARRIVED, booking.getString(AllLabels.KEY_RESTAURANT_ARRIVED));
//reservation.set_table(booking.getString(AllLabels.KEY_RESTAURANT_ARRIVED));
//map.put(AllLabels.KEY_RESTAURANT_ARRIVED, booking.getString(AllLabels.KEY_RESTAURANT_EMAIL));
//reservation.set_table(booking.getString(AllLabels.KEY_RESTAURANT_EMAIL));
// map.put(AllLabels.KEY_RESTAURANT_ARRIVED, booking.getString(AllLabels.KEY_SPECIAL_REQUEST));
// reservation.set_table(booking.getString(AllLabels.KEY_SPECIAL_REQUEST));
//
restaurant = db.getRestaurantDetail(Integer.valueOf(booking.getString(KEY_RESERVATION_RESTAURANT_ID)));
//map.put(KEY_RESERVATION_RESTAURANT_NAME, restaurant.get_name());
System.out.println(restaurant.get_name());
//reservation.set_rest_name(restaurant.get_name());
//db.addReservation(reservation);
//bookingList.add(map);
}
isParsingCompleted = true;
System.out.println(isParsingCompleted);
}
catch (Exception e) {
e.printStackTrace();
isUserAuth = false;
isParsingCompleted = false;
return null;
}
return null;
}
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
Log.d("flow", "MyReservation:onPostExecute");
if (showProcessing) {
pDialog.dismiss();
}
if ((res_code != 200 && res_code != 201) /*|| bookingList.size() < 1*/ || !isParsingCompleted) {
downloadReservationListener.errorDownloadingReservation(isUserAuth, errorMsg);
}
else {
isParsingCompleted = true;
((Activity)context).runOnUiThread(new Runnable() {
public void run() {
downloadReservationListener.successfullyDownloadedReservation(userId);
}
});
}
}
}
DialogInterface.OnClickListener reservationListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
removeFragment();
}
};
private void removeFragment() {
((MainActivity)context).removeFragment();
}
}
答案 0 :(得分:0)
我认为restaurant
为空..因此当您想要在nullpointer
中获取其名称时,它会抛出println()
。
所以请检查以下两行:
restaurant = db.getRestaurantDetail(Integer.valueOf(booking.getString(KEY_RESERVATION_RESTAURANT_ID)));
System.out.println(restaurant.get_name());
确保restaurant
;