title: UpdateAppWidget Null Pointer Exception
body: <p>Following is the static method for updating the appwidget:</p>
  
  <pre><code>    public static void updateAppWidget(Context context, int id, WALL_POST_DETAILS post){
      try{
          System.out.println("In updateAppWidget==&gt;"+id);
          AppWidgetManager mngr=AppWidgetManager.getInstance(context);
  
          String diff=diff(post.created_time.trim());
  
          RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widget_layout);         
          PendingIntent pending=PendingIntent.getActivity(context, 0, new Intent(context,ShareDialogActivity.class), 0);
          views.setOnClickPendingIntent(R.id.TextView01, pending);
          views.setOnClickPendingIntent(R.id.Button03, pending);
  
          views.setTextViewText(R.id.actor_name, post.actor_name);
          views.setTextViewText(R.id.actor_message, post.message);
          views.setImageViewBitmap(R.id.actor_photo, post.image);
          views.setTextViewText(R.id.created_time, diff);
          if(post.no_of_comments&gt;0){
              views.setViewVisibility(R.id.comments_image, View.VISIBLE);
              views.setViewVisibility(R.id.comment_word, View.VISIBLE);               
              views.setTextViewText(R.id.no_of_comments, String.valueOf(post.no_of_comments));
              views.setViewVisibility(R.id.no_of_comments, View.VISIBLE);
              System.out.println("No. of Comments==&gt;"+post.no_of_comments);
          }else{
              views.setViewVisibility(R.id.comments_image, View.INVISIBLE);
              views.setViewVisibility(R.id.comment_word, View.INVISIBLE);
              views.setTextViewText(R.id.no_of_comments, String.valueOf(""));
              views.setViewVisibility(R.id.no_of_comments, View.INVISIBLE);
          }
  
          if(post.no_of_likes&gt;0){
              views.setViewVisibility(R.id.likes_image, View.VISIBLE);
              views.setViewVisibility(R.id.likes_word, View.VISIBLE);
              views.setTextViewText(R.id.no_of_likes, String.valueOf(post.no_of_likes));
              views.setViewVisibility(R.id.no_of_likes, View.VISIBLE);
          }else{
              views.setViewVisibility(R.id.likes_image, View.INVISIBLE);
              views.setViewVisibility(R.id.likes_word, View.INVISIBLE);
              views.setTextViewText(R.id.no_of_likes, String.valueOf(""));
              views.setViewVisibility(R.id.no_of_likes, View.INVISIBLE);
          }
  
          Intent intent_next=new Intent(context,GetPostService.class);
          Bundle b_next=new Bundle();
          b_next.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
          b_next.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.NEXT_FB_POST);
          intent_next.putExtras(b_next);          
          PendingIntent nextPostService=PendingIntent.getService(context, MyWidgetProvider.NEXT_FB_POST, intent_next, PendingIntent.FLAG_UPDATE_CURRENT);
          views.setOnClickPendingIntent(R.id.next_button, nextPostService);
  
          Intent intent_prev=new Intent(context,GetPostService.class);
          Bundle b_prev=new Bundle();
          b_prev.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
          b_prev.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.PREV_FB_POST);
          intent_prev.putExtras(b_prev);
          PendingIntent prevPostService=PendingIntent.getService(context, MyWidgetProvider.PREV_FB_POST, intent_prev, PendingIntent.FLAG_UPDATE_CURRENT);
          views.setOnClickPendingIntent(R.id.prev_button, prevPostService);
  
          Intent intent_comment=new Intent(context,FBShowCommentsActivity.class);
          Bundle b_comment=new Bundle();
          b_comment.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
          b_comment.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.SHOW_COMMENTS);
          b_comment.putString(MyWidgetProvider.POST_ID, SessionStore.restoreCurrentPostId(context));
          intent_comment.putExtras(b_comment);
          PendingIntent fbActionActivity=PendingIntent.getActivity(context, MyWidgetProvider.SHOW_COMMENTS, intent_comment, PendingIntent.FLAG_UPDATE_CURRENT);
          views.setOnClickPendingIntent(R.id.comment_word, fbActionActivity);
          views.setOnClickPendingIntent(R.id.comments_image, fbActionActivity);
          views.setOnClickPendingIntent(R.id.no_of_comments, fbActionActivity);
  
          Intent intent_likes=new Intent(context,ShowLikeDialogActivity.class);
          Bundle b_likes=new Bundle();
          b_likes.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
          b_likes.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.SHOW_LIKES);
          b_likes.putString(MyWidgetProvider.POST_ID, SessionStore.restoreCurrentPostId(context));
          intent_likes.putExtras(b_likes);
          PendingIntent fbLikesActivity=PendingIntent.getActivity(context, MyWidgetProvider.SHOW_LIKES, intent_likes, PendingIntent.FLAG_UPDATE_CURRENT);
          views.setOnClickPendingIntent(R.id.likes_word, fbLikesActivity);
          views.setOnClickPendingIntent(R.id.likes_image, fbLikesActivity);
          views.setOnClickPendingIntent(R.id.no_of_likes, fbLikesActivity);
  
          mngr.updateAppWidget(id, views);
  
      }catch(Exception e){
          e.printStackTrace();
          System.out.println("Exception==&gt;"+e.getMessage());
      }
  }
  </code></pre>
  
  <p>AppWidget is updating successfully from GetPostService pending service. When Pending Activity is launched first time after installation, I can update the widget successfully. But If I again launch the second time, Activity is launched properly, but AppWidget is not getting update and It throws null pointer exception. I am updating the widget from UI thread (from onPostExecute() method of AsyncTask).</p>
  
  <blockquote>
    <p>W/System.err( 522): at android.widget.RemoteViews$ReflectionAction.writeToPa rcel(RemoteViews.java:400) W/System.err( 522): at android.widget.RemoteViews.writeToParcel(RemoteViews. java:1012) W/System.err( 522): at com.android.internal.appwidget.IAppWidgetService$Stub $Proxy.updateAppWidgetIds(IAppWidgetService.java:374) W/System.err( 522): at android.appwidget.AppWidgetManager.updateAppWidget(Ap pWidgetManager.java:246) W/System.err( 522): at android.appwidget.AppWidgetManager.updateAppWidget(Ap pWidgetManager.java:265) W/System.err( 522): at com.mobisys.android.app_widget.MyWidgetProvider.updat eAppWidget(MyWidgetProvider.java:185) W/System.err( 522): at com.mobisys.android.app_widget.MyWidgetProvider.onRec eive(MyWidgetProvider.java:71) W/System.err( 522): at android.app.ActivityThread.handleReceiver(ActivityThr ead.java:1769) W/System.err( 522): at android.app.ActivityThread.access$2400(ActivityThread .java:117) W/System.err( 522): at android.app.ActivityThread$H.handleMessage(ActivityTh read.java:978) W/System.err( 522): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 522): at android.os.Looper.loop(Looper.java:123) W/System.err( 522): at android.app.ActivityThread.main(ActivityThread.java:3 647) W/System.err( 522): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 522): at java.lang.reflect.Method.invoke(Method.java:507) W/System.err( 522): at com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err( 522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597)
    W/System.err( 522): at dalvik.system.NativeStart.main(Native Method)</p>
  </blockquote>
  
  <p>I am calling the updateAppWidget static method in following way from Pending Activity:</p>
  
  <pre><code>MyWidgetProvider.updateAppWidget(context, mAppWidgetId, post); //context is pending Activity's instance
  </code></pre>
  
  <p>What can be the issue?</p>
  
  <p>Thanks in advance.</p>