-1

I'm studying Android java, and I have this error and can't run it. Could you help me fixing this error? What is the problem?

Is it related to the error that I can't make a onClickListener() lambda expression? It gives me error and I can't import. So I tried making new View.OnclickListener() and its color is grey.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 9974
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:8051)
        at android.app.Activity.performCreate(Activity.java:8031)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7839) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
I/Process: Sending signal. PID: 9974 SIG: 9

And this is my mainActivity.

package com.example.myapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    TextView txtViewResults;
    EditText editTextInputWt;
    Button btnConvertWt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setLogo(R.mipmap.ic_launcher_weight_round);
        actionBar.setTitle(R.string.txtTitle);

        btnConvertWt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (radioGroup.getCheckedRadioButtonId() == -1){
                    Toast.makeText(MainActivity.this, "Please select conversion type", Toast.LENGTH_SHORT).show();
                } else if (editTextInputWt.getText().toString().isEmpty()){
                    Toast.makeText(MainActivity.this, "Please select baggage weight", Toast.LENGTH_SHORT).show();
                } else {
                    double inputWt, outputWt;
                    try{
                        inputWt = Double.parseDouble(editTextInputWt.getText().toString());
                        if (inputWt<0){
                            Toast.makeText(MainActivity.this, "Baggage weight can't be negative.", Toast.LENGTH_SHORT).show();
                        } else if (radioGroup.getCheckedRadioButtonId() == R.id.radBtnKgsToLbs){
                            if (inputWt > 500){
                                Toast.makeText(MainActivity.this, "Input bg wt can't be greater than 500Kilos", Toast.LENGTH_SHORT).show();
                            } else {
                                outputWt = inputWt*2.2;
                                txtViewResults.setText(String.format("Converted wt: %.2f Lbs", outputWt));
                            }
                        } else if (radioGroup.getCheckedRadioButtonId() == R.id.radBtnLbsToKgs){
                            if (inputWt > 1000){
                                Toast.makeText(MainActivity.this, "Input bg wt can't be greater than 1000 pounds", Toast.LENGTH_SHORT).show();
                            } else {
                                outputWt = inputWt/2.2;
                                txtViewResults.setText(String.format("Converted wt: %.2f Kgs", outputWt));
                            }
                        }
                    } catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        });
    }
}



QPAO
  • 21
  • 3

1 Answers1

-1

This is due to you haven't initialised your views and as of this stage they are null and you can't invoke method on null object in java which you can see in your error.

so for this you have to initalise your variable you can do like following

as you have used layout file you can do findViewById() as following

public class MainActivity extends AppCompatActivity {
    RadioGroup radioGroup;
    TextView txtViewResults;
    EditText editTextInputWt;
    Button btnConvertWt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ....    
        radioGroup = findViewById(R.id.my_radio_group);
        btnConvertWt = findViewById(R.id.my_button); // the id's are same as defined in your layout file
        ....

        btnConvertWt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ....
            }
        });
    }
}
Bhavin
  • 577
  • 6
  • 11