Search code examples
androidnullpointerexceptioninvokemobile-applicationvirtual-functions

Attempt to invoke virtual method on a null object reference virtual method on a null object reference


have some issue when i run this code on android studio app got crash.

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class FirstScreen extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        TextView textView =  findViewById(R.id.newScreen);
        String value ;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen);
        Intent intent = getIntent();
        value=intent.getStringExtra("Sending");
        textView.setText(value);
    }
}

enter image description here


Solution

  • TextView textView = findViewById(R.id.newScreen); should be after setContentView(R.layout.activity_first_screen);