簡體   English   中英

由於 NullPointerException 無法啟動活動:嘗試在 null object 參考上調用虛擬方法 HashMap

[英]Unable to start activity because of NullPointerException: Attempt to invoke virtual method HashMap on a null object reference

我正在嘗試啟動活動 checkin1。 我正在使用 volley 並從 mySQL db 請求信息以顯示在四個編輯文本字段中。

我正在使用我在我的應用程序的其他活動(會話管理器)中使用的相同策略,並且我的代碼是相同的,因為我一直在交叉引用並想知道為什么第 47 行會發生錯誤:

checkin1 HashMap<String, String> user= sessionManager.GetCheckInInfo();getId = user.get(sessionManager.ID);

我相信這可能是我的 session 管理器( SessionManager 的第 130 行 - GetCheckInInfo();) 中的問題,但我不確定,因為它與我一直在交叉引用的其他類幾乎相同並且工作正常。 任何幫助,將不勝感激!

Sessionmanager.java

public class SessionManager {

    SharedPreferences sharedPreferences;
    public SharedPreferences.Editor editor;
    public Context context;
    int Private_Mode = 0;

    private static final String PREF_NAME= "LOGIN";
    private static final String LOGIN = "IS_LOGIN";
    public static final String USERNAME= "USERNAME";
    public static final String EMAIL= "EMAIL";
    public static final String ID = "ID";
    public static final String PHONE = "PHONE_NUMBER";
    private static final String ADDSM= "ADDITIONAL_SOCIAL_MEDIA";
    private static final String HCNAME = "HOME_COUNTRY_EMERGENCY_CONTACT_NAME";
    public static final String HCPHONE = "HOME_COUNTRY_EMERGENCY_PHONE_NUMBER";
    public static final String HCEMAIL= "HOME_COUNTRY_EMERGENCY_CONTACT_EMAIL";
    public static final String USANAME = "USA_EMERGENCY_CONTACT_NAME";
    public static final String USAPHONE= "USA_EMERGENCY_PHONE_NUMBER";
    public static final String USAEMAIL= "USA_EMERGENCY_CONTACT_EMAIL";
    public static final String WHATSAPP = "WHATSAPP";
    public static final String ADDRESS= "ADDRESS";
    public static final String START_DATE= "START_DATE";
    public static final String END_DATE= "END_DATE";


    public SessionManager(Context context){
        this.context = context;
        sharedPreferences = context.getSharedPreferences(PREF_NAME, Private_Mode);
        editor = sharedPreferences.edit();
    }
    public void createSession(String username, String email, String id){
        editor.putBoolean(LOGIN, true);
        editor.putString(USERNAME, username);
        editor.putString(EMAIL, email);
        editor.putString(ID, id);
        editor.apply();

    }
    public boolean isLoggin(){
        return sharedPreferences.getBoolean(LOGIN, false);

    }
    public void checkLogin(){
        if (!this.isLoggin()){
            Intent i = new Intent(context, Register.class);
            context.startActivity(i);
            ((MainActivity)context).finish();
        }
    }

    public HashMap<String, String> getUserDetail(){
        HashMap<String, String> user = new HashMap<>();
        user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
        user.put(EMAIL, sharedPreferences.getString(EMAIL, null));
        user.put(ID, sharedPreferences.getString(ID, null));

        return user;
    }
    public void logout(){
        editor.clear();
        editor.commit();
        Intent i = new Intent(context, LoginActivity.class);
        context.startActivity(i);
        ((MainActivity)context).finish();
    }
    public void createSession2(String username, String id, String phone_number, String additional_social_media,
                               String home_country_emergency_contact_name, String address,
                               String home_country_emergency_phone_number, String home_country_emergency_contact_email,
                               String usa_emergency_contact_name, String  usa_emergency_phone_number,
                               String usa_emergency_contact_email, String whatsapp){
        editor.putBoolean(LOGIN, true);
        editor.putString(USERNAME, username);
        editor.putString(ID, id);
        editor.putString(PHONE, phone_number);
        editor.putString(ADDRESS, address);
        editor.putString(HCPHONE, home_country_emergency_phone_number);
        editor.putString(HCNAME, home_country_emergency_contact_name);
        editor.putString(HCEMAIL, home_country_emergency_contact_email);
        editor.putString(USAPHONE, usa_emergency_phone_number);
        editor.putString(USANAME,  usa_emergency_contact_name);
        editor.putString(USAEMAIL, usa_emergency_contact_email);
        editor.putString(WHATSAPP, whatsapp);
        editor.putString(ADDSM, additional_social_media);
        editor.apply();

}
    public void createSessionMCI(String username, String id, String phone_number, String email, String address){
        editor.putBoolean(LOGIN, true);
        editor.putString(USERNAME, username);
        editor.putString(ID, id);
        editor.putString(PHONE, phone_number);
        editor.putString(ADDRESS, address);
        editor.putString(EMAIL, email);
        editor.apply();

    }
    public HashMap<String, String> getUserDetail2(){
        HashMap<String, String> user = new HashMap<>();
        user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
         user.put(ID, sharedPreferences.getString(ID, null));
        user.put(ADDRESS, sharedPreferences.getString(ADDRESS, null));
        user.put(WHATSAPP, sharedPreferences.getString(WHATSAPP, null));
        user.put(PHONE, sharedPreferences.getString(PHONE, null));
        user.put(ADDSM, sharedPreferences.getString(ADDSM, null));
        user.put(HCNAME, sharedPreferences.getString(HCNAME, null));
        user.put(HCPHONE, sharedPreferences.getString(HCPHONE, null));
        user.put(HCEMAIL, sharedPreferences.getString(HCEMAIL, null));
        user.put(USANAME, sharedPreferences.getString(USANAME, null));
        user.put(USAPHONE, sharedPreferences.getString(USAPHONE, null));
        user.put(USAEMAIL, sharedPreferences.getString(USAEMAIL, null));
        return user;
    }
    public HashMap<String, String> getUserDates() {
        HashMap<String, String> user = new HashMap<>();
        user.put(START_DATE, sharedPreferences.getString(START_DATE, null));
        user.put(END_DATE, sharedPreferences.getString(END_DATE, null));
        user.put(ID, sharedPreferences.getString(ID, null));

        return user;
    }
    public HashMap<String, String> GetCheckInInfo() {
        HashMap<String, String> user = new HashMap<>();
        user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
        user.put(EMAIL, sharedPreferences.getString(EMAIL, null));
        user.put(PHONE, sharedPreferences.getString(PHONE, null));
        user.put(ADDRESS, sharedPreferences.getString(ADDRESS, null));
        user.put(ID, sharedPreferences.getString(ID, null));

        return user;
    }
}

checkin1.java

 private static final String TAG = checkin1.class.getSimpleName();
EditText phone_number, address, email, username;
SessionManager sessionManager;
 private static String URL_ReadCheckin= "http://192.168.0.86:80/ReadCheckIns.php";
 private static String URL_EDITCheckIN = "http://192.168.0.86:80/edit_detailcheckin.php";
 String getId;
 Button GoToJournal, GoToMain;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_checkin1);

     HashMap<String, String> user= sessionManager.GetCheckInInfo();
     getId = user.get(sessionManager.ID);

     phone_number = findViewById(R.id.updatePhone);
     email = findViewById(R.id.updateEmail);
     address = findViewById(R.id.updateAddress);
     username = findViewById(R.id.updateUsername);
     GoToJournal = findViewById(R.id.myjournalbtn);
     GoToMain = findViewById(R.id.mainmenubtn);


     GoToMain.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             startActivity(new Intent(checkin1.this, MainActivity.class));
             SaveEditDetail();
         }
     });

     GoToJournal.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             startActivity(new Intent(checkin1.this, myJournal.class));
             SaveEditDetail();
         }
     });


 }
 // get info
 private void getCheckInDetails(){

     final ProgressDialog progressDialog = new ProgressDialog(this);
     progressDialog.setMessage("Loading...");
     progressDialog.show();

     StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ReadCheckin,
             new Response.Listener<String>() {
                 @Override
                 public void onResponse(String response) {
                     progressDialog.dismiss();
                     Log.i(TAG, response.toString());
                     try {
                         JSONObject jsonObject = new JSONObject(response);
                         String success = jsonObject.getString("success");
                         JSONArray jsonArray = jsonObject.getJSONArray("read");

                         if(success.equals("1")){
                             for (int i =0; i < jsonArray.length(); i++){
                                 JSONObject object= jsonArray.getJSONObject(i);

                                 String strUserName = object.getString("username").trim();
                                 String strEmail = object.getString("email").trim();
                                 String strAddress = object.getString("address").trim();
                                 String strPhone = object.getString("phone_number").trim();

                                 phone_number.setText(strPhone);
                                 address.setText(strAddress);
                                 username.setText(strUserName);
                                 email.setText(strEmail);
                             }
                         }
                     } catch (JSONException e) {
                         e.printStackTrace();
                         progressDialog.dismiss();
                         Toast.makeText(checkin1.this, "Error Reading Details " + e.toString(), Toast.LENGTH_SHORT).show();
                     }
                 }
             },
             new Response.ErrorListener() {
                 @Override
                 public void onErrorResponse(VolleyError error) {
                     progressDialog.dismiss();
                     Toast.makeText(checkin1.this, "Error Reading Details " + error.toString(), Toast.LENGTH_SHORT).show();

                 }
             })
     {
         @Override
         protected Map<String, String> getParams() throws AuthFailureError {
             Map<String, String> params = new HashMap<>();
             params.put("id", getId);
             return params;
         }
     };

     RequestQueue requestQueue = Volley.newRequestQueue(this);
     requestQueue.add(stringRequest);
 }
 @Override
 protected void onResume(){
     super.onResume();
     getCheckInDetails();
 }```

當程序嘗試使用具有 null 值的 object 引用時,將引發 NullPointerException。

sessionManager = new SessionManager(checkin1.this);
HashMap<String, String> user= sessionManager.GetCheckInInfo();
getId = user.get(sessionManager.ID);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM