Thursday 24 November 2016

java - Parcelable error Attempt to invoke virtual method on a null object reference

I am trying to use parcelable to make a simple edit form. The data was received from an array list. But, when I try to run it, there is an error:




Attempt to invoke virtual method 'int com.insiteprojectid.practiceadapter.user.Student.getId()' on a null object reference




These are my codes:



public class StudentActivity extends AppCompatActivity {


private ListView lv;
private CustomUsersAdapter customUsersAdapter;
private TextView emptyTextView;
private StaticStudent staticStudent;

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

customUsersAdapter = new CustomUsersAdapter(this, new ArrayList());
lv = (ListView)findViewById(R.id.listView);
lv.setAdapter(customUsersAdapter);
emptyTextView = (TextView)findViewById(R.id.emptyView);
lv.setEmptyView(emptyTextView);
staticStudent = StaticStudent.getInstance();

FloatingActionButton floatingActionButton = (FloatingActionButton)findViewById(R.id.fabButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),AddStudentActivity.class);
intent.putExtra("action","add");
startActivity(intent);
}
});

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {

Intent intent = new Intent(getApplicationContext(), AddStudentActivity.class);
Student student = staticStudent.get(position);
intent.putExtra("StudentList", student);
startActivity(intent);
}
});
}

private void populateStudentDummies() {
ArrayList studentList = new ArrayList<>();

studentList.add(new Student(1, "3135136188", "TRI FEBRIANA SIAMI", "tri.febriana@unj.ac.id", "021577888"));
studentList.add(new Student(2, "3135136192", "UMMU KULTSUM", "ummu.kultsum@unj.ac.id", "021577888"));
studentList.add(new Student(3, "3135136215", "ANDREAN OKTAVIANUS H.S.", "andrean.ohs@unj.ac.id", "021577888"));
staticStudent.AddStudents(studentList);
customUsersAdapter = new CustomUsersAdapter(this,staticStudent.getList());
lv.setAdapter(customUsersAdapter);
}

@Override
protected void onResume() {

//overriding method
super.onResume();
//check size of student list
if(staticStudent.count()==0) {
customUsersAdapter = new CustomUsersAdapter(this, new ArrayList());
//set emptyView
emptyTextView.setText("No Student Found");
} else{
customUsersAdapter = new CustomUsersAdapter(this, staticStudent.getList());
}

lv.setAdapter(customUsersAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_student_list, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.createDummy:
populateStudentDummies();
return true;
case R.id.clearList:
StaticStudent.getInstance().clearList();
customUsersAdapter = new CustomUsersAdapter(this, new ArrayList());

lv.setAdapter(customUsersAdapter);
return true;
default:
return super.onOptionsItemSelected(item);
}
}


the Parcelable subclass:




public class Student implements Parcelable{
private int id;
private String noreg;
private String name;
private String mail;
private String phone;

public Student(int id, String noreg, String name, String mail, String phone){
this.id = id;
this.name = name;

this.mail = mail;
this.phone = phone;
this.noreg = noreg;
}

protected Student(Parcel in) {
this.id = in.readInt();
this.noreg = in.readString();
this.name = in.readString();
this.mail = in.readString();

this.phone = in.readString();
}

public Student(){

}

public static final Creator CREATOR = new Creator() {
@Override
public Student createFromParcel(Parcel in) {

return new Student(in);
}

@Override
public Student[] newArray(int size) {
return new Student[size];
}
};

public int getId() {

return id;
}

public void setId(int id) {
this.id = id;
}

public String getNoreg() {
return noreg;
}


public void setNoreg(String noreg) {
this.noreg = noreg;
}

public String getName() {
return name;
}

public void setName(String name) {

this.name = name;
}

public String getMail() {
return mail;
}

public void setMail(String mail) {
this.mail = mail;
}


public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}



@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(getId());
dest.writeString(getNoreg());
dest.writeString(getName());

dest.writeString(getMail());
dest.writeString(getPhone());
}
}


and then the method



public class StaticStudent {
private static ArrayList studentList = new ArrayList<>();

private static StaticStudent instance = new StaticStudent();

public static ArrayList getStudentList() {
return studentList;
}

public static void setStudentList(ArrayList studentList) {
StaticStudent.studentList = studentList;
}


public static StaticStudent getInstance() {
return instance;
}

public static void setInstance(StaticStudent instance) {
StaticStudent.instance = instance;
}

public void addStudent(Student student){
student.setId(nextId());

studentList.add(student);
}

public int nextId(){
return studentList.size()+1;
}

public Student removeLast(){
Student student = studentList.remove(studentList.size()-1);
return student;

}

public Student get(int index){
Student student = studentList.get(index);
return student;
}

public void set(int index, Student student){
studentList.set(index, student);
}


public Student remove(int index){
Student student = studentList.remove(index);
resetCounterId(index);
return student;
}

public Student getLast(){
Student student = studentList.get(studentList.size() - 1);
return student;

}

public void AddStudents(ArrayList students){
studentList.addAll(students);
resetCounterId(0);
}

public ArrayList getList(){
return studentList;
}


public int count(){
return studentList.size();
}

private void resetCounterId(int i){
for (int j = i; j < studentList.size(); j++) {
studentList.get(j).setId(j);
}
}


public void clearList(){
studentList.clear();
}

}


this is the logcat





10-20 11:23:34.753 22842-22842/com.insiteprojectid.practiceadapter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.insiteprojectid.practiceadapter, PID: 22842
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.insiteprojectid.practiceadapter/com.insiteprojectid.practiceadapter.AddStudentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.insiteprojectid.practiceadapter.user.Student.getId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)

at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.insiteprojectid.practiceadapter.user.Student.getId()' on a null object reference
at com.insiteprojectid.practiceadapter.AddStudentActivity.onCreate(AddStudentActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
at android.app.ActivityThread.access$800(ActivityThread.java:169) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5546) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...