Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a custom list view to show some data. Now when I'm entering the data the items doesn't show and my app crashes please help me I used this link for my
Show Multiple data from SQLite database inside ListView in android one by one - Android Examples[^]

What I have tried:

1. LeaveTran Activity:

 LeaveDb myDb;
    Button ShowApprovedStatus;
    //Declaration TextInputLayout
    TextInputLayout textInputGoingAbroad;
    TextInputLayout textInputLayoutDate;
    TextInputLayout textInputLeaveType1;
    TextInputLayout textInputLeaveType2;
    TextInputLayout textInputDays1;
    TextInputLayout textInputExpStDt;
    TextInputLayout textInputExpEndDt;
    TextInputLayout textInputReqType;
    TextInputLayout textInputStatus;
    TextInputLayout textInputSubmit;
    //Declaration EditTexts
    EditText  check_date, Days1, ExpStDt, ExpEndDt;

    
    //Declaration Spinners

      private Spinner LeaveType1, LeaveType2;
    private Spinner ReqType;
    //Declaration Checkbox
    CheckBox Status, GoingAbroad;
    private LinearLayout parentLinearLayout;
    Button onAddField, onDelete ;
<pre> @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_tran);

        myDb = new LeaveDb(this);
        ShowApprovedStatus = (Button) findViewById(R.id.Submit);
        parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
        onAddField = (Button) findViewById(R.id.add_field_button);
        onDelete = (Button) findViewById(R.id.delete_button);




        textInputLeaveType1 = (TextInputLayout) findViewById(R.id.textInputLeaveType1);
        textInputReqType = (TextInputLayout) findViewById(R.id.textInputReqType);
        textInputLeaveType2  = (TextInputLayout) findViewById(R.id.textInputLeaveType2);
        onAddField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View rowView = inflater.inflate(R.layout.field, null);
                // Add the new row before the add field button.
                parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
            }
        });

        onDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                parentLinearLayout.removeView((View) v.getParent());
            }
        });
        //Here i can use Start date as a 01/01/2018 and End Date is 01/01/2019
        try {
          System.out.println ("Days: " + getDaysBetweenDates("2019-05-14","2019-05-18"));

        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        //Cast variables over here.
        final Calendar myCalendar1 = Calendar.getInstance();



        check_date = (EditText) findViewById(R.id.check_date);
        textInputLayoutDate = (TextInputLayout) findViewById(R.id.textInputLayoutDate);
        // Code for Req Date Picker
        final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                myCalendar1.set(Calendar.YEAR, year);
                myCalendar1.set(Calendar.MONTH, month);      // Here you can get day,month and year.
                myCalendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
            private void updateLabel() {
                String myFormat = "yyyy-MM-dd"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                check_date.setText(sdf.format(myCalendar1.getTime()));

            }
        };
        check_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(LeaveTran.this, date2, myCalendar1
                        .get(Calendar.YEAR), myCalendar1.get(Calendar.MONTH),
                        myCalendar1.get(Calendar.DAY_OF_MONTH)).show();

            }
        });
       // Code to retrieve Requested Date

        Days1 = (EditText) findViewById(R.id.Days1);

        textInputDays1 = (TextInputLayout)findViewById(R.id.textInputDays1);

        GoingAbroad = (CheckBox) findViewById(R.id.GoingAbroad);
        textInputGoingAbroad = (TextInputLayout)findViewById(R.id.textInputGoingAbroad);

        final Calendar startDate = Calendar.getInstance();
        textInputExpStDt = (TextInputLayout)findViewById(R.id.textInputExpStDt);
        ExpStDt = (EditText) findViewById(R.id.ExpStDt);
        // Code for ExpStDt picker
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){

            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                startDate.set(Calendar.YEAR, year);
                startDate.set(Calendar.MONTH, month);      // Here you can get day,month and year.
                startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
            private void updateLabel() {

                String myFormat = "yyyy-MM-dd"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                ExpStDt.setText(sdf.format(startDate.getTime()));
            }
        };
        ExpStDt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(LeaveTran.this, date, startDate
                        .get(Calendar.YEAR), startDate.get(Calendar.MONTH),
                        startDate.get(Calendar.DAY_OF_MONTH)).show();
                String outputDate = addDays(new Date(),6 ); //for current dates
                ExpEndDt.setText(outputDate);


            }
        });
        final Calendar endDate = Calendar.getInstance();
        textInputExpEndDt = (TextInputLayout)findViewById(R.id.textInputExpEndDt);
        ExpEndDt = (EditText) findViewById(R.id.ExpEndDt);
        // Code for ExpEndDt picker
        final DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                endDate.set(Calendar.YEAR, year);
                endDate.set(Calendar.MONTH, month);       // Here you can get day,month and year.
                endDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();


            }
            private void updateLabel() {
                String myFormat = "yyyy-MM-dd"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                ExpEndDt.setText(sdf.format(endDate.getTime()));
            }
        };
        ExpEndDt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(LeaveTran.this, date1, endDate
                        .get(Calendar.YEAR), endDate.get(Calendar.MONTH),
                        endDate.get(Calendar.DAY_OF_MONTH)).show();

            }
        });

        Status = (CheckBox) findViewById(R.id.Status);
        textInputStatus = (TextInputLayout)findViewById(R.id.textInputStatus);
        ShowApprovedStatus();
        addItemsOnSpinner2();
        //addItemsOnSpinner1();
        addItemsOnSpinner3();

    }

    // add items into spinner dynamically
    private void addItemsOnSpinner3() {
        ReqType = (Spinner)findViewById(R.id.ReqType);
        List<String> list3 = new ArrayList<String>();
        list3.add("Need Advance Payment");
        list3.add("No Advance Payment");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list3);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ReqType.setAdapter(dataAdapter);
    }

    // add items into spinner dynamically
   /* private void addItemsOnSpinner1() {
        LeaveType2 = (Spinner) findViewById(R.id.LeaveType2);

        List<String> list2 = new ArrayList<String>();
        list2.add("Annual Leave");
        list2.add("Medical Leave Absence");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list2);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        LeaveType2.setAdapter(dataAdapter);

    }*/

    // add items into spinner dynamically
    private void addItemsOnSpinner2() {
        LeaveType1 = (Spinner)findViewById(R.id.LeaveType1);

        List<String> list = new ArrayList<String>();
        list.add("Please Select Leave Type");
        list.add("Annual Leave");
        list.add("Medical Leave Absence");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        LeaveType1.setAdapter(dataAdapter);

    }


    // Calculate days to get the end date
    private String addDays(Date startDate, int Days) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(dateFormat.parse(String.valueOf(startDate)));
        } catch (android.net.ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        c.add(Calendar.DAY_OF_WEEK,Days);
        String resultDate=dateFormat.format(c.getTime());
        return resultDate;
    }


    // Code get number of days
    private long getDaysBetweenDates(String  ExpStDt, String  ExpEndDt) throws java.text.ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
        Date startDate, endDate;
        long Days = 0;
        try {
            startDate = dateFormat.parse(ExpStDt);
            endDate = dateFormat.parse(ExpEndDt);

            Days =  getUnitBetweenDates(startDate, endDate, TimeUnit.DAYS);
        } catch (android.net.ParseException e) {
            e.printStackTrace();
        }
        return Days;
    }

    private long getUnitBetweenDates(Date startDate, Date endDate, TimeUnit Days) {
        long timeDiff = startDate.getTime() - endDate.getTime();
        return Days.convert(timeDiff, TimeUnit.MILLISECONDS);
    }


    //Create a method for adding data
    public void ShowApprovedStatus () {

        ShowApprovedStatus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // Show Validation
                if (validate()){

                    // Get values from edit text fields.

                   String date = check_date.getText().toString();
                   String Leave1 = LeaveType1.getSelectedItem().toString();
                    String text = LeaveType2.getSelectedItem().toString();
                   String Day1 = Days1.getText().toString();
                   String Exp_St_Dt = ExpStDt.getText().toString();
                   String Exp_End_Dt = ExpEndDt.getText().toString();
                   String Req_Type = ReqType.getSelectedItem().toString();
                   String Status1 = Status.toString();
                }
                // Data inserted or not
                boolean isInserted = myDb.insertData(check_date.getText().toString(),ReqType.getSelectedItem().toString(),LeaveType1.getSelectedItem().toString(), LeaveType2.getSelectedItem().toString(),Days1.getText().toString(), ExpStDt.getText().toString(), ExpEndDt.getText().toString(),Status.toString(),GoingAbroad.toString());

                if (isInserted == true)
                {

                    // TODO Auto-generated method stub

                    Intent intent = new Intent(LeaveTran.this, ListViewActivity.class);
                    startActivity(intent);

                }
                else {
                    Snackbar.make(ShowApprovedStatus, "Data Not Inserted Successfully", Snackbar.LENGTH_LONG).show();
                }

            }
            // This method is used to validate input given by user
            private boolean validate() {
                boolean valid = false;
                // Get values from editText fields
                String date = check_date.getText().toString();
                String Leave1 = LeaveType1.getSelectedItem().toString();
                String text = LeaveType2.getSelectedItem().toString();
                String Day1 = Days1.getText().toString();
                String Exp_St_Dt = ExpStDt.getText().toString();
                String Exp_End_Dt = ExpEndDt.getText().toString();
                String Req_Type = ReqType.getSelectedItem().toString();
                String Status1 = Status.toString();

                // Handling validation for User Name field.

                if (date.isEmpty()){
                    textInputLayoutDate.setError("Please Enter Requested Date");

                } else {
                    valid = true;
                    textInputLayoutDate.setError(null);
                }

                if (Leave1.isEmpty()){
                    valid = false;
                    textInputLeaveType1.setError("Please Choose your Type of Leave No 1");

                } else {
                    valid = true;
                    textInputLeaveType1.setError(null);
                }
                if (text.isEmpty()){
                    valid = false;
                    textInputLeaveType2.setError("Please Choose your Type of Leave No 1");

                } else {
                    valid = true;
                    textInputLeaveType2.setError(null);
                }

                if (Day1.isEmpty()) {
                    valid = false;
                    textInputDays1.setError("Please enter the No. days for Leave Type 1");

                } else {
                    valid = true;
                    textInputDays1.setError(null);
                }
                if (Exp_St_Dt.isEmpty()) {
                    valid = false;
                    textInputExpStDt.setError("Please Enter The Expected Date Where you want to start Your leave");

                } else {

                    valid = true;
                    textInputExpStDt.setError(null);
                }
                if (Exp_End_Dt.isEmpty()) {
                    valid = false;
                    textInputExpEndDt.setError("Please Enter the Expected Date Where you want to End Your leave");

                } else {
                    valid = true;
                    textInputExpEndDt.setError(null);
                }

                if (Req_Type.isEmpty()) {
                    valid = false;
                    textInputReqType.setError("Please Select The Type of Request Leave You Want");

                } else {
                    valid = true;
                    textInputReqType.setError(null);
                }
                if (Status1.isEmpty()) {
                    valid = false;
                    textInputStatus.setError("The status should not go more than 100");

                } else {
                    valid = true;
                    textInputStatus.setError(null);
                }

                // Check validation for end date that is greater or less than start date
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

                try {
                    if(sdf.parse(Exp_St_Dt).before(sdf.parse(Exp_End_Dt)))
                    {
                        valid = true;//If start date is before end date
                        textInputExpStDt.setError(null);

                    }
                    else if(sdf.parse(Exp_St_Dt).equals(sdf.parse(Exp_End_Dt)))
                    {
                        valid = false;//If two dates are equal
                        textInputExpStDt.setError("The start date should not be same with end date");
                        textInputExpEndDt.setError("The End Date should not be same as Start Date");
                    }
                    else if (sdf.parse(Exp_End_Dt).before(sdf.parse(Exp_St_Dt)))
                    {
                        valid = false; //If start date is after the end date
                        textInputExpEndDt.setError("The End Date should not be less than Start Date");

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


                return valid;
            }
        });
    }

}

2. ListView.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListViewActivity">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>
</RelativeLayout>


3. ListViewActivity.java

LeaveDb myDb;
SQLiteDatabase SQLITEDATABASE;
Cursor cursor;
SQLiteListAdapter ListAdapter ;

ArrayList<string> ReqDate_ArrayList = new ArrayList<string>();
ArrayList<string> ReqType_ArrayList = new ArrayList<string>();
ArrayList<string> LeaveType1_ArrayList = new ArrayList<string>();
ArrayList<string> LeaveType2_ArrayList = new ArrayList<string>();
ArrayList<string> Days1_ArrayList = new ArrayList<string>();
ArrayList<string> ExpStDt_ArrayList = new ArrayList<string>();
ArrayList<string> ExpEndDt_ArrayList = new ArrayList<string>();
ArrayList<string> Status_ArrayList = new ArrayList<string>();
ArrayList<string> GoingAbroad_ArrayList = new ArrayList<string>();
ListView LISTVIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);

LISTVIEW = (ListView) findViewById(R.id.listView1);

myDb = new LeaveDb(this);
}
@Override
protected void onResume() {

ShowSQLiteDBdata() ;

super.onResume();
}

private void ShowSQLiteDBdata() {
SQLITEDATABASE = myDb.getWritableDatabase();
cursor = SQLITEDATABASE.rawQuery("SELECT * FROM LeaveTran", null);

ReqDate_ArrayList.clear(); // 1
ReqType_ArrayList.clear(); // 2
LeaveType1_ArrayList.clear(); // 3
LeaveType2_ArrayList.clear(); // 4
Days1_ArrayList.clear(); //5
ExpStDt_ArrayList.clear(); //6
ExpEndDt_ArrayList.clear(); //7
Status_ArrayList.clear(); //8
GoingAbroad_ArrayList.clear(); //9

if (cursor.moveToFirst()) {
do {
ReqDate_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ReqDate))); // 1
ReqType_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ReqType))); // 2
LeaveType1_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_LeaveType1))); // 3
LeaveType2_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_LeaveType2))); // 4
Days1_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_Days1))); // 5
ExpStDt_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ExpStDt))); // 6
ExpEndDt_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ExpEndDt))); // 7
Status_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_Status))); // 8
GoingAbroad_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_GoingAbroad))); //9

} while (cursor.moveToNext());
}
ListAdapter = new SQLiteListAdapter(ListViewActivity.this,
ReqDate_ArrayList, //1
ReqType_ArrayList, //2
LeaveType1_ArrayList, //3
LeaveType2_ArrayList, //4
Days1_ArrayList, //5
ExpStDt_ArrayList, //6
ExpStDt_ArrayList, //7
ExpEndDt_ArrayList, // 8
Status_ArrayList, // 9
GoingAbroad_ArrayList // 10
);
LISTVIEW.setAdapter(ListAdapter);

cursor.close();
}


4. listdatalayout.xml

<relativelayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
="" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin">
<textview
android:id="@+id/textView1"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="ReqDate = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewID"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView1" android:layout_alignbottom="@+id/textView1" android:layout_torightof="@+id/textView1" android:text="ReqDate" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textView2"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView1" android:text="ReqType = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewNAME"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textViewID" android:layout_torightof="@+id/textView2" android:text="ReqType" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textView3"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView2" android:text="LeaveType1 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewPHONE_NUMBER"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textView3" android:layout_torightof="@+id/textView3" android:text="LeaveType1" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textView4"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView3" android:text="LeaveType2 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewSUBJECT"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView4" android:layout_alignbottom="@+id/textView4" android:layout_torightof="@+id/textView4" android:text="LeaveType2" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView6"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView3" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Days1 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewDays1"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView6" android:layout_alignbottom="@+id/textView6" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Days1" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView7"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView4" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="ExpStDt = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewExpStDt"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView7" android:layout_alignbottom="@+id/textView7" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="ExpStDt" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView8"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView6" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="ExpEndDt = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewExpEndDt"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView8" android:layout_alignbottom="@+id/textView8" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="ExpEndDt" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textView9"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView7" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Status = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewStatus"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView9" android:layout_alignbottom="@+id/textView9" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Status" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView10"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView8" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Status = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

<textview
android:id="@+id/textViewGoingAbroad"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView10" android:layout_alignbottom="@+id/textView10" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Status" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">

This is what the shows in my Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.huzai.listview.LeaveTran.ShowApprovedStatus(LeaveTran.java:312)
at com.example.huzai.listview.LeaveTran.onCreate(LeaveTran.java:220)
Posted
Updated 1-Jun-19 23:51pm
Comments
David Crow 3-Jun-19 15:09pm    
Please re-edit your post by removing all but the relevant parts. We're glad to answer specific questions, assuming you've spent time narrowing down the problem, but no one in their right mind is going to wade through your entire project.

BTW, have you looked at line 312 of LeaveTran.java?

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, it will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 14477967 2-Jun-19 6:04am    
@OriginalGriff So when I put a breakpoint near my button.setOnclickListner the spinners were giving me a null value.
So how do I solve it?
OriginalGriff 2-Jun-19 6:32am    
Right, so now you know it's the Shirt pocket that is empty.
So when do you normally put a pen in the shirt pocket? Did that happen today? When should it happen? Or has something happened to the pen or shirt since then?

You wrote this code: you understand how it works, and what it is supposed to do. So when should the spinners be set to a value? Did that happen? What value went into them? What has happened since then?
Start asking those questions, and use the debugger to answer them. When you know what values wnet in, when they went in, and what happened you can start looking at why it happened, and by extension why you now have null values. We can't do that for you - we can't run your code under the same conditions as you do!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900