title: How to use preferences in Async Task?
body: <p>I am parsing JSON from a URL &amp; displaying it in a list view(ofc using a Async Task) , that works fine but now when i try to add a preference in the Async Task , the program gives a error.</p>
  
  <p>Here is my code till now :</p>
  
  <pre><code>public class VideoList extends ListActivity {
  
  ArrayAdapter&lt;String&gt; adapter;
  ListView lv;
  PrefMethods pm= new PrefMethods();;
  int vid_id;
  public void getJson(){
        new LoadJsonTask().execute();
   }
  
   private class LoadJsonTask extends AsyncTask&lt;Void, Void, ArrayList&lt;String&gt;&gt;{
        ProgressDialog dialog ;
       protected void onPreExecute (){
            dialog = ProgressDialog.show(VideoList.this ,"Loading....","Retrieving Data,Please Wait !");
  
       }
        protected ArrayList&lt;String&gt; doInBackground(Void[] params) {
  
            return populate();
        }
  
  
        protected void onPostExecute(ArrayList&lt;String&gt; waveData) {
  
            adapter=new ArrayAdapter(
                    VideoList.this,android.R.layout.simple_list_item_1,
                      waveData);  
                    lv.setAdapter(adapter);  
                    lv.setTextFilterEnabled(true);
                    dialog.dismiss();
        };
  
   }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.parser);
      lv=getListView();
  
      //vid_id=pm.loadprefs();
      vid_id=0;
      getJson();
  }
  
  public ArrayList&lt;String&gt; populate() {
      ArrayList&lt;String&gt; items = new ArrayList&lt;String&gt;();
  
      try {
  
          URL urlnew= new URL("www.mydummyjsonlinkhere.com");
          HttpURLConnection urlConnection =
              (HttpURLConnection) urlnew.openConnection();
          urlConnection.setRequestMethod("GET");
          urlConnection.connect();
                      // gets the server json data
          BufferedReader bufferedReader =
              new BufferedReader(new InputStreamReader(
                      urlConnection.getInputStream()));
          String next;
          while ((next = bufferedReader.readLine()) != null){
              JSONArray ja = new JSONArray(next);
              vid_id=pm.loadprefs();// here i load the preferences &amp; error is occured
              int k=ja.length();
              for (int i = 0; i &lt; ja.length(); i++) {
  
                  JSONObject jo = (JSONObject) ja.get(i);
                  WaveData waveData = new WaveData(jo.getString("upload"), jo.getInt("id"),jo.getString("link"),jo.getString("custommsg"));
                  if(jo.getInt("id")&gt;vid_id){
                      if(i==k-1){
                          pm.saveprefs(jo.getInt("id"));  
                          Toast.makeText(getApplicationContext(), "Saved pref no :"+jo.getInt("id"), 500).show();
                      }else{}
                  }else{}
  
                  if(jo.has("id")){
                  items.add(jo.getString("custommsg"));
                  }
  
              }
  
  
  
          }
  
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return items;
  }
  
  
  
  
  }
  </code></pre>
  
  <p>here is the code for my Preferences class:</p>
  
  <pre><code>public class PrefMethods extends Activity {
  SharedPreferences prefs;
   @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          prefs=getPreferences(MODE_PRIVATE);
  
   }  
  
  
   public void saveprefs(int h){
     SharedPreferences.Editor editor=prefs.edit();
     editor.putInt("id", h);
     editor.commit();
    }
   public void saveprefs(boolean b){
         SharedPreferences.Editor editor=prefs.edit();
         editor.putBoolean("inserted", b);
         editor.commit();
    }
  
    public int loadprefs(){
  int v=prefs.getInt("id",0);
  return v;
    }
  
    public boolean loadprefsboolean(){
      boolean v=prefs.getBoolean("inserted", false);
      return v;
    }
  
  }
  </code></pre>
  
  <p>My Logcat error:</p>
  
  <p><a href="http://pastebin.com/JNy5RmP8" rel="nofollow">http://pastebin.com/JNy5RmP8</a></p>
  
  <p>The error is a Null Pointer Exception at the following line in PrefMethods Class:</p>
  
  <pre><code>int v=prefs.getInt("id",0);
  </code></pre>
  
  <p>Can anyone help me &amp; point me out where i am going wrong?</p>
  
  <p>Thank You</p>
  
  <p>Edit: I have added Internet permissions in Manifest &amp; the JSON is getting parsed perfectly fine without the preferences methods.</p>