lundi 14 mai 2018

Android Checkbox error "on a null object reference"

I tried to make a checkbox that when it is checked it will strikethrough text field elements in the listview. I'm pretty sure that I did everything correctly including set up the ID for the checkbox in XML and declared it on the class that I wanna use but I still get the Null object error.

I have looked through all the question and answer all over this topic, I also tried them all but I still get an error. Could someone please help me identify where an error might be.

Thank you so much in advance for any help =)

Class file

package com.puyakul.prin.psychic_shopping;

import...

public class MainListView extends AppCompatActivity {

    //================DATABASE=====================//
    private ShoppingListDatabase databaseConnection;
    private SQLiteDatabase db;


    public ListView ListView_mainListView;
    public ArrayList<ShoppingList> lists;
    public AppCompatCheckBox checkBox_doneCheck;
    public TextView textView_listname;

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


        //================DATABASE Connection================//
        databaseConnection = new ShoppingListDatabase(this);
        db = databaseConnection.open();

        //important!!!
        setContentView(R.layout.activity_main_list_view);
        getLists();

    }

    public void getLists(){

      ....

        checkBox_doneCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (checkBox_doneCheck.isChecked()) {
                    textView_listname.setPaintFlags(textView_listname.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                }
                else{

                    textView_listname.setPaintFlags(textView_listname.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
        });
    }

XML file

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/checkBox_doneCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:checked="false"/>

Error log

Process: com.puyakul.prin.psychic_shopping, PID: 19958
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.puyakul.prin.psychic_shopping/com.puyakul.prin.psychic_shopping.MainListView}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatCheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatCheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
        at com.puyakul.prin.psychic_shopping.MainListView.getLists(MainListView.java:107)
        at com.puyakul.prin.psychic_shopping.MainListView.onCreate(MainListView.java:67)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 




Aucun commentaire:

Enregistrer un commentaire