Error Creating global CheckBox and using it in a method

57 Views Asked by At

I am trying to make that CheckBox object global in order not to creat it's findViewByid in methodes many times and it gives FC in the app and in logcat says that the line of creating the CheckBox is the line having the error

and here is it's class code

public class AdvJustJava_app extends AppCompatActivity {

public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adv_just_java_app);
    Button DecBtn = (Button) findViewById(R.id.adv_dec_btn);
    DecBtn.setEnabled(false);
}

int Q=0, P=0, WC=0, C=0;


private void adv_displayQuantity(int numberOfCoffees) {
    TextView quantityTextView = (TextView) findViewById(R.id.adv_quantity_text_view);
    quantityTextView.setText("" + numberOfCoffees);
}

private void adv_displayOrderSummary(String message) {
    TextView quantityTextView = (TextView) findViewById(R.id.adv_order_summary_text_view);
    TextView ThanksTextView = (TextView) findViewById(R.id.adv_thank_you_tv);
    ThanksTextView.setText("Thank You ;)");

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ThanksTextView.getLayoutParams();
    params.setMargins(0, 4, 0, 4    ); //substitute parameters for left, top, right, bottom
    ThanksTextView.setLayoutParams(params);

    quantityTextView.setText(message);
}

public void adv_OrderSummary(View view) {
    if(Q==0) {
        if(!checkBoxWC.isChecked()){WC = 0;}
        if(!checkBoxC.isChecked()){C = 0;}
        P= WC + C ;
        adv_displayOrderSummary("Free :D");
        Toast toast = Toast.makeText(this, "Your Ordered Quantity is Zero", Toast.LENGTH_LONG);
        toast.show();
    }else if(!checkBoxWC.isChecked()&& !checkBoxC.isChecked() ){
            adv_displayOrderSummary("Free :D");
            Toast toast = Toast.makeText(this, "You Did Not Select the Product", Toast.LENGTH_LONG);
            toast.show();
    }else {
        EditText editText = (EditText)findViewById(R.id.nameET);
        String name = (String) editText.getText().toString();
        String adv_Price = NumberFormat.getCurrencyInstance().format(P);
        adv_displayOrderSummary("Name: " + name + "\nQuantity: " + Q + "\nTotal: " + adv_Price);
    }
 }
}

and that is the Logcat

02-12 19:50:53.225 17044-17044/courses.omy.dasser.androidcousres E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: courses.omy.dasser.androidcousres, PID: 17044
                                                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{courses.omy.dasser.androidcousres/courses.omy.dasser.androidcousres.AdvJustJava_app}: java.lang.NullPointerException
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                   at android.os.Looper.loop(Looper.java:193)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.NullPointerException
                                                                                   at android.app.Activity.findViewById(Activity.java:1903)
                                                                                   at courses.omy.dasser.androidcousres.AdvJustJava_app.<init>(AdvJustJava_app.java:21)
                                                                                   at java.lang.Class.newInstanceImpl(Native Method)
                                                                                   at java.lang.Class.newInstance(Class.java:1215)
                                                                                   at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2276)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                   at android.os.Looper.loop(Looper.java:193) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 
2

There are 2 best solutions below

1
On BEST ANSWER

You cannot call findViewById() until after setContentView(). Replace:

public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

with:

public CheckBox checkBoxWC;
public CheckBox checkBoxC;

and add these lines after your setContentView(R.layout.adv_just_java_app) call in onCreate():

checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);
0
On

You should not be doing this in

public final CheckBox checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
public final CheckBox checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);

Instead do this ->

public CheckBox checkBoxWC;
public CheckBox checkBoxC;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adv_just_java_app);
    checkBoxWC = (CheckBox)findViewById(R.id.WCcheckBox);
    checkBoxC = (CheckBox)findViewById(R.id.CcheckBox);
    Button DecBtn = (Button) findViewById(R.id.adv_dec_btn);
    DecBtn.setEnabled(false);
}