java.lang.NullPointerException:尝试在空对象引用上调用接口方法'java.lang.Object java.util.Map.get(java.lang.Object)'

时间:2019-03-19 08:26:40

标签: java android

我已经使用uid从firebase检索所有数据,但这不起作用

这是我的MyAccountFragment代码:-

public class MyAccountFragment extends Fragment {
    DatabaseReference ref;
    FirebaseAuth auth;
    FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
    Toolbar toolbar;
    Button update;
    int b;
    EditText acc_name,acc_mobile,acc_email,acc_dob,acc_gender,acc_city,acc_state,acc_sml,acc_syp,acc_lic_no,acc_car_number,acc_car_color,acc_car_model;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_myaccount,container,false);
        toolbar = view.findViewById(R.id.toolbar);



            super.onCreate(savedInstanceState);
            acc_name=view.findViewById(R.id.acc_name);
            acc_mobile=view.findViewById(R.id.acc_mobile);
            acc_email=view.findViewById(R.id.acc_email);
            acc_dob=view.findViewById(R.id.acc_dob);
            acc_gender=view.findViewById(R.id.acc_gender);
            acc_city=view.findViewById(R.id.acc_city);
            acc_state=view.findViewById(R.id.acc_state);
            acc_sml=view.findViewById(R.id.acc_sml);
            acc_syp=view.findViewById(R.id.acc_syp);
        acc_lic_no=view.findViewById(R.id.acc_lic_no);
        acc_car_color=view.findViewById(R.id.acc_car_color);
        acc_car_number=view.findViewById(R.id.acc_car_number);
        acc_car_model=view.findViewById(R.id.acc_car_model);
           // update=view.findViewById(R.id.update);


            ref= FirebaseDatabase.getInstance().getReference(uid);
        Log.i(TAG, "teena : "+ref);
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                    String name=map.get("name1");//error goes here
                    String email=map.get("emailid1");
                    String phone=map.get("phone1");
                    String dob=map.get("dob1");
                    String gender=map.get("gender2");
                    String city=map.get("address1");
                    String state=map.get("state1");
                    String sml=map.get("sharelocmobile1");
                    String preference=map.get("preference1");
                    String license=map.get("lic_no1");
                    String car_number=map.get("car_number1");
                    String car_colour=map.get("car_colour1");
                    String car_model=map.get("car_model1");

                    acc_name.setText(Html.fromHtml("<font><b>" + "NAME : " + "</b></font>" +name));

                    acc_email.setText(Html.fromHtml("<font><b>" + "EMAIL : "+ "</b></font>"+email));

                    acc_mobile.setText(Html.fromHtml("<font><b>" + "MOBILE No. : "+ "</b></font>"+phone));

                    acc_dob.setText(Html.fromHtml("<font><b>" + "DOB : "+ "</b></font>"+dob));

                    acc_gender.setText(Html.fromHtml("<font><b>" + "GENDER : "+ "</b></font>"+gender));

                    acc_city.setText(Html.fromHtml("<font><b>" + "CITY : "+ "</b></font>"+city));

                    acc_state.setText(Html.fromHtml("<font><b>" + "STATE : "+ "</b></font>"+state));

                    acc_sml.setText(Html.fromHtml("<font><b>" + "SHARING MOBILE No. : "+ "</b></font>"+sml));

                    acc_syp.setText(Html.fromHtml("<font><b>" + "YOUR PREFERENCE : "+ "</b></font>"+preference));

                    acc_lic_no.setText(Html.fromHtml("<font><b>" + "LICENSE No. : "+ "</b></font>"+license));

                    acc_car_color.setText(Html.fromHtml("<font><b>" + "CAR COLOUR : "+ "</b></font>"+car_colour));

                    acc_car_model.setText(Html.fromHtml("<font><b>" + "CAR MODEL : "+ "</b></font>"+car_model));

                    acc_car_number.setText(Html.fromHtml("<font><b>" + "CAR NUMBER : "+ "</b></font>"+car_number));
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Log.i(TAG,"Error occured in connecting with database");
                }
            });
        return view;
    }

这是我的Logcat:-

 java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
        at com.sahayatra.samyatra.MyAccountFragment$1.onDataChange(MyAccountFragment.java:68)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)

2天前通过这种方法检索数据,但现在不是

这是我的User Class代码:

public class User2 {
    public String phone1;
    public String name1;
    public String emailid1;
    public String dob1;
    public String address1;
    public String state1;
    public String occupation1;
    public String car_number1;
    public String car_model1;
    public String car_colour1;
    public String lic_no1;
    public String sharelocmobile1;
    public String gender2;
    public String preference1;
    public String profile_picture_url1,fromo,too,time1,name2,phone2,sign_up_number1;


    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public User2(){

    }


    public User2(EditText editTextPhone, EditText name, EditText emailid, EditText dob, EditText address, EditText state, EditText occupation, EditText car_number, EditText car_model, EditText car_colour, EditText lic_no, EditText sharelocmobile, String gender1, String preference, Uri downloadUri) {
        phone1 = editTextPhone.getText().toString();
        name1 = name.getText().toString();
        emailid1 = emailid.getText().toString();
        dob1 = dob.getText().toString();
        address1 = address.getText().toString();
        state1 = state.getText().toString();
        occupation1 = occupation.getText().toString();
        car_number1 = car_number.getText().toString();
        car_model1 = car_model.getText().toString();
        car_colour1 = car_colour.getText().toString();
        lic_no1 = lic_no.getText().toString();
        sharelocmobile1 = sharelocmobile.getText().toString();
        this.gender2 = gender1;
        this.preference1 = preference;
        profile_picture_url1 = downloadUri.toString();
    }

    public User2(EditText from2,EditText to2,EditText editTextPhone, EditText name,EditText time2){
        fromo = from2.getText().toString();
        too=to2.getText().toString();
        phone1 = editTextPhone.getText().toString();
        name1 = name.getText().toString();
        time1=time2.getText().toString();
    }
    public User2(String name,String phone,String sign_up_number0)
    {
        this.name2=name;
        this.phone2=phone;
        this.sign_up_number1=sign_up_number0;
    }
}

我已经追溯了我的每一步,但是无法提出解决方案

任何帮助将不胜感激 谢谢!

0 个答案:

没有答案