0

I have a Firebase Database which is to be used by two Android projects. I am trying to add data from one project / app and retrieve data in another by following various tutorials on YouTube. However, I am having a tough time retrieving data. Here is a snapshot of my database.

enter image description here

My app loads the UI, but crashes after I run the following code and click on the button Get Data.

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Attr;

import java.util.jar.Attributes;
import java.util.*;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Main2Activity extends AppCompatActivity {

    DatabaseReference reff;

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

        final TextView text1=(TextView)findViewById(R.id.textView);
        final TextView text2=(TextView)findViewById(R.id.textView2);
        final TextView text3=(TextView)findViewById(R.id.textView3);
        final TextView text4=(TextView)findViewById(R.id.textView4);
        final TextView text5=(TextView)findViewById(R.id.textView5);
        final TextView text6=(TextView)findViewById(R.id.textView6);
        final TextView text7=(TextView)findViewById(R.id.textView7);
        final TextView text8=(TextView)findViewById(R.id.textView8);
        final TextView text9=(TextView)findViewById(R.id.textView9);
        final TextView text10=(TextView)findViewById(R.id.textView10);
        final TextView text11=(TextView)findViewById(R.id.textView11);
        final TextView text12=(TextView)findViewById(R.id.textView12);
        final TextView text13=(TextView)findViewById(R.id.textView13);
        final TextView text14=(TextView)findViewById(R.id.textView14);
        Button btn=(Button)findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");
                reff.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        int attribute1=Integer.parseInt(dataSnapshot.child("attribute1").getValue().toString());
                        int attribute2=Integer.parseInt(dataSnapshot.child("attribute2").getValue().toString());
                        int attribute3=Integer.parseInt(dataSnapshot.child("attribute3").getValue().toString());
                        int attribute4=Integer.parseInt(dataSnapshot.child("attribute4").getValue().toString());
                        int attribute5=Integer.parseInt(dataSnapshot.child("attribute5").getValue().toString());
                        int attribute6=Integer.parseInt(dataSnapshot.child("attribute6").getValue().toString());
                        int attribute7=Integer.parseInt(dataSnapshot.child("attribute7").getValue().toString());
                        int attribute8=Integer.parseInt(dataSnapshot.child("attribute8").getValue().toString());
                        int attribute9=Integer.parseInt(dataSnapshot.child("attribute9").getValue().toString());
                        int attribute10=Integer.parseInt(dataSnapshot.child("attribute10").getValue().toString());
                        int attribute11=Integer.parseInt(dataSnapshot.child("attribute11").getValue().toString());
                        int attribute12=Integer.parseInt(dataSnapshot.child("attribute12").getValue().toString());
                        int attribute13=Integer.parseInt(dataSnapshot.child("attribute13").getValue().toString());
                        int attribute14=Integer.parseInt(dataSnapshot.child("attribute14").getValue().toString());

                        text1.setText(attribute1);
                        text2.setText(attribute2);
                        text3.setText(attribute3);
                        text4.setText(attribute4);
                        text5.setText(attribute5);
                        text6.setText(attribute6);
                        text7.setText(attribute7);
                        text8.setText(attribute8);
                        text9.setText(attribute9);
                        text10.setText(attribute10);
                        text11.setText(attribute11);
                        text12.setText(attribute12);
                        text13.setText(attribute13);
                        text14.setText(attribute14);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }
        });

    }
}

I get the following error:-

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appdoctor, PID: 4920
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.example.appdoctor.Main2Activity$1$1.onDataChange(Main2Activity.java:56)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        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:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Process 4920 terminated.

Things that I've tried:-

  • Added the google-services.json file
  • Added classpath 'com.google.gms:google-services:4.3.3' in Project Gradle
  • Added apply plugin: 'com.google.gms.google-services' in Build Gradle
  • Changed the variables attribute1 - attribute10 from String to Integer

Any help is really appreciated. Thanks!

0

2 Answers 2

1

Change this:

reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");

into this:

reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("-M0pSXmPNPbXa3vgQeuc");

The node contains a dash child("-M0pSXmPNPbXa3vgQeuc");

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for late reply. Unfortunately it didn't solve the problem.
0

I figured it out. I solved the NullPointerException by initializing attribute1 - attribute14

String attribute1=" ";
attribute1=dataSnapshot.child("attribute1").getValue().toString();
text1.setText(attribute1);

This was helpful What is a NullPointerException, and how do I fix it?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.