0

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.targetsoftsystem.sonyproject, PID: 14326
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.targetsoftsystem.sonyproject/com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.callDataList(AppointmentActivity.java:45)
at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.onCreate(AppointmentActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6259)

I am Displaying some data in ListView using BaseAdapter that works fine but when I use onItemclickListener to show the content of full arrayoflist it Throws NullPointerException on Null Object Reference. here I am using Arraylist<Model> and List to set data and I pass the listView position using Intent and I get that position through intent and I assign that position to ArrayList .I post my Code below guide me what wrong I am doing

Class Where I Set the Value to Array List and Intent in onItemClickListener

public class ReservationActivity extends BaseActivity<DBAccess> implements VolleyTasksListener,AdapterView.OnItemClickListener{

private ListView lstReserve;
private ArrayList<ReservationList> reservationLists;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.activity_reservation);

    findViews();
}

private void findViews() {
    lstReserve = (ListView)findViewById(R.id.lst_reservation);
    lstReserve.setOnItemClickListener(this);

    reservationLists = new ArrayList<ReservationList>();
    ReservationList reservationList = new ReservationList();
    reservationList.setName("Rahul");
    reservationList.setCCID("q123");
    reservationList.setServiceType("Repair");
    reservationList.setLocation("T Nagar");

    reservationList.setCallFrom("Delear");
    reservationList.setAppointmentAge("4");
    reservationList.setAppointmentDate("17-04-1992");
    reservationList.setDelearName("Sony Anna Nagar");
    reservationList.setDelearMobile("995222145");
    reservationList.setCustomerAddress("41/27 2nd Circular Road, JawaharNagar,Chennai-600082");

    ReservationList reservationList1 = new ReservationList();
    reservationList1.setName("Tamil");
    reservationList1.setCCID("q987");
    reservationList1.setServiceType("Service");
    reservationList1.setLocation("KK Nagar");

    ReservationList reservationList2 = new ReservationList();
    reservationList2.setName("Selvi");
    reservationList2.setCCID("W123");
    reservationList2.setServiceType("Service");
    reservationList2.setLocation("KK Nagar");

    ReservationList reservationList3 = new ReservationList();
    reservationList3.setName("Ravi");
    reservationList3.setCCID("W456");
    reservationList3.setServiceType("Instal");
    reservationList3.setLocation("Anna Nagar");

    reservationLists.add(reservationList);
    reservationLists.add(reservationList1);
    reservationLists.add(reservationList2);
    reservationLists.add(reservationList3);

    ReservationList reservationList4 = new ReservationList();
    reservationList4.setName("Rahul");
    reservationList4.setCCID("q123");
    reservationList4.setServiceType("Repair");
    reservationList4.setLocation("T Nagar");

    ReservationList reservationList5 = new ReservationList();
    reservationList5.setName("Tamil");
    reservationList5.setCCID("q987");
    reservationList5.setServiceType("Service");
    reservationList5.setLocation("KK Nagar");

    ReservationList reservationList6 = new ReservationList();
    reservationList6.setName("Selvi");
    reservationList6.setCCID("W123");
    reservationList6.setServiceType("Service");
    reservationList6.setLocation("KK Nagar");

    ReservationList reservationList7 = new ReservationList();
    reservationList7.setName("Ravi");
    reservationList7.setCCID("W456");
    reservationList7.setServiceType("Instal");
    reservationList7.setLocation("Anna Nagar");

    reservationLists.add(reservationList);
    reservationLists.add(reservationList1);
    reservationLists.add(reservationList2);
    reservationLists.add(reservationList3);
    reservationLists.add(reservationList4);
    reservationLists.add(reservationList5);
    reservationLists.add(reservationList6);
    reservationLists.add(reservationList7);

    ReservationAdapter reservationAdapter = new ReservationAdapter(ReservationActivity.this,reservationLists);
    lstReserve.setAdapter(reservationAdapter);
}

@Override
public void handleError(Exception error) {

}

@Override
public void handleResult(String method_name, JSONObject response) {

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this,AppointmentActivity.class);
    intent.putExtra("Key_POSITION",position);
    startActivity(intent);
    finish();
}}

Class where I need to display full data of list in TextView

public class AppointmentActivity extends BaseActivity<DBAccess> implements VolleyTasksListener {

private TextView txtCallFrom;
private TextView txtAppointmentAge;
private TextView txtAppointmentDate;
private TextView txtAddress;
private TextView txtCustomerName;
private TextView txtLocation;
private TextView txtDelearName;
private TextView txtDelearMobile;
private int position;
private ArrayList<ReservationList> reservationLists;

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

    findViews();
    callDataList();
}

private void callDataList() {

    Intent intent = getIntent();
    position = intent.getExtras().getInt("Key_POSITION");

    txtCallFrom.setText("Call From : "+reservationLists.get(position).getCallFrom());
    txtAppointmentAge.setText("Call From : "+reservationLists.get(position).getAppointmentAge());
    txtAppointmentDate.setText("Call From : "+reservationLists.get(position).getAppointmentDate());
    txtAddress.setText("Call From : "+reservationLists.get(position).getCustomerAddress());
    txtCustomerName.setText("Call From : "+reservationLists.get(position).getName());
    txtLocation.setText("Call From : "+reservationLists.get(position).getLocation());
    txtDelearName.setText("Call From : "+reservationLists.get(position).getDelearName());
    txtDelearMobile.setText("Call From : "+reservationLists.get(position).getDelearMobile());
}

private void findViews() {
    txtCallFrom = (TextView)findViewById(R.id.txt_call_from);
    txtAppointmentAge = (TextView)findViewById(R.id.txt_app_age);
    txtAppointmentDate= (TextView)findViewById(R.id.txt_app_date);
    txtAddress= (TextView)findViewById(R.id.txt_address);
    txtCustomerName= (TextView)findViewById(R.id.txt_customer_name);
    txtLocation= (TextView)findViewById(R.id.txt_location);
    txtDelearName= (TextView)findViewById(R.id.txt_delear_name);
    txtDelearMobile= (TextView)findViewById(R.id.txt_delear_mobile);
}}
USER9561
  • 1,084
  • 3
  • 15
  • 41
Rahul
  • 404
  • 4
  • 16
  • Where you have intialized reservationLists in AppointmentActivity? – Raghavendra Oct 20 '16 at 05:07
  • your `reservationLists` is null in your `AppointmentActivity`. Initialize `reservationLists` – SripadRaj Oct 20 '16 at 05:07
  • I have initialized reservationlists to appointmentActivity but now getting ' Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.callDataList(AppointmentActivity.java:45)at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.onCreate(AppointmentActivity.java:37)' – Rahul Oct 20 '16 at 05:13

2 Answers2

0

You need to pass the data to AppointmentActivity using intent.

Instead of passing position you can get the corresponding data from reservationLists and pass that using intent. Now, in AppointmentActivity get that data and use it directly.

In ReservationActivity's onItemClick

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this,AppointmentActivity.class);
intent.putExtra("DATA",reservationLists.get(position));
startActivity(intent);
finish();
}

Now in AppointmentActivity

private void callDataList() {

Intent intent = getIntent();
ReservationList reservationList = getIntent().getSerializableExtra("DATA");

txtCallFrom.setText("Call From : "+reservationList.getCallFrom());
txtAppointmentAge.setText("Call From : "+reservationList.getAppointmentAge());
txtAppointmentDate.setText("Call From : "+reservationList.getAppointmentDate());
//....

}

Also, as mentioned by MSS you need to set a view for Activity before initializing any view in activity.

Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0
 **findViews();** in AppointmentActivity.java cause the problem

You are starting an activity(appointmentactivity) from ReservationActivity, but findViews() resides in ReservationActivity.

When new activity appeared, You can not touch UI of previous activity and You can not get value from previous activity with out use of static/bundle/serialization.