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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:06:40+00:00 2026-05-25T17:06:40+00:00

Attempting to write my first ArrayAdapter for Android, failing miserably at the moment. It

  • 0

Attempting to write my first ArrayAdapter for Android, failing miserably at the moment. It crashes on the setAdapter(adapter); line and throws a NullPointerException.

ContractTestActivity:

public class ContractTestActivity extends Activity {

    private ArrayList<Contract> contracts;
    public final String TAG = "ContractTest";
    //public Contract newContract = new Contract();

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

        setContentView(R.layout.main);
        ListView list;
        list = (ListView)findViewById(R.id.list);
        ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, contracts);
        list.setAdapter(adapter);
    }
}

ContractAdapter:

public class ContractAdapter extends ArrayAdapter<Contract> {


        private ArrayList<Contract> contracts;

        public ContractAdapter(Context context, int view, ArrayList<Contract> passedContracts) {
                super(context, view, passedContracts);
                contracts = passedContracts;
        }

        @Override
        public int getCount() {
            return contracts.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View currentView = convertView;
                LayoutInflater currentViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                currentView = currentViewInflater.inflate(android.R.layout.simple_list_item_1, null);
                Contract currentContract = contracts.get(position);
                TextView text = (TextView) currentView.findViewById(android.R.id.text1);
                text.setText(currentContract.getName());
                return currentView;
        }

}

Contract:

public class Contract extends ContractTestActivity {

    private String name;
    private float payRate;
    private int hoursWorked;
    private int holidays;

    public Contract() {

    }

    public String getName() {
        return name;
    }

}

Suggestions?

  • 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-05-25T17:06:41+00:00Added an answer on May 25, 2026 at 5:06 pm

    The problem is that you are not initializing your ArrayList

    private ArrayList<Contract> contracts;
    

    Its therefore you are getting the error NullPointerException.

    UPADATED:

    ContractTestActivity.java

    public class ContractTestActivity extends Activity {
        private List<Contract> contracts = new ArrayList<Contract>();
        public final String TAG = "ContractTest";
        //public Contract newContract = new Contract();
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
            ListView list;
            list = (ListView)findViewById(R.id.mylist);
            ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, myContracts());
            list.setAdapter(adapter);
        }
    
        private List<Contract> myContracts(){
    
            List<Contract> list = new ArrayList<Contract>();
    
            list.add(new Contract("Friend1"));
            list.add(new Contract("Friend2"));
            list.add(new Contract("Friend3"));
            list.add(new Contract("Friend4"));
            return list;
        }
    }
    

    Contract.java

    public class Contract {
    
        private String name;
    
        public Contract(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }
    

    ContractAdapter.java

    public class ContractAdapter extends ArrayAdapter<Contract> {
    
    
        private List<Contract> contracts;
    
        public ContractAdapter(Context context, int view, List<Contract> passedContracts) {
                super(context, view, passedContracts);
                contracts = passedContracts;
        }
    
        @Override
        public int getCount() {
            return contracts.size();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View currentView = convertView;
                LayoutInflater currentViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                currentView = currentViewInflater.inflate(android.R.layout.simple_list_item_1, null);
                Contract currentContract = contracts.get(position);
                TextView text = (TextView) currentView.findViewById(android.R.id.text1);
                text.setText(currentContract.getName());
                return currentView;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to write my first android app, HelloAndroid. The error in the title
I am attempting to write a one-line Perl script that will toggle a line
I'm attempting to write my first jQuery plugin to query multi-dimensional arrays of complex
I'm attempting to write my first custom Cocoa Framework and I want to expose
I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first
I am attempting to write some JUnit tests for my android application. The application
I'm attempting to write my first unity script. This is the code for a
I'm attempting to write a widget for DotNetNuke in C#. This is my first
I am attempting to write my first implementation of databinding in a WPF form.
I've been attempting to write a Lisp macro that would perfom the equivalent of

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.