Data is being fetched from DB but still giving NullPointerException











up vote
1
down vote

favorite












I am working on a MapActivity where I am getting supplier information from my database and storing it in Supplier object. I am calling two functions fetchSupplier() and FetchStationInfo() where first one fetch supplier info and stores into Object and second one uses it. So, the problem is when I run the fetchSupplier() alone the data gets fetched and stored in my object but, when I call both functions i.e FetchStationInfo() after fetchSupplier() it gives a null pointer exception in FetchStationInfo(). Here's how I am calling the functions:



@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}


Both of my functions:



private void fetchSupplier(){
String uid = mAuth.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("SUPPLIERS").child(uid);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
supplier = dataSnapshot.getValue(Supplier.class);
Log.i("USER FETCH COMPLETE ","["+supplier.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("FETCHING USER [ERROR] ",databaseError.getMessage());
}
});
}

private void fetchStationInfo(){
//Exception is here in this line where I am calling `getStation_id()`
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("STATIONS").child(supplier.getStation_id());

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
station = dataSnapshot.getValue(Station.class);
Log.i("STATION FETCH COMPLETE ","["+station.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("STATION FETCH [ERROR] ",databaseError.getMessage());
}
});
}


My objects are declared globally. Been working on it for hours but unable to fix it. My firebase structure:
enter image description here










share|improve this question
























  • Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
    – PradyumanDixit
    Nov 7 at 15:29










  • @PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
    – Ali Hussam
    Nov 7 at 15:32










  • Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
    – Barns
    Nov 7 at 15:48












  • @Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
    – Ali Hussam
    Nov 7 at 15:56












  • Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
    – Barns
    Nov 7 at 16:28

















up vote
1
down vote

favorite












I am working on a MapActivity where I am getting supplier information from my database and storing it in Supplier object. I am calling two functions fetchSupplier() and FetchStationInfo() where first one fetch supplier info and stores into Object and second one uses it. So, the problem is when I run the fetchSupplier() alone the data gets fetched and stored in my object but, when I call both functions i.e FetchStationInfo() after fetchSupplier() it gives a null pointer exception in FetchStationInfo(). Here's how I am calling the functions:



@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}


Both of my functions:



private void fetchSupplier(){
String uid = mAuth.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("SUPPLIERS").child(uid);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
supplier = dataSnapshot.getValue(Supplier.class);
Log.i("USER FETCH COMPLETE ","["+supplier.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("FETCHING USER [ERROR] ",databaseError.getMessage());
}
});
}

private void fetchStationInfo(){
//Exception is here in this line where I am calling `getStation_id()`
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("STATIONS").child(supplier.getStation_id());

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
station = dataSnapshot.getValue(Station.class);
Log.i("STATION FETCH COMPLETE ","["+station.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("STATION FETCH [ERROR] ",databaseError.getMessage());
}
});
}


My objects are declared globally. Been working on it for hours but unable to fix it. My firebase structure:
enter image description here










share|improve this question
























  • Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
    – PradyumanDixit
    Nov 7 at 15:29










  • @PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
    – Ali Hussam
    Nov 7 at 15:32










  • Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
    – Barns
    Nov 7 at 15:48












  • @Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
    – Ali Hussam
    Nov 7 at 15:56












  • Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
    – Barns
    Nov 7 at 16:28















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am working on a MapActivity where I am getting supplier information from my database and storing it in Supplier object. I am calling two functions fetchSupplier() and FetchStationInfo() where first one fetch supplier info and stores into Object and second one uses it. So, the problem is when I run the fetchSupplier() alone the data gets fetched and stored in my object but, when I call both functions i.e FetchStationInfo() after fetchSupplier() it gives a null pointer exception in FetchStationInfo(). Here's how I am calling the functions:



@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}


Both of my functions:



private void fetchSupplier(){
String uid = mAuth.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("SUPPLIERS").child(uid);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
supplier = dataSnapshot.getValue(Supplier.class);
Log.i("USER FETCH COMPLETE ","["+supplier.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("FETCHING USER [ERROR] ",databaseError.getMessage());
}
});
}

private void fetchStationInfo(){
//Exception is here in this line where I am calling `getStation_id()`
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("STATIONS").child(supplier.getStation_id());

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
station = dataSnapshot.getValue(Station.class);
Log.i("STATION FETCH COMPLETE ","["+station.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("STATION FETCH [ERROR] ",databaseError.getMessage());
}
});
}


My objects are declared globally. Been working on it for hours but unable to fix it. My firebase structure:
enter image description here










share|improve this question















I am working on a MapActivity where I am getting supplier information from my database and storing it in Supplier object. I am calling two functions fetchSupplier() and FetchStationInfo() where first one fetch supplier info and stores into Object and second one uses it. So, the problem is when I run the fetchSupplier() alone the data gets fetched and stored in my object but, when I call both functions i.e FetchStationInfo() after fetchSupplier() it gives a null pointer exception in FetchStationInfo(). Here's how I am calling the functions:



@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}


Both of my functions:



private void fetchSupplier(){
String uid = mAuth.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("SUPPLIERS").child(uid);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
supplier = dataSnapshot.getValue(Supplier.class);
Log.i("USER FETCH COMPLETE ","["+supplier.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("FETCHING USER [ERROR] ",databaseError.getMessage());
}
});
}

private void fetchStationInfo(){
//Exception is here in this line where I am calling `getStation_id()`
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("STATIONS").child(supplier.getStation_id());

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
station = dataSnapshot.getValue(Station.class);
Log.i("STATION FETCH COMPLETE ","["+station.getName()+"]");
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("STATION FETCH [ERROR] ",databaseError.getMessage());
}
});
}


My objects are declared globally. Been working on it for hours but unable to fix it. My firebase structure:
enter image description here







android firebase google-maps firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 15:31

























asked Nov 7 at 15:24









Ali Hussam

15410




15410












  • Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
    – PradyumanDixit
    Nov 7 at 15:29










  • @PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
    – Ali Hussam
    Nov 7 at 15:32










  • Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
    – Barns
    Nov 7 at 15:48












  • @Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
    – Ali Hussam
    Nov 7 at 15:56












  • Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
    – Barns
    Nov 7 at 16:28




















  • Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
    – PradyumanDixit
    Nov 7 at 15:29










  • @PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
    – Ali Hussam
    Nov 7 at 15:32










  • Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
    – Barns
    Nov 7 at 15:48












  • @Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
    – Ali Hussam
    Nov 7 at 15:56












  • Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
    – Barns
    Nov 7 at 16:28


















Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
– PradyumanDixit
Nov 7 at 15:29




Please add your firebase structure too, so we can see if there is something going wrong with fetching the value from database
– PradyumanDixit
Nov 7 at 15:29












@PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
– Ali Hussam
Nov 7 at 15:32




@PradyumanDixit I don't think theres anything wrong with the structure but I just edited my answer.
– Ali Hussam
Nov 7 at 15:32












Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
– Barns
Nov 7 at 15:48






Just curious: Does any supplier have more than one "Station"? (From your data example your data structure looks pretty flat.) If each supplier does have only one station then there really is no reason to have the data separated into two tables.
– Barns
Nov 7 at 15:48














@Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
– Ali Hussam
Nov 7 at 15:56






@Barns It is like each supplier does have one station but each station will have multiple suppliers. So I am somehow using the concepts of reference keys and stuff here. Sorry if I am being stupid this is my first experience with a NoSql Database. Plus there's a lot to be added.
– Ali Hussam
Nov 7 at 15:56














Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
– Barns
Nov 7 at 16:28






Not at all! You are NOT being "stupid"! Coming from a Relational Database background moving to the concept of NoSQL is not easy! And my experience with NoSQL is quite limited. I just wanted to see if there was any potential advantage in combining the two "tables". Keep coding! Good Luck!
– Barns
Nov 7 at 16:28














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The problem is that fetchStationInfo() is trying to fetch supplier from fetchSupplier() .



Remember that Firebase calls are asynchronous and you are trying to fetch suppliers while it's being fetched.



So, if you do this



public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fetchSupplier();
fetchStationInfo();
}


The two methods will fire and since they are asynchronous you don't know which one ends first, so, if fetchStationInfo ends first it will have suppliers with a null value since fetchSupplier did not finished fetching the result yet.



In order to fix this, you can make a simple interface after fetchSuppliers() that will notify when the job is done, after that job is done , in the callback of that interface you run fetchStationInfo() , in this order you will always have the suppliers info before trying to fetch it in fetchStationInfo().



This is know as a Race Condition.



Please visit these links in order to understand better how it works:



How to return dataSnapshot value as a result of a method?



android - Firebase return value from datasnapshot Why?






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192493%2fdata-is-being-fetched-from-db-but-still-giving-nullpointerexception%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    The problem is that fetchStationInfo() is trying to fetch supplier from fetchSupplier() .



    Remember that Firebase calls are asynchronous and you are trying to fetch suppliers while it's being fetched.



    So, if you do this



    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    fetchSupplier();
    fetchStationInfo();
    }


    The two methods will fire and since they are asynchronous you don't know which one ends first, so, if fetchStationInfo ends first it will have suppliers with a null value since fetchSupplier did not finished fetching the result yet.



    In order to fix this, you can make a simple interface after fetchSuppliers() that will notify when the job is done, after that job is done , in the callback of that interface you run fetchStationInfo() , in this order you will always have the suppliers info before trying to fetch it in fetchStationInfo().



    This is know as a Race Condition.



    Please visit these links in order to understand better how it works:



    How to return dataSnapshot value as a result of a method?



    android - Firebase return value from datasnapshot Why?






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      The problem is that fetchStationInfo() is trying to fetch supplier from fetchSupplier() .



      Remember that Firebase calls are asynchronous and you are trying to fetch suppliers while it's being fetched.



      So, if you do this



      public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      fetchSupplier();
      fetchStationInfo();
      }


      The two methods will fire and since they are asynchronous you don't know which one ends first, so, if fetchStationInfo ends first it will have suppliers with a null value since fetchSupplier did not finished fetching the result yet.



      In order to fix this, you can make a simple interface after fetchSuppliers() that will notify when the job is done, after that job is done , in the callback of that interface you run fetchStationInfo() , in this order you will always have the suppliers info before trying to fetch it in fetchStationInfo().



      This is know as a Race Condition.



      Please visit these links in order to understand better how it works:



      How to return dataSnapshot value as a result of a method?



      android - Firebase return value from datasnapshot Why?






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        The problem is that fetchStationInfo() is trying to fetch supplier from fetchSupplier() .



        Remember that Firebase calls are asynchronous and you are trying to fetch suppliers while it's being fetched.



        So, if you do this



        public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        fetchSupplier();
        fetchStationInfo();
        }


        The two methods will fire and since they are asynchronous you don't know which one ends first, so, if fetchStationInfo ends first it will have suppliers with a null value since fetchSupplier did not finished fetching the result yet.



        In order to fix this, you can make a simple interface after fetchSuppliers() that will notify when the job is done, after that job is done , in the callback of that interface you run fetchStationInfo() , in this order you will always have the suppliers info before trying to fetch it in fetchStationInfo().



        This is know as a Race Condition.



        Please visit these links in order to understand better how it works:



        How to return dataSnapshot value as a result of a method?



        android - Firebase return value from datasnapshot Why?






        share|improve this answer














        The problem is that fetchStationInfo() is trying to fetch supplier from fetchSupplier() .



        Remember that Firebase calls are asynchronous and you are trying to fetch suppliers while it's being fetched.



        So, if you do this



        public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        fetchSupplier();
        fetchStationInfo();
        }


        The two methods will fire and since they are asynchronous you don't know which one ends first, so, if fetchStationInfo ends first it will have suppliers with a null value since fetchSupplier did not finished fetching the result yet.



        In order to fix this, you can make a simple interface after fetchSuppliers() that will notify when the job is done, after that job is done , in the callback of that interface you run fetchStationInfo() , in this order you will always have the suppliers info before trying to fetch it in fetchStationInfo().



        This is know as a Race Condition.



        Please visit these links in order to understand better how it works:



        How to return dataSnapshot value as a result of a method?



        android - Firebase return value from datasnapshot Why?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 22:49









        halfer

        14.2k757106




        14.2k757106










        answered Nov 7 at 15:40









        Gastón Saillén

        2,9842828




        2,9842828






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192493%2fdata-is-being-fetched-from-db-but-still-giving-nullpointerexception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini