The object inside setOnClickListener() is null, but I don't know how to modify it "T"

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

I am a beginner of Android, but I am studying the voting app, but I don't know what to do...

E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.abc, PID: 15185

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abc/com.example.abc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)

        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)

        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)

        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:2066)

        at android.os.Handler.dispatchMessage(Handler.java:106)

        at android.os.Looper.loop(Looper.java:223)

        at android.app.ActivityThread.main(ActivityThread.java:7656)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

        at com.example.abc.MainActivity.onCreate(MainActivity.java:30)

        at android.app.Activity.performCreate(Activity.java:8000)

        at android.app.Activity.performCreate(Activity.java:7984)

        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)

        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 

        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 

        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:2066) 

        at android.os.Handler.dispatchMessage(Handler.java:106) 

        at android.os.Looper.loop(Looper.java:223) 

        at android.app.ActivityThread.main(ActivityThread.java:7656) 

        at java.lang.reflect.Method.invoke(Native Method) 

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Error content.
package com.example.abc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn;
    int vCount[] =new int[6];
    ImageView[] imgV = new ImageView[6];
    int[] imgID = {R.id.img1, R.id.img2, R.id.img3, R.id.img4, R.id.img5, R.id.img6};
    String Name[] = {"Chris", "Brown", "Arab", "Chulsu", "Eunsu", "Kangmin"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android:setTitle ("Leader Election Vote");

        btn = (Button)findViewById(R.id.btn);

        for (int i=0; i<imgID.length; i++) {
            final int index;
            index = i;
            imgV[index] = (ImageView) findViewById(imgID[index]);
            **imgV[index].setOnClickListener(new View.OnClickListener() {**
                @Override
                public void onClick(View view) {
                    vCount[index]++;
                    Toast.makeText(getApplicationContext(),Name[index] + "I voted for you.",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), resultvote.class);
                intent.putExtra("vCount", vCount);
                intent.putExtra("Name", Name);
                startActivity(intent);
            }
        });
    }

android

2022-09-20 11:40

1 Answers

The error message is Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on null

imgV[index].setOnClickListener(), so imgV[index] is null somewhere. It comes from findViewById, so maybe there is something wrong with the resource name.


2022-09-20 11:40

If you have any answers or tips


© 2022 pinfo. All rights reserved.