简体   繁体   中英

App crash with FATAL EXCEPTION with NullPointerException when I click go this activity

I created an android studio project but I found null pointer exception error and all of these error again and again when I click a button that links activity while it didn't happen to other activity who have similar code. even I try to change something, it always shows the same error in logcat. I quite beginner in android development

Here is my code

package com.example.chemicalculators;

import static java.lang.Integer.*;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.atomic.AtomicReference;

public class CalCal5 extends AppCompatActivity {
    int eNo;
    String eSym;
    String eName;
    double eMass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AtomicReference<EditText> eNoString = new AtomicReference<>((EditText) findViewById(R.id.editTextNumber));
        ImageButton searchButton = (ImageButton) findViewById(R.id.imageButton);
        Button button = (Button) findViewById(R.id.button);
        final TextView outESymbol = (TextView) findViewById(R.id.elementSymbol);
        final TextView outEName = (TextView) findViewById(R.id.elementName);
        final TextView outEMass = (TextView) findViewById(R.id.AtomicMassValue);

        searchButton.setOnClickListener(view -> {
            eNo = parseInt(eNoString.get().getText().toString());
            switch(eNo)
            {
                case 1:
                    eSym = "H";
                    eName = "Hydrogen";
                    eMass = 1.00797;
                case 2:
                    eSym = "He";
                    eName = "Helium";
                    eMass = 4.0026;
                case 3:
                    eSym = "Li";
                    eName = "Lithium";
                    eMass = 6.941;
                case 4:
                    eSym = "Be";
                    eName = "Beryllium";
                    eMass = 9.0122;
                case 5:
                    eSym = "B";
                    eName = "Boron";
                    eMass = 10.811;
                case 6:
                    eSym = "C";
                    eName = "Carbon";
                    eMass = 12.0107;
                
                default :
                    eSym = "-";
                    eName = "None";
                    eMass = 0.00;
            }

            outESymbol.setText(eSym);
            outEName.setText(eName);
            outEMass.setText(String.valueOf(eMass));

            Toast.makeText(CalCal5.this, "Finding Success", Toast.LENGTH_SHORT).show();
        });

        button.setOnClickListener(view -> {
            Intent p = new Intent(CalCal5.this, MainActivity.class);
            startActivity(p);
        });
    }
}

And this is my Logcat

2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:25.414 6477-6477/com.example.chemicalculators E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chemicalculators, PID: 6477
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chemicalculators/com.example.chemicalculators.CalCal5}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.chemicalculators.CalCal5.onCreate(CalCal5.java:33)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

You are not calling setContentView() to inflate layout

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.YOUR_LAYOUT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM