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);