0

This is my code and i dont see why im getting an error in the logcat of null pointer exception. please help. Cant seem to figure out why it does that because im not getting any errors in the build. The app would run then it would just stop working randomly

     package com.tonmulnahil.asapgo;

        import androidx.appcompat.app.AppCompatActivity;

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

        public class Login extends AppCompatActivity {

        Button callSignUp;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.
            LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_login);

            callSignUp = findViewById(R.id.signup_screen);

            callSignUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Login.this, SignUp.class);
                    startActivity(intent);
                }
            });


          }
        }

This is my xml code.

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Login"
    android:orientation="vertical"
    android:background="#fff"
    android:padding="20dp"
    >

    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:transitionName="logo_image"
        android:src="@drawable/splash_screen_background"/>

    <TextView
        android:id="@+id/logo_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/chewy"
        android:text="Hello There, Welcome Back"
        android:textColor="#64B5F6"
        android:textSize="40sp"
        android:transitionName="logo_text" />

    <TextView
        android:id="@+id/slogan_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/bubblegum_sans"
        android:text="Sign In To Continue"
        android:textColor="#000000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:orientation="vertical">

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/username"
            android:hint="Username"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHighlight="@color/black" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/password"
            android:hint="Password"
            app:passwordToggleEnabled="true"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"/>

        </com.google.android.material.textfield.TextInputLayout>

        <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_margin="5dp"
            android:background="#00000000"
            android:elevation="0dp"
            android:text="Forget Password?"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="GO"
            android:background="#000"
            android:textColor="#fff"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            android:text="New User? SIGN UP"
            android:layout_gravity="right"
            android:elevation="0dp"
            android:layout_margin="5dp"
            android:textColor="#000"
            android:fontFamily="@font/bubblegum_sans"/>

     </LinearLayout>
     </LinearLayout>

This is the message im getting in the logcat.

     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.tonmulnahil.asapgo.Login.onCreate(Login.java:23)
  • 1
    add id to your button – Shahriyar Aghajani Jun 15 '20 at 05:51
  • @Shahriyar Aghajani is right, what is happening is you are `findViewById` is searching the xml for a button with the id `signup_screen` which is not there, so it returns null. adding and id to the button you want to add the listener to should fix this – Mark Gilchrist Jun 15 '20 at 07:18

0 Answers0