0

This is my JSON from firebase database:

{
  "57c80ec0-e73f-49df-8296-25999e194288" : {
    "email" : "ss@zz.mm",
    "hotel" : {
      "-KEaYE1YfvPTRfgcO6Z_" : {
        "latit" : 20.15135303358223,
        "longit" : 23.86566750705242,
        "name" : "Westin",
        "rating" : 1,
        "review" : "hehehe hehehe hehehe hehehe hehehe hehehe hehehe"
      },
      "-KEaYGDhbFAN99GyY0cH" : {
        "latit" : 30.211601559876424,
        "longit" : -4.754576571285725,
        "name" : "Sheraton",
        "rating" : 5,
        "review" : "hohoho hohoho hohoho hohoho hohoho hohoho hohoho hohoho     hohoho hohoho"
      },
      "-KEbjCjmx4-Xmr_1aVCy" : {
        "latit" : 20.58906906737089,
        "longit" : 25.271570831537247,
        "name" : "Admiral",
        "rating" : 3,
        "review" : "hahaha hahaha hahaha hahaha hahaha hahaha hahaha hahaha     hahaha hahaha hahaha hahaha"
      }
    },
    "password" : "nn",
    "username" : "ss"
  },
  "c01528ea-a22c-43de-b77a-bc833c5e394c" : {
    "email" : "ses@ses.ses",
    "hotel" : {
      "-KEaLImMODDRiDwK3gSo" : {
        "latit" : 18.160196227874213,
        "longit" : 26.094263345003128,
        "name" : "Osijek",
        "rating" : 2,
        "review" : "lalala lalala lalala lalala lalala alalal lalala lalala     lalala lalalal"
      },
      "-KEaLKSEg1psa4xvggse" : {
        "latit" : 31.71881281252363,
        "longit" : -2.9933271557092667,
        "name" : "Slavonija",
        "rating" : 4,
        "review" : "aaaa aaaa aaaa aaaa aaaa aaaaa aaaaa aaaa aaaa aaaaa     aaaa aaaaaa aaaaa aa"
      }
    },
    "password" : "ses",
    "username" : "ses"
  }
}


@Override
    public Map<String, Object> rateHotel(String name, double lat, double lon) {
        Map<String, Object> hotel = new HashMap<>();
        hotel.put("name", name);
        hotel.put("latit", lat);
        hotel.put("longit", lon);
        return hotel;
    }

    @Override
    public void addHotel(String name, double lat, double lon) {
        String uid = firebase.getAuth().getUid();
        final Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid + "/hotel/");
        hotelsRef.push().setValue(rateHotel(name, lat, lon));
        presenter.onSuccess(name, lat, lon);
    }

This is how I pushed my hotels in a database. I log in and I have logged in user's list of hotels. when I click on one hotel from the list, I'm new activity where I want to edit some fields from that certain hotel.

How am I supposed to look for, or what firebase URL should I use to reach to a certain hotel key?

This is what url I've been using for pushing new hotel into firebase...it puts new hotel in "hotel" field and generates key for every new hotel. How can I get that key?

Firebase userRef = new Firebase("https://josip-my-application.firebaseio.com/Users/");
String uid = stringObjectMap.get("uid").toString();
userRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid + "/hotel/);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
joe
  • 1,341
  • 4
  • 21
  • 32
  • "I get null pointer exception for child path" [edit](http://stackoverflow.com/posts/36549400/edit) your question to include the full stack trace and error message. Point out what line numbers in the stack trace correspond to what lines in your code. With that we might be able to [find the cause of the NullPointerException](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – Frank van Puffelen Apr 11 '16 at 13:32
  • I assume it's because hotel's uId is null ... I'm still exploring Firebase and I'm not sure why is this field null..i posted my method for pushing hotel to firebase to check if I did something wrong. Should that hotel uid be auto generated or it is for example for hotel Osijek, appropriate key(""-KEaLImMODDRiDwK3gSo") ... or maybe I'm using wrong firebase url reference in MapActivity? – joe Apr 11 '16 at 14:24
  • From a quick scan of your JSON, there is no `uid` property in the hotel data, so that field in you Java will not be populated. Aside from that, Stack Overflow is a lousy debugger. Run the app in a debugger and it will break exactly on the line that is raising the exception. That will tell you what is null. – Frank van Puffelen Apr 11 '16 at 15:18
  • Let's ignore this hotel uid for a second. I have a uid field both in Hotel class and User class ... and if I want to reach certain user, for example first one, when I call String uid = firebase.getAuth().getUid(); final Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid); i get uid = "57c80ec0-e73f-49df-8296-25999e194288" just like above in JSON ... – joe Apr 11 '16 at 15:40
  • but the problem is, I don't know how to reach certain hotel, max I can reach is Firebase hotelsRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid + "/hotel/"); and I don't know how to dig deeper... hope you understand what I'm trying to ask – joe Apr 11 '16 at 15:40
  • As it says in Firebase guide: Every time you call push() Firebase generates a unique ID, like messages/users// ... but I have a problem how to access my hotel id? If I'm logged in I can change my data by calling this reference Firebase userRef = new Firebase("josip-my-application.firebaseio.com/Users/"; + uid); where uid = firebaseRef.getAuth().getUid(); if Fifrebase firebaseRef = new Firebase("josip-my-application.firebaseio.com/Users/"); ...but if I want to go deeper to change one certain hotel, I don't know how to reach it. That's basically the problem – joe Apr 11 '16 at 18:01
  • You seem to be confusing **push id** (which is generated when you call `push()`) with a uid (which is generated when a user authenticates). Your JSON has `$uid/hotels/$pushid`. Aside from that I have a hard time parsing your question. Is it possible that you *reduce* the question to a *minimum* complete example that reproduces your problem? – Frank van Puffelen Apr 11 '16 at 21:12
  • Sry, I'm not sure how to ask properly, or if it's dumb what I'm asking...I changed the question a little bit, hope it's a little bit clearer now – joe Apr 12 '16 at 07:25
  • If you can change how you push your hotel in the database, you could just do `hotelsRef.child(name).setValue(rateHotel(lat, lon));` (assuming your hotel names are unique). This way there would be no randomly generated key. The key would be the name of the hotel. – Pierre C. Apr 12 '16 at 07:37
  • Yeah, I thought about that too, but the problem could be if for example 3 users rate the same hotel differently. And when one user wants to edit, for example rating , it would be eddited for every hotel with the same name? ... I will have 2 lists, one with list of hotels for logged user and one for all the other user's hotels. And I want to allow only logged in user to change hotels from his list, so if he changes rating for hotel "Hilton" , every other user's hotel "Hilton" will remain unchanged – joe Apr 12 '16 at 07:55
  • Tnx Pierre, it's working this way! – joe Apr 13 '16 at 08:28

0 Answers0