title: Android: push notifications crash application
body: <p>I implemented push notification in my application. They works well when the application is opened or it is in the background but when the the application is closed a pup up message on my screen is displayed saying that a problem occurred in the application.</p>
  
  <p>here there is the piece of code that implements push notifications.</p>
  
  <pre><code>public class GcmIntentService extends IntentService {
  public static final int NOTIFICATION_ID = 1;
  private NotificationManager mNotificationManager;
  NotificationCompat.Builder builder;
  FeedReaderDbHelperMessages mDbHelper;
  /**
   * Tag used on log messages.
   */
  static final String TAG = "GCM Intent";
  /**
   * Db access object
   * */
  
  public GcmIntentService() {
      super("GcmIntentService");
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
  
      Bundle extras = intent.getExtras();
      GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
      // The getMessageType() intent parameter must be the intent you received
      // in your BroadcastReceiver.
      String messageType = gcm.getMessageType(intent);
  
      if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
          /*
          /*
           * Filter messages based on message type. Since it is likely that GCM
           * will be extended in the future with new message types, just ignore
           * any message types you're not interested in, or that you don't
           * recognize.
           */
          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
              sendNotification(extras);
          } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
              sendNotification(extras);
              // If it's a regular GCM message, do some work.
          } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
              // Post notification of received message.
              sendNotification(extras);
              Intent i = new Intent("android.intent.action.MAIN").putExtras(extras);
              this.sendBroadcast(i);
              Log.i(TAG, "Received: " + extras.toString());
  
          }
      }
      // Release the wake lock provided by the WakefulBroadcastReceiver.
      GcmBroadcastReceiver.completeWakefulIntent(intent);
  }
  
  // Put the message into a notification and post it.
  // This is just one simple example of what you might choose to do with
  // a GCM message.
  private void sendNotification(Bundle msg) {
      FacebookSdk.sdkInitialize(getApplicationContext());
      Profile userProfile = Profile.getCurrentProfile();
      String user_id = userProfile.getId();
  
      mNotificationManager = (NotificationManager)
              this.getSystemService(Context.NOTIFICATION_SERVICE);
  
      Intent newIntent = new Intent(this, drawnerActivity.class);
      newIntent.setAction("OPEN_NOTIFICATION");
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0,newIntent, 0);
  
      mDbHelper = new FeedReaderDbHelperMessages(getApplicationContext());
      UpdateMessages updateMessages = new UpdateMessages(mDbHelper);
  
      if(msg.getString("notificationType").equals("question"))
      {
          NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(this)
                          .setSmallIcon(R.drawable.logo_notifica_domanda_mini)
                          .setContentTitle("MyApp")
                          .setStyle(new NotificationCompat.BigTextStyle().bigText("New question"))
                          .setContentText("New question");
  
          Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
          v.vibrate(400);
  
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
  
          updateMessages.update(user_id);
      }
      if(msg.getString("notificationType").equals("answer"))
      {
          Contact userAnswer = ContactListFragment.findContactById(msg.getString("friendId"));
          Timestamp notificationTimestamp = Timestamp.valueOf(msg.getString("timestamp"));
          Log.i(TAG, "Answer notification received from: "+ userAnswer.getName() + " timestamp: " + notificationTimestamp.toString());
          NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(this)
                          .setSmallIcon(R.drawable.logo_mini_dialog)
                          .setContentTitle("MyApp")
                          .setStyle(new NotificationCompat.BigTextStyle().bigText("New answer"))
                          .setContentText(userAnswer.getName() + " answered");
  
          Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
  
          long[] pattern = {
                  0,  // Start immediately
                  100,
                  100,
                  100
          };
  
          v.vibrate(pattern, -1);
  
          //updateMessages.updateRowWithAnswer(msg.getString("questionId"), msg.getString("answer"));
          FeedReaderDbHelperNotification mDbHelperNotifications = new FeedReaderDbHelperNotification(getApplicationContext());
          UpdateNotifications updateNotifications = new UpdateNotifications(mDbHelperNotifications, mDbHelper);
          updateNotifications.update(user_id);
          //NotificationFragment.addNotification(new NotificationImpl(notificationTimestamp, 1, 0, msg.getString("questionId"), ""), getApplication().getApplicationContext());
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>the problem is a <code>NullPointerException</code> here <code>String user_id = userProfile.getId();</code>. The userProfile cames from <code>com.facebook.Profile</code> class because I use <code>facebook sdk</code> to keep session. Why is it null? </p>
  
  <p>here there is the stacktrace</p>
  
  <pre><code> java.lang.NullPointerException
          at com.bellantoni.chetta.lieme.GcmIntentService.sendNotification(GcmIntentService.java:80)
          at com.bellantoni.chetta.lieme.GcmIntentService.onHandleIntent(GcmIntentService.java:63)
          at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:137)
          at android.os.HandlerThread.run(HandlerThread.java:61)
  </code></pre>