-1

I created a class called UserInfo to get all user data from firebase database. Here is the method I used to get firebase data (username).

 public String getUsername() {
        final Firebase usernameLink = new Firebase(firebaseLink + getUid());
        usernameLink.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                username = (String) dataSnapshot.getValue();
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
        return username;
    }

And the firebase link has no problem. And it gives the username too.

But in another class I want to get the username.

So I made a object of that class..

UserInfo userInfo = new UserInfo();

And to get the username,

username = userInfo.getUsername();

I wrote the previous code in my onCreate method.

On a button click in my app, I am trying to add the username as my child.

send_request_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request_text = request_msg.getText().toString();
                URL = new Firebase(firebaseUrl);
               Firebase addChild = URL.child(username);
                                addChild.setValue("hhhhh");        
                        }
                );

Well this code works when I replace username with some dummy predefined string data. But when I add username it gives me the following error.

java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
 at com.firebase.client.Firebase.child(Firebase.java:187)
 at com.buckydroid.anonchat.SendRequest$1$1.onClick(SendRequest.java:67)
 at android.view.View.performClick(View.java:5637)
 at android.view.View$PerformClick.run(View.java:22433)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6126)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Doge
  • 853
  • 11
  • 35
  • 1
    Please have patience --- you just asked this question not 25 minutes ago! Also which line throws the NPE, and which variable is null? – Hovercraft Full Of Eels Jan 29 '17 at 14:36
  • username is throwing NPE and sorry @HovercraftFullOfEels – Doge Jan 29 '17 at 14:37
  • The line is Firebase addChild = NotificationURL.child(userInfo.getUsername()); – Doge Jan 29 '17 at 14:37
  • Then username is absolutely **not** the variable causing the NPE to be thown. Perhaps it's userInfo, or NotificationURL -- did you test which is null? If not, you will want to do so. – Hovercraft Full Of Eels Jan 29 '17 at 14:38
  • As I said, After changing NotificationURL.child(username); to NotificationURL.child("username"); or some other dummy value everything works correctly – Doge Jan 29 '17 at 14:41
  • The user name is loaded from Firebase asynchronously. By the time your `return` statement runs, it hasn't been loaded yet. Instead of explaining this again, I'll refer to an answer I wrote earlier today: http://stackoverflow.com/questions/41918459/firebase-android-fetchprovidersforemail-why-are-all-the-calls-asynchronous/41918701#41918701 – Frank van Puffelen Jan 29 '17 at 15:11
  • Can u plz give a little hint here on what should I do? I am little confused @FrankvanPuffelen – Doge Jan 29 '17 at 17:23

1 Answers1

1

Make sure your variable firebaseLink is initialized and is assigned to some data before running this method. The NullPointerException output I'm seeing tells me that you are trying to pass a null variable to the pathString variable of the Firebase class.

milkman
  • 44
  • 2
  • firebaseLink is correct. As I said when I replace username variable with some dummy string, It works . – Doge Jan 29 '17 at 14:46
  • Try casting the variable to a (String) when passing it to child(var) then. – milkman Jan 29 '17 at 14:49
  • 1
    Are you absolutely sure the user me variable is being initialized to begin with? Do a system.out.println(username) right before calling firebase.child() and see if it prints null – milkman Jan 29 '17 at 14:53
  • Yeah username is null – Doge Jan 29 '17 at 14:57
  • 1
    Boom. Move back farther in your code and try to find out why username isn't being initialized then try it again – milkman Jan 29 '17 at 14:58
  • I tried System.out.println in UserInfo class and it's working – Doge Jan 29 '17 at 15:04
  • Are you declaring the username variable inside the oncreate method? – milkman Jan 29 '17 at 15:09
  • I declared username variable before onCreate and associated userinfo.getUsername(); to username variable inside onCreate – Doge Jan 29 '17 at 15:10
  • Is there a constructor other than default in the user info class? You may need to pass it information when declaring your userinfo variable to initialize everything properly – milkman Jan 29 '17 at 15:15