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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:34:08+00:00 2026-06-17T05:34:08+00:00

Good day. I’m having some issues with my android project specifically listview. I tried

  • 0

Good day. I’m having some issues with my android project specifically listview. I tried searching for other information here in this site, and implemented some of the answers. However, it is still not working.

The error specifically is

NullPointerException at line 76 at MainActivity

Here is the code of my MainActivity

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

     final ArrayList<String> studentName = new ArrayList<String>();

     ArrayAdapter<String> aa;

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



        ListView myList = (ListView) findViewById(R.id.listName);

         aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, studentName);

         myList.setAdapter(aa);


        //droid.R.id.list;

        //add
        Button bAdd = (Button) findViewById(R.id.addstudent);
        bAdd.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                startActivity(new Intent("android.intent.action.ADDSTUDENTS"));
            }   
        });


        //edit

        Button bEdit = (Button) findViewById(R.id.editstudent);
        bEdit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View x) {
                startActivity(new Intent("android.intent.action.EDITSTUDENTS"));
            }
        });

        //edit

        Button bDelete = (Button) findViewById(R.id.deletestudent);
        bDelete.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View x) {
                startActivity(new Intent("android.intent.action.DELETESTUDENTS"));
                }
            });


    }

    public ArrayList<String> getArray(){
        return studentName;
    }

    public void notifyArray(){
        aa.notifyDataSetChanged();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

and line 76 by the way is

aa.notifyDataSetChanged();

Here is my code for the AddStudents class

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddStudents extends Activity{

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

        setContentView(R.layout.add_student);

        Button bAddStudents = (Button) findViewById(R.id.add);
        final EditText et = (EditText) findViewById(R.id.student_name);

        bAddStudents.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                MainActivity as = new MainActivity();

                as.getArray().add(et.getText().toString());
                as.notifyArray();
                finish();

            }   


        });

        Button bBack = (Button) findViewById(R.id.backadd);

        bBack.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                finish();

            }   
    });


    }

}

and the xml part with the list view is

 <ListView
            android:id="@+id/listName" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

        </ListView>

I hope you can help me cause I want to also learn what my mistakes are. I can add other information if you want.

  • 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-17T05:34:09+00:00Added an answer on June 17, 2026 at 5:34 am

    In your AddStudents class, you’re calling notifyArray() right after you instantiated MainActivity. MainActivity.onCreate() will not be called just by instantiating it.

    Instantiating your MainActivity there is probably not what you want anyway (because that object will be disposed directly after the onClick handler is done).

    What you want instead is to access the existing instance of MainActivity. For that, add a reference to the current instance to a static member of your MainActivity class, e.g.

    public class MainActivity extends Activity {
        public static MainActivity activity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            activity = this;
        }
    }
    

    Then in your AddStudent class access it via

    MainActivity.activity.notifyArray()
    

    This is not the most beautiful way to solve your issue, but it works as long as you can be sure to only have one MainActivity instance. (If not, you could make the array itself static; or create a Singleton wrapper class for it.)

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

Sidebar

Related Questions

Good day all, I am having some trouble with image permissions. I am loading
Good Day! I tried to recreate the sample TTFacebook Project of the three20 samples
Good day. Having read about Model 2 architecture I got confused about some points.
Good day, Ive been having a hard time dealing with android notifications lately so
Good day, I'm having a little trouble with calculating number of days from now
Good day, I was playing with the example found here on SO: PHP Socket
Good Day, I'm new to this Youtube GData api and i'm having trouble with
Good day, I've read a few other stack overflow postings and other tutorials, but
Good day everyone! So currently I'm working on a project with video processing, so
Good day, Hope title is not confusing and i can be clear here. I

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.