Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8699627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:59:37+00:00 2026-06-13T01:59:37+00:00

I am parsing JSON from a URL & displaying it in a list view(ofc

  • 0

I am parsing JSON from a URL & 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.

Here is my code till now :

public class VideoList extends ListActivity {

ArrayAdapter<String> adapter;
ListView lv;
PrefMethods pm= new PrefMethods();;
int vid_id;
public void getJson(){
      new LoadJsonTask().execute();
 }

 private class LoadJsonTask extends AsyncTask<Void, Void, ArrayList<String>>{
      ProgressDialog dialog ;
     protected void onPreExecute (){
          dialog = ProgressDialog.show(VideoList.this ,"Loading....","Retrieving Data,Please Wait !");

     }
      protected ArrayList<String> doInBackground(Void[] params) {

          return populate();
      }


      protected void onPostExecute(ArrayList<String> 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<String> populate() {
    ArrayList<String> items = new ArrayList<String>();

    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 & error is occured
            int k=ja.length();
            for (int i = 0; i < 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")>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;
}




}

here is the code for my Preferences class:

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;
  }

}

My Logcat error:

http://pastebin.com/JNy5RmP8

The error is a Null Pointer Exception at the following line in PrefMethods Class:

int v=prefs.getInt("id",0);

Can anyone help me & point me out where i am going wrong?

Thank You

Edit: I have added Internet permissions in Manifest & the JSON is getting parsed perfectly fine without the preferences methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:59:38+00:00Added an answer on June 13, 2026 at 1:59 am

    prefs is not being set to anything as onCreate is never called.

    Activity subclasses are for Activities not Preferences.

    Create a PrefMethods class which does not extend Activity, but takes a Context in the constructor.

    The PrefMethods class should then use that context to access the Preferences.

    Something like this

          public class PrefMethods {
    SharedPreferences prefs;
    
    private Context mContext;
    
    private static final String PREF_NAME = "MyPrefs";
    
        public PrefMethods( Context c ) 
        {
            prefs= c.getSharedPreferences( PREF_NAME, Context.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;
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of places in listview which i got from parsing JSON.
I'm having some trouble parsing a JSON that I obtain from my DB to
I'm attempting to retrieve a JSON file from an API for parsing. I'm using
I am parsing json from the following url http://www.kaverisoft.com/careers/assignments/android/a1.php with the following code public
I found this article about parsing JSON response from a URL request i iOS:
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I'm work with GWT and parsing a JSON result from an ASP.NET webservice method
Am am successfully parsing and sending JSON values from my client for my server
From a previous question on Stackoverflow Iterating through/Parsing JSON Object via JavaScript .... My
I am having trouble parsing my JSON which i get from javascript. The format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.