0

I am having trouble reading the status of a boolean from my firebase database into a switch I have .

I keep getting a null pointer exception but the value exists in the database so not sure what i have done incorrect

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference

Fragment

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.display_match_details_fragment, container, false);


    //FIREBASE
    mFirebaseAuth = FirebaseAuth.getInstance();
    firebaseUser = mFirebaseAuth.getCurrentUser();
    final String uId = firebaseUser.getUid();

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    usersDatabase = mFirebaseDatabase.getReference().child("users");
    groupsDatabase = mFirebaseDatabase.getReference().child("groups");

    tvAddedBy = (TextView) view.findViewById(R.id.tvAddedByMatchDisplay);
    tvMatchTime = (TextView) view.findViewById(R.id.tvMatchTime);
    tvMatchDay = (TextView) view.findViewById(R.id.tvMatchDay);
    switchPlaying = (Switch) view.findViewById(R.id.switchPlay);

    btnConfirmStatus = (Button) view.findViewById(R.id.btnPlayingConfirm);

    switchPlaying.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean playingExtra) {
            Snackbar.make(compoundButton, "Playing: " + playingExtra, Snackbar.LENGTH_SHORT)
                    .setAction("ACTION", null).show();
        }
    });



    btnConfirmStatus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });



     readPlaying();


    return view;
}// end onCreate


// Read playing times from DB
private void readPlaying() {
    usersDatabase.child(firebaseUser.getUid()).child("playingExtra").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(!dataSnapshot.exists() || dataSnapshot.getValue() == null) {

                Toast.makeText(getActivity(), "NULL", Toast.LENGTH_SHORT).show();

            }
            else{
                Boolean playingExtra = Boolean.valueOf(dataSnapshot.getValue().toString());

                switchPlaying.setChecked(playingExtra);

            }
        }

Full Stack Trace

05-08 15:57:00.461 2392-2601/com..squad E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8e53e40
05-08 15:57:01.046 2392-2392/com.ciaranbyrne.squad E/AndroidRuntime: FATAL EXCEPTION: main

                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
                                                                         at android.support.design.widget.Snackbar.<init>(Snackbar.java:192)
                                                                         at android.support.design.widget.Snackbar.make(Snackbar.java:224)
                                                                         at com.ciaranbyrne.squad.DisplayMatchFragment$1.onCheckedChanged(DisplayMatchFragment.java:97)
                                                                         at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
                                                                         at android.widget.Switch.setChecked(Switch.java:1070)
                                                                         at com.ciaranbyrne.squad.DisplayMatchFragment$3.onDataChange(DisplayMatchFragment.java:134)
                                                                         at com.google.android.gms.internal.zzajp.zza(Unknown Source)
                                                                         at com.google.android.gms.internal.zzakp.zzcxi(Unknown Source)
                                                                         at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:168)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5845)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
05-08 15:57:15.694 2392-2601/com.ciaranbyrne.squad E/OpenGLRenderer: GL error:  Out of memory!
AL.
  • 36,815
  • 10
  • 142
  • 281
Ciaránimo
  • 517
  • 1
  • 9
  • 21
  • Please add your database structure. – Alex Mamo May 08 '17 at 14:48
  • 1
    First of all, when posting a question about a crash, the complete stack trace helps to pinpoint exactly which line is at fault. Second of all, the error message means that at the line at fault, you are doing something like `.getContext()` but the `ViewGroup` is `null`, so I'm not sure Firebase has anything to do with it. Finally, only `dataSnapshot.getValue(Boolean.class);` is necessary if the value has been set with `reference.setValue();` – Sunshinator May 08 '17 at 14:54
  • Added the stack trace as per suggestion, I just tried using Boolean playingExtra = dataSnapshot.getValue(Boolean.class); but no luck – Ciaránimo May 08 '17 at 15:00
  • Post your activity – Martin De Simone May 08 '17 at 15:01
  • Thank you for your comment, I am not too familiar with debugging in Android but after looking in the stack trace as per your suggestion it looks like it had something to with the SnackBar. Still learning, but thanks again for the tip – Ciaránimo May 08 '17 at 15:04
  • @Ciaránimo i made an edit, try now – Martin De Simone May 08 '17 at 15:10

3 Answers3

0

Try to use

Snackbar.make(findViewById(android.R.id.content), "Playing: " + String.valueOf(playingExtra), Snackbar.LENGTH_SHORT)
                .setAction("ACTION", null).show();
Martin De Simone
  • 2,108
  • 3
  • 16
  • 30
0

Assuming you have a CoordinatorLayout that looks like this:

CoordinatorLayout coordinatorLayout = (CoordinatorLayout) view.findViewById(R.id.coordinator_layout);

Than to use the Snackbar class, please use the following code:

Snackbar.make(coordinatorLayout, "Playing: " + playingExtra, Snackbar.LENGTH_LONG)
    .setAction("ACTION", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //do what you want
        }
     }).show();

Also try to change that line with:

dataSnapshot.getValue(Boolean.class);

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

I don't understand the details well enough to explain them, but there appear to be issues with creating a SnackBar in a Fragment before the fragment's view hierarchy has been added to the enclosing activity (related question).

You need to perform the processing in readPlaying() at a later point in the Fragment lifecycle. Move it out of onCreateView() and into onActivityCreated() or some lifecycle method that executes later, such as onStart().

Community
  • 1
  • 1
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158