ListView: null pointer exception

Good afternoon. I have some problems with my Android project specifically listview. I tried to find other information here on this site and performed some of the answers. However, it still does not work.

In particular, the error

NullPointerException on line 76 in 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

 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 of the list 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 because I also want to know what my mistakes are. I can add other information if you want.

+4
source share
3 answers

In your AddStudents class AddStudents you call notifyArray() right after you create an instance of MainActivity . MainActivity.onCreate() will not be called simply by instantiating it.

When creating an instance of MainActivity , you probably still don't want to (because this object will be deleted immediately after the onClick handler is onClick ).

Instead, you want to access an existing instance of MainActivity . To do this, add a link to the current instance to the static member of your MainActivity class, for example.

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

Then in your AddStudent class, access through

 MainActivity.activity.notifyArray() 

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

+3
source

notifyArray() is called before onCreate .

+1
source

Try calling getArray (). add (et.getText (). toString ()); and notifyArray (); inside onResume () MainActivity and NOT from AddStudentActivity (not recommended!)

So, onResume (), you would ideally want to add a new student to the list, so in your case you can get the student’s name using a common common object such as a hash table or something similar, make it single, and use this from anywhere in the application

A general class might look something like this:

 class CommonHashtable{ private static Hashtable<String, Object> commonHashtable = null; public static getInstance(){ if(commonHashtable == null) commonHashtable = new Hashtable<String, Object>(); return commonHashtable; } 

on getInstance (), it returns a commonHashtable that can be used to temporarily store values!

so add this to the clickbutton click event

 Hashtable hash = CommonHashtable.getInstance(); hash.put("NEW_STUDENT_NAME", et.getText().toString()); 

and add this to you onResume () from MainActivity

 Hashtable hash = CommonHashtable.getInstance(); Object studentName = (String) hash.get("NEW_STUDENT_NAME"); if(studentName != null){ notifyArray(); } 
0
source

All Articles