2

Data is successfully being added to Firebase (confirmed through firebase console). But when I am trying to extract the Data I am getting NULL values.

package com.example.shaur.nimblenavigationdrawer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.firebase.database.ChildEventListener;
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 org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class all_books extends Fragment {

    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference rootRef = db.getReference();
    DatabaseReference bookRef = rootRef.child("BookAds");


    RecyclerView recyclerView;

    List<Books_getter_function> bookList;

    String b_name,b_price,b_desc,negotiate,contact,email;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_books, container, false);


        bookList = new ArrayList<>();
        final BookAdapter adapter = new BookAdapter(getActivity(),bookList);

        recyclerView = view.findViewById(R.id.recyclerView_books);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setAdapter(adapter);

//This hardcoded data is being displayed correctly.
        //bookList.add(new Books_getter_function("Ghatak 2008","200","[email protected]","Excellent book must buy","1233214390",R.drawable.book_blue_icon));  

        bookRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value = String.valueOf(dataSnapshot.getValue());
                //LOG IS WORKING FINE
                Log.i("Book",value);

                try {
                    JSONObject object = new JSONObject(value);
                    b_name = object.getString("BookName");
                    b_price = object.getString("BookPrice");
                    contact = object.getString("ContactNumber");
                    b_desc  = object.getString("Description");
                    negotiate = object.getString("Negotiable");
                    email = object.getString("Email");

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                bookList.add(new Books_getter_function(b_name,b_price,email,b_desc,contact,R.drawable.book_blue_icon));
                adapter.notifyDataSetChanged();
            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        return view;
    }
}

LOG value is correct : {UserId=IVXm0BS3NWZnsp7e6qz0W3bsYz2, BookPrice=122, Description=excellent, ContactNumber=9899999999, Negotiable=true, BookName=HK das, [email protected]}

The value PASSED by JSONObject to String b_name,b_price is null. Why is this happening? I have LOGGED the value string and it is giving me correct output now I just have to extract data.

I have verified everything else is correct added hardcoded data to check whether is gets displayed and it work completely fine.

The DataExtracted from FireBase by JSONObject is the Problem here.

OUTPUT

2
  • In the try catch Block, the Data passed to String is null, I can't figure out why? While the String value variable has correct data in it. Commented Nov 5, 2017 at 10:52
  • Most probably because your value string is not in proper JSON format? Shouldn't JSON be in { "name":"John", "age":31, "city":"New York" }? Commented Nov 5, 2017 at 11:15

1 Answer 1

1

In firebase the database is made of JSON, but you dont need to use JSONObject to get the data. You can simply do this, instead of:-

 b_name = object.getString("BookName");
                b_price = object.getString("BookPrice");
                contact = object.getString("ContactNumber");
                b_desc  = object.getString("Description");
                negotiate = object.getString("Negotiable");
                email = object.getString("Email");

do this:

String b_name=dataSnapshot.child("BookName").getValue().toString();
String b_price=dataSnapshot.child("BookPrice").getValue().toString();
String contact=dataSnapshot.child("ContactNumber").getValue().toString();
String b_desc=dataSnapshot.child("Description").getValue().toString();
String negotiate=dataSnapshot.child("Negotiable").getValue().toString();
String email=dataSnapshot.child("Email").getValue().toString();

the child() is an attribute in your database

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

2 Comments

Worked like a charm
@shauryauppal Glad I could help! happy coding

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.