title: How to retrieve value from firebase in android?
body: <p>I am new in using Firebase. I am trying to get data from Firebase, but when ever I am getting data from Firebase my application is getting crash. I am trying to get value from <code>msg</code>, my data structure is like this:-</p>
  
  <pre><code>users
   -KUWbdwMC7Nw9EIqKfHH
   msg: "none"
  </code></pre>
  
  <p>Notification Service class</p>
  
  <pre><code>public class NotificationListener extends Service {
  
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Toast.makeText(NotificationListener.this, "Service Started", Toast.LENGTH_SHORT).show();
  
  
          SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);
  
  
          String id = sharedPreferences.getString(Constants.UNIQUE_ID, null);
  
  
          Firebase firebase = new Firebase(Constants.FIREBASE_APP + id);
  
  
          firebase.addValueEventListener(new ValueEventListener() {
  
  
  
  
              @Override
              public void onDataChange(DataSnapshot snapshot) {
  
                  String msg = snapshot.child("msg").getValue().toString();
  
  
                  if (msg.equals("none"))
                      return;
  
                  showNotification(msg);
              }
  
              @Override
              public void onCancelled(FirebaseError firebaseError) {
                  Log.e("The read failed: ", firebaseError.getMessage());
              }
          });
  
          return START_STICKY;
      }
  
  
      private void showNotification(String msg){
          //Creating a notification
          NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
          builder.setSmallIcon(R.mipmap.ic_launcher);
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com"));
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
          builder.setContentIntent(pendingIntent);
          builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
          builder.setContentTitle("Firebase Push Notification");
          builder.setContentText(msg);
          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          notificationManager.notify(1, builder.build());
      }
  }
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>10-20 15:59:24.525 8000-8000/com.example.pitech09.mbz E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.pitech09.mbz, PID: 8000
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
      at com.example.pitech09.mbz.NotificationListener$1.onDataChange(NotificationListener.java:55)
      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
      at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
      at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5343)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
  </code></pre>