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

  • SEARCH
  • Home
  • 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 8062551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:42:29+00:00 2026-06-05T10:42:29+00:00

I have created an AsyncTask that loads data from a website then when I

  • 0

I have created an AsyncTask that loads data from a website then when I execute it. At the moment I’m getting a null pointer exception which I think is caused because I’m trying to fill the UI too early so I’m hoping to create some kind of if statement that says if AsyncTask isn’t complete show a loading graphic and when it is fill the data. But I’m not sure how to go about doing this does anyone know how I could achieve this or at least point me in the right direction.

Here’s my code so far

public void checkPreferences(){

    SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    ChosenMethod = preferences.getString("ChosenMethod", ChosenMethod);
    ChosenLeagueID = preferences.getString("ChosenLeagueId", ChosenLeagueID);
    ChosenTeamId = preferences.getString("ChosenTeamId", ChosenTeamId);

    Log.v("lc", "newsurl" + newsFeedURL);


    Log.v("myapp", "ChosenMethod Home = " + ChosenMethod);
    Log.v("myapp", "ChosenLeagueID Home = " + ChosenLeagueID);
    Log.v("myapp", "ChosenTeamID Home = " + ChosenTeamId);

    if (ChosenMethod.equals("Team")) {
        setContentView(R.layout.homeactteam2);
        newsAmount = 5;

    } else {
        newsAmount = 10;
        setContentView(R.layout.homeactteam);
    }

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    //Check Preferences which sets UI

    checkPreferences();
   PostTask posttask;
   posttask = new PostTask();
   posttask.execute();

   FillData();


    Button backbtn = (Button) findViewById(R.id.backbtn);

    //Listening to button event
    backbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            Intent previousScreen = new Intent(getApplicationContext(), ChooseTeamActivity.class);
            ChosenMethod = "null";
            SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("ChosenMethod", ChosenMethod);            
            editor.commit();
            previousScreen.putExtra("FullData", fulldata);
            startActivity(previousScreen);


        }
    });

//((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
//   
//    public void onRefresh() {
//        // Do work to refresh the list here.
//        loadData();
//    }
//});
//


}



public void loadNewsFeed(){

     newsFeedRequest = "website/" + chosenLeagueId + "/news?timestamp=" + unixTimeStamp;
     newsFeedURL = "https://www.website.com" + newsFeedRequest; 

        String myhash = buildHmacSignature(apiKey, newsFeedURL);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(newsFeedURL);

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("requestToken", myhash));
        pairs.add(new BasicNameValuePair("apiUser", apiUser));

            try {
                post.setEntity (new UrlEncodedFormEntity(pairs));
                HttpResponse response = client.execute(post);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                String json = reader.readLine();
                fulldata = String.valueOf(json);
                Log.v("myApp","newsdata" + fulldata);

                newsList = new ArrayList<String>();
                newsList2 = new ArrayList<String>();
                newsList3 = new ArrayList<String>();
                imageList = new ArrayList<String>();


                JSONObject obj = new JSONObject(json);    
                JSONObject objData = obj.getJSONObject("data");
                JSONArray jArray = objData.getJSONArray("news");
                Log.v("lc","newsAmount= " + newsAmount);

                   for(int t = 0; t < newsAmount; t++){
                       JSONObject newsTitleDict = jArray.getJSONObject(t);
                       imageList.add(newsTitleDict.getString("image_small"));
                     newsList3.add(newsTitleDict.getString("title"));

                   }

                   for(int t = 0; t < 1; t++){
                       JSONObject newsTitleDict = jArray.getJSONObject(t);

                 newsList.add(newsTitleDict.getString("title"));
            //       newsList2.add(newsTitleDict.getString("title"));

                   }



    } catch (ClientProtocolException 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();
    }


}           


public void loadResultsFeed(){


    resultsFeedRequest = "website/" + chosenLeagueId + "/results?&team_id=" + ChosenTeamId + "&limit=31&timestamp=" + unixTimeStamp;
    resultsFeedURL = "https://www.website.com" + resultsFeedRequest;    

    String myhash = buildHmacSignature(apiKey, resultsFeedURL);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(resultsFeedURL);

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("requestToken", myhash));
    pairs.add(new BasicNameValuePair("apiUser", apiUser));

    try {
        post.setEntity (new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        fulldata = String.valueOf(json);
        Log.v("myApp","resultsdata" + fulldata);

        newsList = new ArrayList<String>();
        newsList2 = new ArrayList<String>();
        newsList3 = new ArrayList<String>();
        imageList = new ArrayList<String>();


        JSONObject obj = new JSONObject(json);    
        JSONObject objData = obj.getJSONObject("data");
        JSONArray jArray = objData.getJSONArray("results");


        if(jArray.length() < 1) loadLastResults();



    } catch (ClientProtocolException 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();
    }   


}


public void loadLastResults(){

    resultsFeedRequest = "website/" + chosenLeagueId + "/results?month=04&team_id=" + ChosenTeamId + "&limit=31&timestamp=" + unixTimeStamp;
    resultsFeedURL = "https://www.website.com" + resultsFeedRequest;    

     String myhash = buildHmacSignature(apiKey, resultsFeedURL);

    Date anotherCurDate = new Date();  
    SimpleDateFormat formatter = new SimpleDateFormat("MM");  
    String CurMonth = formatter.format(anotherCurDate);  

    int Int = Integer.parseInt(CurMonth);

    int MonthInt = Int -1;


    CurMonth = (String) (String.valueOf(MonthInt));



    if (CurMonth.equals("1")){

        lastMonth = "12";

    }    
     else {
        if(CurMonth.length() < 2){
            lastMonth = "0" + CurMonth;
        } else {
            lastMonth = CurMonth;
        }
    }

    Log.v("lc","month= " + CurMonth);
    Log.v("lc","LastMonth= " + lastMonth);


        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(resultsFeedURL);

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("requestToken", myhash));
        pairs.add(new BasicNameValuePair("apiUser", apiUser));

        try {
            post.setEntity (new UrlEncodedFormEntity(pairs));
            HttpResponse response = client.execute(post);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
            fulldata = String.valueOf(json);
            Log.v("myApp","resultsdata" + fulldata);

            newsList = new ArrayList<String>();
            newsList2 = new ArrayList<String>();
            newsList3 = new ArrayList<String>();
            imageList = new ArrayList<String>();


            JSONObject obj = new JSONObject(json);    
            JSONObject objData = obj.getJSONObject("data");
            JSONArray jArray = objData.getJSONArray("results");


               for(int t = 0; t < 1; t++){

                   resultsDict = jArray.getJSONObject(t);
                  HomeTeam = resultsDict.getString("hometeam");
                  AwayTeam = resultsDict.getString("awayteam");
                  HomeScore = resultsDict.getString("homescore");
                  AwayScore = resultsDict.getString("awayscore");
                  Attendance = resultsDict.getString("attendance");
                  Division = resultsDict.getString("division");

                  Log.v("lc","hometeam" + HomeTeam);
                  Log.v("lc","awayteam" + AwayTeam);


               }


        } catch (ClientProtocolException 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();
        }


    }   


public class PostTask extends AsyncTask<Void, String, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        boolean result = false;
        loadNewsFeed();
        loadResultsFeed();
        loadLastResults();
        publishProgress("progress");
        return result;
    }

    protected void onProgressUpdate(String... progress) {
        StringBuilder str = new StringBuilder();
            for (int i = 1; i < progress.length; i++) {
                str.append(progress[i] + " ");
            }
    }

}

public void FillData(){ 


     if (ChosenMethod.equals("Team")) {


         resultsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.resultscell,
                 null);

       TextView homeTeam = (TextView) resultsView.findViewById(R.id.HomeTeam);
       homeTeam.setText(HomeTeam);

       TextView awayTeam = (TextView) resultsView.findViewById(R.id.AwayTeam);
       awayTeam.setText(AwayTeam);

       TextView homeScore = (TextView) resultsView.findViewById(R.id.HomeScore);
       homeScore.setText(HomeScore);

       TextView awayScore = (TextView) resultsView.findViewById(R.id.AwayScore);
       awayScore.setText(AwayScore);

       TextView attendance = (TextView) resultsView.findViewById(R.id.Attendence);
       attendance.setText("Att:" + Attendance);

       TextView division = (TextView) resultsView.findViewById(R.id.Division);
       division.setText(Division);

        arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);

         String[] mStrings = (String[]) imageList.toArray(new String[imageList.size()]);
         String[] news = (String[]) newsList3.toArray(new String[newsList3.size()]);

         arrayAdapter3 = new LazyAdapter(this, mStrings, news);


            ListView list = getListView();
               list.setTextFilterEnabled(true);

               LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
                View header = inflater.inflate( R.layout.homeheader, list, false);
                View header2 = inflater.inflate( R.layout.homeheader2, list, false);
                View header3 = inflater.inflate( R.layout.homeheader3, list, false);


        //setListAdapter (arrayAdapter);


            adapter = new MergeAdapter();
            adapter.addView(header);
            adapter.addAdapter(arrayAdapter);
            adapter.addView(header2);
            adapter.addView(resultsView);
            adapter.addView(header3);
            adapter.addAdapter(arrayAdapter3);
            setListAdapter(adapter);



     } else {

         arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
            arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
            //arrayAdapter3 = new ArrayAdapter(this, R.layout.complex_item, newsList3);

             String[] mStrings = (String[]) imageList.toArray(new String[imageList.size()]);
             String[] news = (String[]) newsList3.toArray(new String[newsList3.size()]);


             arrayAdapter3 = new LazyAdapter(this, mStrings, news);


                ListView list = getListView();
                   list.setTextFilterEnabled(true);

                   LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
                    View header3 = inflater.inflate( R.layout.homeheader3, list, false);


            //setListAdapter (arrayAdapter);


                adapter = new MergeAdapter();
                adapter.addView(header3);
                adapter.addAdapter(arrayAdapter3);
                setListAdapter(adapter);
         } 
}
  • 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-05T10:42:31+00:00Added an answer on June 5, 2026 at 10:42 am

    Blindly Assumption…! (Without StackTrace report)

    Implement onPostExecute() in your AsyncTask and put FillData() method in it.. When your doInBackground().. completes control comes in onPostExecute() automatically.. Then fill your UI part..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that downloads videos from a URL. I am using AsyncTask.
I have an Activity that retrieves data from a web service. This data is
I have some AsyncTask that loads a video stream into the player, so I
Some specific situation: I have two Activities . FirstActivity loads data from network(~100 kb)
I have created custom listview in that have list of textview & list of
I have created a custom JTree. That tree could be filtered to show only
I have created an app in android and it requires some data to be
I have an app that connects to a remote chat server and then the
I have created a new class called Get_WebPage1 and init I have extends that
I have a ProgressDialog running in a AsyncTask. I`m trying to achive that as

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.