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 7724277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:38:54+00:00 2026-06-01T04:38:54+00:00

I am trying to set my AsyncTask returning a custom class type such as

  • 0

I am trying to set my AsyncTask returning a custom class type such as

AsyncTask<Void, Void, MyClass>

For this I have created a class User.
File: User.java

package com.asynctask.namespace;
import java.util.ArrayList;

public class User
{   
    private ArrayList<String> name;

    public void addName(String value)
    {       
        name.add(value);
    }

    public String getName(int pos)
    {       
        return name.get(pos).toString();
    }
}

In my main activity I have:

package com.asynctask.namespace;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class AsyncTaskExampleActivity extends Activity {
    private TextView result;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        result = new TextView(this);
        setContentView(result);

        new loadData().execute();        
    }
    // --- Subclass --- //
    private class loadData extends AsyncTask<Void, Void, User> 
    {    
        ProgressDialog progDialog;
        User client = new User();

        protected void onPreExecute() 
        {
            progDialog = new ProgressDialog(AsyncTaskExampleActivity.this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDialog.setMessage("Conectando a base de dados...");
            progDialog.show();
        }

        protected User doInBackground(Void... args) 
        {
            client.addName("testName");         
            return client;
        }

        protected void onPostExecute(User values) 
        {                   
            progDialog.dismiss();
            result.setText(values.getName(0));

        }

    }

}

I get the following Logcat errors:

04-02 13:45:05.683: W/dalvikvm(1056): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
04-02 13:45:05.683: E/AndroidRuntime(1056): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
04-02 13:45:05.783: E/AndroidRuntime(1056): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 13:45:05.783: E/AndroidRuntime(1056):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.lang.Thread.run(Thread.java:1096)
04-02 13:45:05.783: E/AndroidRuntime(1056): Caused by: java.lang.NullPointerException
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.User.addName(User.java:11)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.AsyncTaskExampleActivity$loadData.doInBackground(AsyncTaskExampleActivity.java:39)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at com.asynctask.namespace.AsyncTaskExampleActivity$loadData.doInBackground(AsyncTaskExampleActivity.java:1)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 13:45:05.783: E/AndroidRuntime(1056):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-02 13:45:05.783: E/AndroidRuntime(1056):     ... 4 more
04-02 13:45:05.903: I/dalvikvm(1056): threadid=7: reacting to signal 3
04-02 13:45:06.093: I/dalvikvm(1056): Wrote stack trace to '/data/anr/traces.txt'

Any ideas why?

Your help is much appreciated!
Happy coding!

  • 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-01T04:38:55+00:00Added an answer on June 1, 2026 at 4:38 am

    You have to initialize the array list name before you use it. Try this:

    package com.asynctask.namespace;
    import java.util.ArrayList;
    
    public class User
    {   
        // initialize before use
        private ArrayList<String> name = new ArrayList<String>();
    
        public void addName(String value)
        {       
            name.add(value);
        }
    
        public String getName(int pos)
        {       
            return name.get(pos).toString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to set up a config file for a custom module - do I
Im trying set the single table inheritance model type in a form. So i
Trying to set up a gridview with a custom adapter, I get my cursor
Trying to set 301 redirect in .htaccess file and here is what i am
Trying to set up the svn commit with trac using this script. It is
Im trying to set my tabs as a scrollbar. I have 6 tabs but
Trying to set up a new spring project and im having this issue where
Trying to set up a menu so that I have border-left on each menu
So I have this AsyncTask that loads a WebView in onPostExecute that otherwise works
Okay, this has me confused once again. I am trying to either A: set

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.