Boolean java util list add

java.lang.NullPointerException: Attempt to invoke interface method ‘boolean java.util.List.add(java.lang.Object)’ on a null object reference [duplicate]

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.

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; >> 
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 ListmPersonList; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); ArrayListmPersonList = new ArrayList(); 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); >> 
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 ListmPersonList; PersonListAdapter(Context mContext, List 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; >> 
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); >> > 

Источник

Читайте также:  Очистить css от неиспользуемых стилей css

Interface List

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2) , and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator , add , remove , equals , and hashCode methods. Declarations for other inherited methods are also included here for convenience.

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

The List interface provides a special iterator, called a ListIterator , that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

Читайте также:  8 html css backgrounds

The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.

The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.

Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException . Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as «optional» in the specification for this interface.

Unmodifiable Lists

  • They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List’s contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException .
  • They are serializable if all elements are serializable.
  • The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.
  • The lists and their subList views implement the RandomAccess interface.
  • They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
  • They are serialized as specified on the Serialized Form page.

This interface is a member of the Java Collections Framework.

Источник

Interface List

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2) , and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

Читайте также:  Python get key press

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator , add , remove , equals , and hashCode methods. Declarations for other inherited methods are also included here for convenience.

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

The List interface provides a special iterator, called a ListIterator , that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.

The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.

Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException . Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as «optional» in the specification for this interface.

Unmodifiable Lists

  • They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List’s contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException .
  • They are serializable if all elements are serializable.
  • The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.
  • The lists and their subList views implement the RandomAccess interface.
  • They are value-based. Programmers should treat instances that are equal as interchangeable and should not use them for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones.
  • They are serialized as specified on the Serialized Form page.

This interface is a member of the Java Collections Framework.

Источник

Оцените статью