0

I have been trying to fetch some uploaded data from firebase realtime database but the app keeps crashing.

I am going from "Profile_Tab" to "Project_Creation_Activity" to upload data to firebase.The code that handles uploading of data is given below

btnUpload.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                final ProgressDialog progressDialog=new ProgressDialog(Project_Creation_Activity.this);
                progressDialog.setMessage("Updating Info..");
                progressDialog.show();

                FirebaseUser currentUser=mAuth.getCurrentUser();

                if(projectTitle.getText().toString().equals(""))
                {
                    Toasty.error(Project_Creation_Activity.this,"Project title missing",Toasty.LENGTH_SHORT,true).show();
                }
                else
                {
                    if(projectDescription.getText().toString().equals(""))
                    {
                        Toasty.error(Project_Creation_Activity.this,"Project description missing",Toasty.LENGTH_SHORT,true).show();
                    }
                    else
                    {
                        String skill= Arrays.toString(skills);


                        //Adding project data to user
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("projectTitle").setValue(projectTitle.getText().toString());
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("skills").setValue(skill);
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("projectDescription").setValue(projectDescription.getText().toString());
                         mDatabase.child("users").child(uid).child("userProjects").child(projectTitle.getText().toString()).child("leaderUid").setValue(uid);

                        //Adding data to project
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("projectTitle").setValue(projectTitle.getText().toString());
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("skills").setValue(skill);
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("projectDescription").setValue(projectDescription.getText().toString());
                         mDatabase.child("projects").child(projectTitle.getText().toString()).child("leaderUid").setValue(uid);

                        Toasty.success(Project_Creation_Activity.this,"Uploaded",Toasty.LENGTH_SHORT,true).show();
                        Intent intent=new Intent(Project_Creation_Activity.this,Social_Home_Activity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                progressDialog.dismiss();
            }
        });

This takes the activity to the Profile tab where i try to fetch the new data as such

mDatabase.child("users").child(currentUser.getUid()).child("userProjects").addChildEventListener(new ChildEventListener(){
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
            {
                String title=(String)dataSnapshot.child("projectTitle").getValue();
                String description=(String)dataSnapshot.child("projectDescription").getValue();
                String skill=(String)dataSnapshot.child("skills").getValue();
                Log.i("Pratik",title);
                Log.i("Pratik",description);
                Log.i("Pratik",skill);
                Create_Project_Card(dataSnapshot.child("projectTitle").getValue().toString(),dataSnapshot.child("projectDescription").getValue().toString(),dataSnapshot.child("skills").getValue().toString(),view);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {
                Log.i("Pratik",databaseError.getDetails());
            }
        });

After I run the app and upload anything, The app crashes and I have to start the app again.After the upload process, I checked the firebase database for the newly uploaded data but only half the data got uploaded. I even tried adding a delay between the upload and the the startActivity(intent) so that the complete data gets uploaded but the app crashed again. The error is shown at the point where I try to fetch the data from firebase. It says that i have a null point exception. Even the complete data dosent get uploaded to firebase. I have to start the app again and then the data gets uploaded.

This is my database structure: enter image description here

Stack Trace from the crash:

2019-11-24 11:40:16.347 15445-15445/com.example.collaborator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.collaborator, PID: 15445
    java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.i(Log.java:179)
        at com.example.collaborator.Profile_Tab$4.onChildAdded(Profile_Tab.java:511)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:874)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The line Profile_Tab line 511 is:Log.i("Pratik",description);

  • Please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Nov 24 '19 at 06:06
  • That would mean that `description` is null. If that's allowed, then nothing is really wrong, there. You just can't pass a null value to a `Log` method. If you'd still like to log it, then you could do `String.valueOf(description)`, or `"" + description`, etc. – Mike M. Nov 24 '19 at 06:17
  • Ya i get that. I tried uploading all the data but only half the data gets uploaded before the app crashes. I even tried adding a delay to make up for the slow upload. How can i solve this problem?? – Pratik Baid Nov 24 '19 at 06:19
  • Well, it seems to me that you're updating only one value at a time, so when `"projectTitle"` updates first, the `ChildEventListener` fires, but the other values are still null. I'm not very familiar with Firebase, so you should probably wait for an expert in that field to come along, but I would really think that you can update a whole child at once, instead of one value at a time. Otherwise, just don't try logging those null values like that. – Mike M. Nov 24 '19 at 06:25

1 Answers1

0

firebase setValue() method is async method. So your every setValue() method will execute in background thread and your start activity code gets called irrespective of all data is send or not on firebase. It's better to make a model class and send full model on firebase. So in this way all item will be uploaded together on firebase. If you need help in code let me know.

Amit Tiwary
  • 787
  • 1
  • 8
  • 16
  • Thanks. It is working without crashing. I just made a class for the project and took all the input data and uploaded them all at once. Worked like a charm! – Pratik Baid Nov 24 '19 at 11:35