0

I'm the beginner for developing Android Apps. I want to do the function which let the user be able to add the car plate into the firebase-database. I will be using the ArrayList to let the user to add a list of car plate into the system. The code will be shown like that:

    List<String>carPlate=new ArrayList<String>();

 mDatabase = FirebaseDatabase.getInstance().getReference();
    user = FirebaseAuth.getInstance().getCurrentUser();
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            phone = Phone.getText().toString();
            ic = IC.getText().toString();
            fullname = FullName.getText().toString();
            carplate = CarPlate.getText().toString();
            carPlate.add(carplate);

           //this data will be successful storing in firebase-database and the results will be the images above
            Account account = new Account(fullname, phone, carPlate, ic);
            if(user!=null) {
                mDatabase.child(user.getUid()).setValue(account);
                Toast.makeText(getBaseContext(), "Register Successfully...", Toast.LENGTH_LONG).show();
                Intent in = new Intent(RegisterPage.this, MainActivity.class);
                startActivity(in);
                finish();
            }

        }
    });
}

Besides, another class which contain the update function as shown at below:

THE PROBLEM IS AT HERE

Now I want to update the data. According to this Firebase-database, I try to map.get all the value and then using setter for encapsulation. After that, I remove the data from firebase-database. Then I create new object and then put all the task inside. Finally, I setvalue to firebase-database. After I update the new data, the firebase-database will be automatically push to second level.

First Result

When I update again, it will push it again

Second Result

The second problem is NullException at this line: phoneAcc.set_IC(map.get("_IC"));

The error

E/UncaughtException: java.lang.NullPointerException
                 at com.google.online_mobile_flexi_parking.ChangeEmailPassword$8$1.onDataChange(ChangeEmailPassword.java:251)
                 at com.google.android.gms.internal.zzakg.zza(Unknown Source)
                 at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source)
                 at com.google.android.gms.internal.zzalj$1.run(Unknown Source)
                 at android.os.Handler.handleCallback(Handler.java:733)
                 at android.os.Handler.dispatchMessage(Handler.java:95)
                 at android.os.Looper.loop(Looper.java:136)
                 at android.app.ActivityThread.main(ActivityThread.java:5028)
                 at java.lang.reflect.Method.invokeNative(Native Method)
                 at java.lang.reflect.Method.invoke(Method.java:515)
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
                 at dalvik.system.NativeStart.main(Native Method)

and here is my code:

private List CarPlate;
     changePhone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.setVisibility(View.VISIBLE);
            txtPhone = newPhone.getText().toString().trim();
            if (!txtPhone.equals("")) {
                mDatabase.child(user.getUid()).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String key=mDatabase.child(user.getUid()).getKey();
                        Account phoneAcc=new Account();
                        Map<String,String>map=(Map)dataSnapshot.getValue();
                        phoneAcc.set_Key(mDatabase.child(user.getUid()).getKey());
                        phoneAcc.set_IC(map.get("_IC"));
                        phoneAcc.set_Phone(txtPhone);
                        phoneAcc.set_FullName(map.get("_FullName"));
                        CarPlate.add(map.get("_CarPlate0"));
                        phoneAcc.set_CarPlate0(CarPlate);

                        //dataSnapshot.getRef().removeValue();

                        mDatabase.child(mDatabase.child(user.getUid()).getKey()).removeValue();
                        Map<String,Object> task=new HashMap<String,Object>();
                        task.put("_FullName",phoneAcc.get_FullName());
                        task.put("_CarPlate0",phoneAcc.get_CarPlate0());
                        task.put("_Phone",phoneAcc.get_Phone());
                        task.put("_IC",phoneAcc.get_IC());
                        mDatabase.child(phoneAcc.get_Key()).setValue(task);
 Toast.makeText(ChangeEmailPassword.this, "Phone Number is updated, sign in with new password!", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(ChangeEmailPassword.this, MainActivity.class));
                            finish();
                            signOut();
  progressBar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });


                } else if (txtPhone.equals("")) {
                    newPhone.setError("Enter phone number");
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jane
  • 1
  • Every time you update a record, you add a new child. I would recommend you figure that out before worrying about the NullPointerException – OneCricketeer Dec 23 '16 at 02:01
  • How to do that? Would you teach me? – Jane Dec 23 '16 at 02:02
  • Actually `map.get("_IC")` is trying to return an Integer? See http://stackoverflow.com/questions/1811706/java-null-pointer-exception-when-unboxing-integer – OneCricketeer Dec 23 '16 at 02:07
  • How do you know that that is integer? Because in encapsulation class I have set the IC into String class – Jane Dec 23 '16 at 02:31
  • Just a guess? The image shows 52 is the value of _IC – OneCricketeer Dec 23 '16 at 02:32
  • Basically, I cannot tell whatever is throwing a NullPointerException. The Map will return null, yes, when the key does not exist, so you should debug your code a bit to see what data the map actually contains – OneCricketeer Dec 23 '16 at 02:35

0 Answers0