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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:21:22+00:00 2026-06-02T12:21:22+00:00

I am using an ArrayAdapter to display data held in an array via a

  • 0

I am using an ArrayAdapter to display data held in an array via a ListView, my code compiles fine but when I call the class below is causes the process to end with the Sorry application has stopped exception.

   public class Timetable extends Activity {

    private ListView timeTableView;
    private ArrayList <Module> Modules;
    private ArrayAdapter <Module> adaptModule;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.view);

            timeTableView = (ListView) findViewById(R.id.timeTableView);

            adaptModule = new ArrayAdapter<Module>(this, android.R.layout.simple_list_item_1, Modules);
            timeTableView.setAdapter(adaptModule);
     }
    }

Here is my Add class which deals with creating an array and adding items to the array:

public class Add extends Activity implements OnClickListener{

private EditText ModuleText;
private EditText RoomText;
private Spinner Day;
private Spinner Start;
private Spinner Duration;
private Spinner Session;
private ArrayList<Module> Modules;

/*
 * OnCreate method for add class
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    // spinner for days
    Spinner day = (Spinner) findViewById(R.id.spinnerDay);
    ArrayAdapter<CharSequence> adapterDay = ArrayAdapter.createFromResource(
            this, R.array.days_array, android.R.layout.simple_spinner_item);
    adapterDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    day.setAdapter(adapterDay);


    // spinner for start time
    Spinner startTime = (Spinner) findViewById(R.id.spinnerStart);
    ArrayAdapter<CharSequence> adapterstart = ArrayAdapter.createFromResource(
            this, R.array.start_array, android.R.layout.simple_spinner_item);
    adapterstart.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    startTime.setAdapter(adapterstart);

    // spinner for duration
    Spinner duration = (Spinner) findViewById(R.id.spinnerDuration);
    ArrayAdapter<CharSequence> adapterduration = ArrayAdapter.createFromResource(
            this, R.array.duration_array, android.R.layout.simple_spinner_item);
    adapterduration.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    duration.setAdapter(adapterduration);

    // spinner for class type
    Spinner session = (Spinner) findViewById(R.id.spinnerSession);
    ArrayAdapter<CharSequence> adapterSession = ArrayAdapter.createFromResource(
            this, R.array.session_array, android.R.layout.simple_spinner_item);
    adapterSession.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    session.setAdapter(adapterSession);

    //create array
    Modules = new ArrayList<Module>();

    // ref to UI Widgets
    ModuleText = (EditText) findViewById(R.id.ModuleText);
    RoomText = (EditText) findViewById(R.id.RoomText);
    Day = (Spinner) findViewById(R.id.spinnerDay);
    Start = (Spinner) findViewById(R.id.spinnerStart);
    Duration = (Spinner) findViewById(R.id.spinnerDuration);
    Session = (Spinner) findViewById(R.id.spinnerSession);


    // buttons for this activity also includes listeners
    View addEntryButton = findViewById(R.id.addEntry_button);
    addEntryButton.setOnClickListener(this);
    View cancelEntryButton = findViewById(R.id.cancelEntry_button);
    cancelEntryButton.setOnClickListener(this);


}

/*
 * on click listeners for buttons
 * @see android.view.View.OnClickListener#onClick(android.view.View)
 */
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.cancelEntry_button:
        Intent i = new Intent(this, Home.class);
        startActivity(i);
        break;

    }
     switch (v.getId()) {
        case R.id.addEntry_button:
            Intent i = new Intent(this, Home.class);
            addModule();
            startActivity(i);
            break;
    }

    }

    /* 
     * Method for adding an entry into the table
     */
public void addModule() 
   {
    if(ModuleText !=null || RoomText !=null)
    {
    Modules.add(new Module(ModuleText.getText().toString(), RoomText.getText().toString(), Day.getSelectedItem().toString(), Start.getSelectedItem().toString(), Duration.getSelectedItem().toString(), Session.getSelectedItem().toString()));
    Toast.makeText(getApplicationContext(), "Timetable entry has been added succesfully", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Error adding timetable entry, please check details and try again", Toast.LENGTH_SHORT).show();
    }

   }


} 

Here is my logcat with the exception:

04-22 11:52:46.290: E/AndroidRuntime(426): FATAL EXCEPTION: main
04-22 11:52:46.290: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start    activity ComponentInfo{org.assignment.timetable/org.assignment.timetable.Timetable}:   java.lang.NullPointerException
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-22 11:52:46.290: E/AndroidRuntime(426):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.os.Looper.loop(Looper.java:123)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 11:52:46.290: E/AndroidRuntime(426):  at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:52:46.290: E/AndroidRuntime(426):  at java.lang.reflect.Method.invoke(Method.java:521)
04-22 11:52:46.290: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 11:52:46.290: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 11:52:46.290: E/AndroidRuntime(426):  at dalvik.system.NativeStart.main(Native Method)
04-22 11:52:46.290: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.widget.ListView.setAdapter(ListView.java:436)
04-22 11:52:46.290: E/AndroidRuntime(426):  at org.assignment.timetable.Timetable.onCreate(Timetable.java:25)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 11:52:46.290: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  • 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-02T12:21:25+00:00Added an answer on June 2, 2026 at 12:21 pm

    You never initialize the Modules field in your Timetable class and when you use it in the adaptModuleadapter it will throw the NullPointerException that you see. If you build the Modules list in another activity and then you try display it in the Timetable activity then you’ll have to pass the Modules list from that activity(probably the the Add activity) and use that list in the adapter.

    There are various ways on how to pass that ArrayList from one activity to another activity, you can see more examples here on Stackoverflow, for example:

    Android: how to pass ArrayList<object> from an activity to another

    Small example added:

    The class that extends Application:

    /**
     * The class that extends Application, and where we will store our ArrayList
     */
    public class MyApplication extends Application {
    
        /**
         * This will hold your Modules list so we can access it from evry activity
         */
        private ArrayList<Module> mModules;
    
        /**
         * We will call this method when we want to store the list of modules(for
         * example when we finish adding elements to it(in the Add activity) and we
         * are switching to the Timetable activity)
         */
        public void setModulesList(ArrayList<Module> data) {
            this.mModules = data;
        }
    
        /**
         * Retrieve the list of modules and work with it(for example to display it
         * in the Timetable activity)
         */
        public ArrayList<Module> getModulesList() {
            return mModules;
        }
    }
    

    An activity where we add elements to the list of Modules:

    public class AddActivity extends Activity {
    
        // local list of modules on which, in this activity, we will add elements
        private ArrayList<Module> items = new ArrayList<Module>();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // simple layout with one Button that will get us to the Timetable
            // activity
            Button b = new Button(this);
            b.setText("I've put something in the Modules arrayList so go to the new activity");
            b.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // we are switching to the other activity so store the list of
                    // modules in MyApplication
                    ((MyApplication) getApplicationContext()).setModulesList(items);
                    Intent i = new Intent(AddActivity.this, Timetable.class);
                    startActivity(i);
                }
            });
            setContentView(b);
            // some dummy work, adding elements to the list of Modules.
            for (int i = 0; i < 12; i++) {
                items.add(new Module("Module " + i));
            }
    
        }
    }
    

    The activity where we will show the list of Modules previously created in the AddActivity :

    public class Timetable extends ListActivity {
    
        // private field, that will hold a copy of the list from MyApplication
        private ArrayList<Module> mModules;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // retrieve the list of Modules from MyApplication
            mModules = ((MyApplication) getApplicationContext()).getModulesList();
            // it is possible to not have elements in the list at this time so just
            // create an
            // empty list so we don't throw exceptions
            if (mModules == null) {
                mModules = new ArrayList<Module>();
            }
            // set the adapter
            setListAdapter(new ArrayAdapter<Module>(this,
                    android.R.layout.simple_list_item_1, mModules));
        }
    }
    

    In order for our MyApplication to work we’ll have to register it in the manifest(see the application element, and the added android:name tag):

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.luksprog.passit"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="8" />
    
        <application
            android:name=".MyApplication"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".AddActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".Timetable"
                android:label="@string/app_name" >
            </activity>
        </application>
    
    </manifest>
    

    And finally the Module class:

    /**
     * A small Module class. 
     *
     */
    public class Module {
    
        String name;
    
        public Module(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            //This will show a nice string in the `ListView` 
            return "This is:" + name;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call another intent from ArrayAdapter class using startActivityForResult but i cann't
I'm using a ListView to display an array of names. Code for it looks
I am trying to display the list of songs using array adapters. But the
I am adding item in ListView using ArrayAdapter. First display 15 item in the
i am using a viewholder to display from a dynamic arrayadapter.it works but the
I am using list view using ArrayAdapter to display number of items while user
Im trying to display this listview, but I keep getting a: 07-17 21:14:22.233: ERROR/AndroidRuntime(349):
I'm using an ArrayAdapter with my ListView: What I'm trying to achive: I'm displaying
Hi I am developing one app using the below link Custom ListView With Headers
I'm using ListActivity with my own ArrayAdapter class. When I override the methods ArrayAdapter.areAllItemsEnabled()

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.