title: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference error on OnPostExecute() - AsyncTask
body: <p>I am trying to get a result from an <code>AsyncTask</code> to be shown in my search activity.</p>
  
  <p>I had a example from a previous app, that was made with help from a colleague(i was an intern there).</p>
  
  <p>I adjusted the example to the needs of this app, but now Im getting the <code>NullPointerException</code>:</p>
  
  <pre><code>03-04 03:50:23.865: E/AndroidRuntime(8224): FATAL EXCEPTION: main 
  03-04 03:50:23.865: E/AndroidRuntime(8224): Process: com.cyberdog.what2watch, PID: 8224
  03-04 03:50:23.865: E/AndroidRuntime(8224): java.lang.NullPointerException: Attempt to invoke interface method 'void com.cyberdog.what2watch.JsonHandling$IOnFinish.onGetData(org.json.JSONObject)' on a null object reference
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at com.cyberdog.what2watch.JsonHandlingTMDBAsync.onPostExecute(JsonHandling.java:321)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at com.cyberdog.what2watch.JsonHandlingTMDBAsync.onPostExecute(JsonHandling.java:1)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.os.AsyncTask.finish(AsyncTask.java:632)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.os.Handler.dispatchMessage(Handler.java:102)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.os.Looper.loop(Looper.java:135)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at android.app.ActivityThread.main(ActivityThread.java:5274)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at java.lang.reflect.Method.invoke(Native Method)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at java.lang.reflect.Method.invoke(Method.java:372)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
  03-04 03:50:23.865: E/AndroidRuntime(8224):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
  </code></pre>
  
  <p>I also checked other samples on <code>onPostExecute()</code>, and it seems that i did the right thing.. but still getting the error.</p>
  
  <p>I have used a breakpoint at the <code>onPostExecute()</code> and the result is not null, altho it does run the code twice.</p>
  
  <p>code of the asyncclass:</p>
  
  <pre><code>class JsonHandlingTMDBAsync extends AsyncTask&lt;String, Void, JSONObject&gt; {// params,
                                                                          // progress,
                                                                          // result
  
  private String api_key_tmdb = "2fb98d2bca5895c89a6efaf70903f706";
  private String tmdb_multi_url = "http://api.themoviedb.org/3/search/multi?query=";
  int Total_pages;
  int number_entries_per_page;
  private JSONObject resultObject;
  private IOnFinish listener;
  String Titel="";
  String Year ="";
  public JsonHandlingTMDBAsync(String titel, String year, IOnFinish listener) {
      this.listener = listener;
      this.Titel=titel;
      this.Year=year;
  }
  
  @Override
  protected JSONObject doInBackground(String... params) {
      return loadJSON(params);
  }
  
  private JSONObject loadJSON(String... params) {
      resultObject = new JSONObject();
      StringBuilder builder = new StringBuilder();
      HttpClient client = new DefaultHttpClient();
      HttpGet httpGet;
  
      if (Titel.trim().length() == 0 &amp;&amp; Year.trim().length() != 0) {
          httpGet = new HttpGet(tmdb_multi_url + Year + "&amp;api_key="
                  + api_key_tmdb);
      } else if (Titel.trim().length() != 0 &amp;&amp; Year.trim().length() == 0) {
          httpGet = new HttpGet(tmdb_multi_url + Titel + "&amp;api_key="
                  + api_key_tmdb);
  
      } else if (Titel.trim().length() == 0 &amp;&amp; Year.trim().length() == 0) {
          System.out.print("error null titel/year");
          httpGet = new HttpGet(tmdb_multi_url + "star+wars&amp;api_key="
                  + api_key_tmdb);
      } else {
          httpGet = new HttpGet(
                  "http://www.omdbapi.com/?s=star&amp;y=2008&amp;r=JSON"
                          + "&amp;plot=short");
      }
  
      try {
          HttpResponse response = client.execute(httpGet);
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          if (statusCode == 200) {
              HttpEntity entity = response.getEntity();
              InputStream content = entity.getContent();
              BufferedReader reader = new BufferedReader(
                      new InputStreamReader(content));
              String line;
              while ((line = reader.readLine()) != null) {
                  builder.append(line);
              }
          } else {
              Log.e(JsonHandling.class.toString(), "Failed to download file");
          }
  
          resultObject = new JSONObject(builder.toString());
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return resultObject;
  }
  
  @Override
  protected void onPostExecute(JSONObject result){
      super.onPostExecute(result);
      listener.onGetData(result);
  }}
  </code></pre>
  
  <p>activity code:</p>
  
  <pre><code>    package com.cyberdog.what2watch;
  
  import java.util.List;
  
  import org.json.JSONObject;
  
  import com.cyberdog.what2watch.JsonHandling.IOnFinish;
  
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.inputmethod.InputMethodManager;
  import android.widget.AbsListView;
  import android.widget.AbsListView.OnScrollListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.Toast;
  
  public class SearchActivity extends Activity implements OnScrollListener, IOnFinish{
  
  
  
      /** Called when the activity is first created. */
  
      private static SearchCustomAdapter adapter;
      EditText tvTitel = null;
      EditText tvYear = null;
      ListView lvSearch = null;
      JsonHandling jh = new JsonHandling(this);
      int pageNumber =0;
      int previeusTotal=0;
      private static List&lt;Serie&gt; searchResult;
      private IOnFinish iof;
      private static List&lt;Serie&gt;series;
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.search);
  
          tvTitel = (EditText) findViewById(R.id.etf_search_titel);
          tvYear = (EditText) findViewById(R.id.etf_search_year);
          lvSearch = (ListView)findViewById(R.id.lvSearch);
  
  
  
          Button btnSearch = (Button) findViewById(R.id.btnSearch_search);
          btnSearch.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
                  InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  
                  inputManager.hideSoftInputFromWindow(getCurrentFocus()
                          .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  
                  JsonHandling.getInstance().getJSONFromUrl(tvTitel.getText().toString(), tvYear.getText().toString(),iof);
              }
          });
      }
  
      public void search(int pageNumber){
          // adapter for the custom list
          //lvSearch = (ListView) findViewById(R.id.lvSearch);
  
  //      SearchCustomAdapter lvAdapt = new SearchCustomAdapter(
  //              SearchActivity.this, jh.search_db(tvTitel.getText()
  //                      .toString(), tvYear.getText().toString(),
  //                      searchSite, pageNumber));
  //                      lvSearch.setAdapter(lvAdapt);
  
                          //lvAdapt.notifyDataSetChanged();
      }
  
      @Override
      public void onGetData(JSONObject obj) {
          if (obj == null) {
              Toast.makeText(this,
                      "obj is null",
                      Toast.LENGTH_LONG).show();
              Serie serie = new Serie("not found","");
              Serie[] seriesList = new Serie[series.size()];
              seriesList[0] = serie;
              adapter = new SearchCustomAdapter(this, seriesList);
              lvSearch = (ListView) findViewById(R.id.lvSearch);
              lvSearch.setAdapter(adapter);
          }
          if(obj !=null){
              if (JsonHandling.pageNumber == 0){
              series =Serie.serieListFromJSON(this, JsonHandling.getInstance().getJSONArraySerieFromUrlTMDB(obj));
              Serie[] seriesList =series.toArray(new Serie[series.size()]);
              adapter = new SearchCustomAdapter(this, seriesList);
              lvSearch = (ListView)findViewById(R.id.lvSearch);
              lvSearch.setAdapter(adapter);
              //lvSearch.setOnItemClickListener(this);
              lvSearch.setOnScrollListener(this);
  
  
              }}
  
          }
  
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem,
              int visibleItemCount, int totalItemCount) {
          int lastInScreen = firstVisibleItem+visibleItemCount;
          totalItemCount = 20+20 * pageNumber;
          //int totalSearchResults = 0;
  
          Log.i("total, last in screen, pagenumber outside", totalItemCount+":" + lastInScreen+ ":"+pageNumber);
          if((lastInScreen +20*pageNumber == totalItemCount)){
              Log.i("total, last in screen, pagenumber inside", totalItemCount+":" + lastInScreen+ ":"+pageNumber);
  
  
  
              pageNumber++;
  
              search(pageNumber);
  
          }
          else{
  
          }
  
      }
  
      @Override
      public void onScrollStateChanged(AbsListView arg0, int arg1) {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>
  
  <p>and the json/interface part code:</p>
  
  <pre><code>public interface IOnFinish{
      void onGetData(JSONObject obj);
  }
  
  public void getJSONFromUrl(String titel, String year, IOnFinish listener) {
      JsonHandlingTMDBAsync task = new JsonHandlingTMDBAsync(titel, year, listener);
      task.execute();
  }
  </code></pre>
  
  <p>In my activity class i have a break point on the <code>onGetData()</code> but it is never reached, though the <code>onPostExecute()</code> has the object and is called twice(don't know why).</p>