java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I need to give validation for every edit text of main XML and save button is also scrolling and date is showing in Text view of date,after save that person details should be added to the next screen at top of the List View ,already List view had five defalut person details,i am unable to do this please help me,in List View every items should consists update and delete button ,click on update dailog should open and need to be edited in dailog,delete means delete person details from the listview.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:layout_marginRight="30dp"
android:padding="15dp"
android:text="Name:"
android:textSize="15sp" />
<EditText
android:id="@+id/et_name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:hint="Enter Your Name"
android:padding="15dp"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:padding="15dp"
android:text="Email:"
android:textSize="15dp" />
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:hint="Enter Your Mail"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_phoneno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:padding="15dp"
android:text="Phone No:"
android:textSize="15dp" />
<EditText
android:id="@+id/et_phoneno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:hint="Enter your Contact No"
android:padding="15dp"
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="2">
<TextView
android:id="@+id/tv_dob"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1"
android:text="DOB:"
android:textSize="15dp"
/>
<Button
android:id="@+id/datepicker1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="70dp"
android:layout_weight="1"
android:textSize="15dp">
</Button>
</LinearLayout>
<TextView
android:id="@+id/tv_qualification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:padding="15dp"
android:text="Qualification"
android:textSize="15dp" />
<EditText
android:id="@+id/et_qualification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:hint="Enter Your Qualification"
android:padding="15dp"
android:textSize="15dp" />
<LinearLayout
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="2">
<TextView
android:id="@+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1"
android:text="Time:"
android:textSize="15dp"
/>
<Button
android:id="@+id/timepicker1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="70dp"
android:layout_weight="1"
android:textSize="15dp">
</Button>
</LinearLayout>
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_gravity="center"
android:textSize="15dp">
</Button>
</LinearLayout>
</ScrollView>
</RelativeLayout
Person.java
package com.example.android.persondetails;
public class Person {
private String name;
private String contactno;
private String email;
private String qualification;
private String datepicker;
private String timepicker;
public Person(String name, String contactno, String email, String qualification, String datepicker, String timepicker) {
this.name = name;
this.contactno = contactno;
this.qualification=qualification;
this.datepicker=datepicker;
this.email=email;
this.timepicker=timepicker;
}
public String getName() {
return name == null ? "" : name;
}
public void setName(String name) {
this.name = name;
}
public String getContactno() {
return contactno;
}
public void setContactno(String contactno){
this.contactno=contactno;
}
public String getEmail() {
return email;
}
public String getQualification() {
return qualification;
}
public String getDatepicker() {
return datepicker;
}
public String getTimepicker() {
return timepicker;
}
}
SecondActivity.java
package com.example.android.persondetails;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static java.util.Calendar.AM;
public class SecondActivity extends AppCompatActivity {
private ListView lvContact;
private Button addBtn,dleBtn;
private PersonListAdapter adapter;
private List<Person> mPersonList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
ArrayList<Person> mPersonList = new ArrayList<Person>();
addBtn = (Button) findViewById(R.id.update);
dleBtn=(Button)findViewById(R.id.delete);
lvContact = (ListView) findViewById(R.id.listView);
addMethod();
setAdapter();
Intent intent = getIntent();
String text;
text = (String) intent.getStringExtra(MainActivity.EXTRA_NAME);
String text1 = (String) intent.getStringExtra(MainActivity.EXTRA_EMAIL);
String text3 = (String) intent.getStringExtra(MainActivity.EXTRA_PHONE_NUMBER);
String text4 = (String) intent.getStringExtra(MainActivity.EXTRA_DOB);
String text5 = (String) intent.getStringExtra(MainActivity.EXTRA_TIME);
String text2 = (String)intent.getStringExtra(MainActivity.EXTRA_QUALIFICATION);
Log.d("PersonDetails", text + " " + text1 + "" + text2 + "" + text3 + "" + text4 + "" + text5);
}
private void addMethod() {
mPersonList.add(new Person("Narendra", "83330072", "narendraprasad616@gmail.com", "btech", "14 / 06 / 1996", "5-30 AM"));
mPersonList.add(new Person("Banu", "715452", "naredra@gmail.com", "btech", "14 / 06 / 1996", "5-45 AM"));
mPersonList.add(new Person("Kalyan", "83330072", "narendraprad616@gmail.com", "btech", "14 / 06 / 1998", "5-30 AM"));
mPersonList.add(new Person("Teja", "8775564", "narendraprasa16@gmail.com", "btech", "14 / 06 / 1990", "5-30 AM"));
mPersonList.add(new Person("Manu", "86423", "narendraprad616@gmail.com", "btech", "14 / 06 / 1996", "5-30 AM"));
}
private void setAdapter() {
adapter = new PersonListAdapter(SecondActivity.this, mPersonList);
lvContact.setAdapter(adapter);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.persondetails">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".SecondActivity">
</activity>
<activity android:name=".MainActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
PersonListAdapter.java
package com.example.android.persondetails;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class PersonListAdapter extends BaseAdapter {
private Context mContext;
private List<Person> mPersonList;
PersonListAdapter(Context mContext, List<Person> mPersonList) {
this.mContext = mContext;
this.mPersonList = mPersonList;
}
@Override
public int getCount() {
return mPersonList.size();
}
@Override
public Object getItem(int position) {
return mPersonList.get(position);
}
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.second_layout, parent, false);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name1);
holder.tv_contactno = (TextView) convertView.findViewById(R.id.tv_phoneno);
holder.tv_email=(TextView)convertView.findViewById(R.id.tv_email);
holder.tv_dob=(TextView)convertView.findViewById(R.id.tv_dob);
holder.tv_qualification=(TextView)convertView.findViewById(R.id.tv_qualification);
holder.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_name.setText(mPersonList.get(position).getName());
holder.tv_contactno.setText(mPersonList.get(position).getContactno());
holder.tv_email.setText(mPersonList.get(position).getEmail());
holder.tv_dob.setText(mPersonList.get(position).getDatepicker());
holder.tv_qualification.setText(mPersonList.get(position).getQualification());
holder.tv_time.setText(mPersonList.get(position).getTimepicker());
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView tv_name;
TextView tv_email;
TextView tv_contactno;
TextView tv_dob;
TextView tv_qualification;
TextView tv_time;
}
}
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name1"
android:text="Name"
android:textSize="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_phoneno"
android:layout_below="@+id/tv_name1"
android:text="contactno"
android:textSize="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_phoneno"
android:id="@+id/tv_email"
android:text="Email"
android:textSize="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_dob"
android:layout_below="@+id/tv_email"
android:text="DOB"
android:textSize="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_qualification"
android:layout_below="@+id/tv_dob"
android:text="Qualification"
android:textSize="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_time"
android:layout_below="@+id/tv_qualification"
android:text="Time"
android:textSize="15dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="120dp"
android:weightSum="2"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_time"
android:layout_weight="1"
android:textSize="15dp"
android:text="Delete"
android:padding="10dp"
android:id="@+id/delete"
android:layout_marginLeft="10dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:text="Upadate"
android:padding="10dp"
android:id="@+id/update"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</RelativeLayout>
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Person">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
MainActivity.java
package com.example.android.persondetails;
import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.content.Intent; import android.icu.text.SimpleDateFormat; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker;
import java.text.ParseException; import java.util.Calendar; import java.util.Date;
import static android.content.Intent.EXTRA_EMAIL; import static android.content.Intent.EXTRA_PHONE_NUMBER; import static android.os.DropBoxManager.EXTRA_TIME; import static android.security.KeyChain.EXTRA_NAME;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String EXTRA_NAME = "com.example.android.persondetails.EXTRA_NAME";
public static final String EXTRA_EMAIL = "com.example.android.persondetails.EXTRA_EMAIL";
public static final String EXTRA_PHONE_NUMBER = "com.example.android.persondetails.EXTRA_PHONE_NUMBER";
public static final String EXTRA_DOB = "com.example.android.persondetails.EXTRA_DOB";
public static final String EXTRA_QUALIFICATION = "com.example.android.persondetails.EXTRA_QUALIFICATION";
public static final String EXTRA_TIME = "com.example.android.persondetails.EXTRA_TIME";
Button button1, button2, button3;
TextView textView1, textView2, textView3, textView4, textView5, textView6;
EditText editText1, editText2, editText3, editText4;
private int mYear, mMonth, mDay, mHour, mMinute, format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button3 = (Button) findViewById(R.id.button_save);
textView1 = (TextView) findViewById(R.id.tv_name1);
textView2 = (TextView) findViewById(R.id.tv_email);
textView3 = (TextView) findViewById(R.id.tv_phoneno);
textView4 = (TextView) findViewById(R.id.tv_dob);
textView5 = (TextView) findViewById(R.id.tv_qualification);
textView6 = (TextView) findViewById(R.id.tv_time);
button1 = (Button) findViewById(R.id.datepicker1);
button2 = (Button) findViewById(R.id.timepicker1);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == button1) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
format = c.get(Calendar.AM_PM);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
textView4.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == button2) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
format = c.get(Calendar.AM_PM);
final TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String time = selectedHour + ":" + selectedMinute;
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
Date date = null;
try {
date = fmt.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat fmtOut = new SimpleDateFormat("hh:mm aa");
String formattedTime = fmtOut.format(date);
textView6.setText(formattedTime);
}
}, mHour, mMinute, false);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
if (v == button3) {
EditText editText1 = (EditText) findViewById(R.id.et_name1);
String text = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.et_email);
String text1 = editText2.getText().toString();
EditText editText3 = (EditText) findViewById(R.id.et_phoneno);
String number = String.valueOf(editText3.getText().toString());
Button button1 = (Button) findViewById(R.id.datepicker1);
String number1 = button1.getText().toString();
EditText editText4 = (EditText) findViewById(R.id.et_qualification);
String text2 = editText4.getText().toString();
Button button2 = (Button) findViewById(R.id.timepicker1);
String number2 = button2.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(EXTRA_NAME, text);
intent.putExtra(EXTRA_EMAIL, text1);
intent.putExtra(EXTRA_PHONE_NUMBER, number);
intent.putExtra(EXTRA_DOB, number1);
intent.putExtra(EXTRA_QUALIFICATION, text2);
intent.putExtra(EXTRA_TIME, number2);
startActivity(intent);
}
}
}