Search code examples
androidcheckboxnullpointerexceptionischecked

Android CheckBox isChecked() throws nullpointereception


I have another problem. I defined CheckBoxes in layout and I'm getting their state with isChecked() method in a class. However I'm getting an error whenever this method is called with the following error log:

09-05 14:55:58.457: ERROR/AndroidRuntime(582): FATAL EXCEPTION: main
09-05 14:55:58.457: ERROR/AndroidRuntime(582): java.lang.IllegalStateException: Could not execute method of the activity
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.view.View$1.onClick(View.java:2144)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.view.View.performClick(View.java:2485)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.view.View$PerformClick.run(View.java:9080)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.os.Handler.handleCallback(Handler.java:587)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.os.Looper.loop(Looper.java:123)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invoke(Method.java:507)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at dalvik.system.NativeStart.main(Native Method)
09-05 14:55:58.457: ERROR/AndroidRuntime(582): Caused by: java.lang.reflect.InvocationTargetException
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at java.lang.reflect.Method.invoke(Method.java:507)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at android.view.View$1.onClick(View.java:2139)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     ... 11 more
09-05 14:55:58.457: ERROR/AndroidRuntime(582): Caused by: java.lang.NullPointerException
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     at alesito.WorkingHoursLogger.zapolniDelavnike.shraniUrnik(zapolniDelavnike.java:88)
09-05 14:55:58.457: ERROR/AndroidRuntime(582):     ... 14 more

This is class code:

private DatabaseAdapter dbOperator;
private Cursor cursor;
private static final String TABLE_NAME = "hours";
public static final String COL_ROWID = "_id";
public static final String COL_DATE = "date";
public static final String COL_DATE_TYPE = "dateType";
public static final String COL_DEF_HOURS = "defaultHours";
public static final String COL_COR_HOURS = "correctionHours";
public static final String COL_OPOMBA = "opomba";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dnevi);
    dbOperator = new DatabaseAdapter(this);
    dbOperator.open();

}
public void zapolniDni(View view){
    setContentView(R.layout.zapolni);
}
public void shraniUrnik(View view) throws ParseException{
    DatePicker zacetniDatumPicker = (DatePicker)findViewById(R.id.zacetniDatum);
    DatePicker koncniDatumPicker = (DatePicker)findViewById(R.id.koncniDatum);


    TimePicker zacetniUraPicker = (TimePicker)findViewById(R.id.zacetnaUra);
    Time zacetnaUra = new Time();
    zacetnaUra.set(0, zacetniUraPicker.getCurrentMinute(), zacetniUraPicker.getCurrentHour(), 0, 0, 0);

    TimePicker koncniUraPicker = (TimePicker)findViewById(R.id.koncnaUra);
    Time koncnaUra = new Time();
    koncnaUra.set(0, koncniUraPicker.getCurrentMinute(), koncniUraPicker.getCurrentHour(), 0, 0, 0);

    double razlika = 0;
    if (Time.compare(zacetnaUra, koncnaUra) < 0){
        double zacetniCas = zacetnaUra.hour * 60 + zacetnaUra.minute;
        double koncniCas = koncnaUra.hour * 60 + koncnaUra.minute;
        razlika = (koncniCas - zacetniCas) / 60;
    }

    CheckBox ponedeljek = (CheckBox)findViewById(R.id.ponedeljek);
    CheckBox torek = (CheckBox)findViewById(R.id.torek);
    CheckBox sreda = (CheckBox)findViewById(R.id.sreda);
    CheckBox cetrtek = (CheckBox)findViewById(R.id.cetrtek);
    CheckBox petek = (CheckBox)findViewById(R.id.petek);
    CheckBox sobota = (CheckBox)findViewById(R.id.sobota);
    CheckBox nedelja = (CheckBox)findViewById(R.id.nedelja);


    GregorianCalendar gcal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.d");
    Date startDate = sdf.parse(zacetniDatumPicker.getYear()+"."+ (zacetniDatumPicker.getMonth()+1)+"."+ zacetniDatumPicker.getDayOfMonth());
    Date endDate = sdf.parse(koncniDatumPicker.getYear()+"."+ (koncniDatumPicker.getMonth()+1)+"."+ (koncniDatumPicker.getDayOfMonth()+1));


    gcal.setTime(startDate);
    while (gcal.getTime().before(endDate)){
        switch (gcal.getTime().getDay()){
            case 0:
                if (nedelja.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 1:
                if (ponedeljek.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 2:
                if (torek.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 3:
                if (sreda.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 4:
                if (cetrtek.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 5:
                if (petek.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            case 6:
                if (sobota.isChecked()){
                    dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, "");
                }
            default:
                Toast nicOznaceno = Toast.makeText(this, "nič ni označeno", Toast.LENGTH_LONG);
                nicOznaceno.show();
        }
        gcal.add(gcal.DAY_OF_YEAR, 1);
    }
}

And this is layout file:

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="">
    <TextView 
        android:id="@+id/navodiloDnevi"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/navodiloDnevi"/>
    <CheckBox 
        android:id="@+id/ponedeljek"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/navodiloDnevi"
        android:text="@string/ponedeljek"/>
    <CheckBox 
        android:id="@+id/torek"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/ponedeljek"
        android:text="@string/torek"/>
    <CheckBox 
        android:id="@+id/sreda"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/torek"
        android:text="@string/sreda"/>
    <CheckBox 
        android:id="@+id/cetrtek"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/sreda"
        android:text="@string/cetrtek"/>
    <CheckBox 
        android:id="@+id/petek"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/cetrtek"
        android:text="@string/petek"/>
    <CheckBox 
        android:id="@+id/sobota"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/petek"
        android:text="@string/sobota"/>
    <CheckBox 
        android:id="@+id/nedelja"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/sobota"
        android:text="@string/nedelja"/>
    <Button
        android:id="@+id/posljiIzbraneDni"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/posljiIzbraneDni"
        android:onClick="zapolniDni"
        android:layout_below="@id/nedelja"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

Why am I getting the errors?

EDIT:

Ok, I'll specify further. The class that has the source up there calls two layouts. The first layout that is called (R.layout.dnevi) contains the checkboxes (attached xml source). When checking is done and button is clicked, the next method in the class is executed which shows a new layout (R.layout.zapolni). This one doesn't contain any checkboxes but it uses the data (checked/unchecked) from the first layout (R.layout.dnevi). Hope this helps.


Solution

  • Well I found a solution since nothing that I found on web helped. I created separate activities for both layouts and I put isChecked status in Intent with putExtra().