0

While running this code error is coming in the logact section.I guess its minor error so its difficult for me to search the error.I tried various things but it didn't worked for me.I don't know where my code went wrong.

this is my NewUserActivity .java code

public class NewUserActivity extends AppCompatActivity {


    EditText name;
    EditText email;
    EditText phone;
    EditText usname;
    EditText passsword;
    Button register;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_user_login);


        name = (EditText) findViewById(R.id.name);
        email = (EditText) findViewById(R.id.email);
        phone = (EditText) findViewById(R.id.phone);
        usname = (EditText) findViewById(R.id.usname);
        passsword = (EditText) findViewById(R.id.passsword);
        register= (Button) findViewById(R.id.textView2);

        
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String NAME = name.getText().toString().trim();
                String EMAIL = email.getText().toString().trim();
                String PHONENO =phone.getText().toString().trim();
                String username = usname.getText().toString().trim();
                String password = passsword.getText().toString().trim();
                if (NAME.isEmpty())
                    Toast.makeText(getApplicationContext(),"Please enter name",Toast.LENGTH_SHORT).show();
                else if(EMAIL.isEmpty())
                    Toast.makeText(getApplicationContext(),"Please enter correct email address",Toast.LENGTH_SHORT).show();
                else if(PHONENO.isEmpty())
                    Toast.makeText(getApplicationContext(),"Please enter correct phone number",Toast.LENGTH_SHORT).show();
                else if(username.isEmpty())
                    Toast.makeText(getApplicationContext(),"Please enter Username",Toast.LENGTH_SHORT).show();
                else if(username.length()<8|| username.length()>15)
                    Toast.makeText(getApplicationContext(),"Username must contain  atleast 8  characters",Toast.LENGTH_SHORT).show();
                else if(password.isEmpty())
                    Toast.makeText(getApplicationContext(),"Please enter Password",Toast.LENGTH_SHORT).show();
                else if(password.length()<6|| password.length()>10)
                    Toast.makeText(getApplicationContext(),"Password must contain 6  characters",Toast.LENGTH_SHORT).show();
                else {
                }
            }
        });


    }


}

this is my xnl file for the same.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    tools:context=".NewUserActivity"
    android:background="@drawable/ic_launcher_background"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="276dp"
        android:layout_height="38dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="106dp"
        android:layout_marginBottom="581dp"
        android:text="REGISTRATION "
        android:textColor="@color/WhiteSmoke"
        android:textSize="30dp" />

    <EditText
        android:layout_height="26dp"
        android:layout_width="297dp"
        android:layout_marginBottom="487dp"
        android:layout_marginEnd="65dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/name"/>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="218dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="135dp"
        android:layout_marginBottom="530dp"
        android:drawableLeft="@drawable/ic_action_name"
        android:text="NAME"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="195dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="161dp"
        android:layout_marginBottom="439dp"
        android:drawableLeft="@drawable/ic_action_mail"
        android:text="EMAIL ID"
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:layout_height="26dp"
        android:layout_width="297dp"
        android:layout_marginBottom="399dp"
        android:layout_marginEnd="67dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/email"/>

    <TextView
        android:id="@+id/textView7"
        android:layout_width="147dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="209dp"
        android:layout_marginBottom="351dp"
        android:drawableLeft="@drawable/ic_action_phone"
        android:text="PHONE NO."
        android:textColor="@color/WhiteSmoke"
        android:textSize="20dp" />

    <EditText
        android:layout_width="297dp"
        android:layout_height="26dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="63dp"
        android:layout_marginBottom="306dp"
        android:id="@+id/phone"/>

    <TextView
        android:layout_height="30dp"
        android:layout_width="195dp"
        android:textSize="20dp"
        android:textColor="@color/WhiteSmoke"
        android:text="USERNAME"
        android:layout_marginBottom="256dp"
        android:layout_marginEnd="160dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/textView8"
        android:drawableLeft="@drawable/ic_action_username"/>

    <EditText
        android:layout_height="26dp"
        android:layout_width="297dp"
        android:layout_marginBottom="205dp"
        android:layout_marginEnd="63dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/usname"/>

    <TextView android:layout_height="30dp" android:layout_width="195dp"
        android:textSize="20dp" android:textColor="@color/WhiteSmoke"
        android:text="PASSWORD" android:layout_marginBottom="160dp"
        android:layout_marginEnd="156dp" android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" android:id="@+id/textView9"
        android:drawableLeft="@drawable/ic_action_password"/>


    <EditText
        android:layout_width="297dp"
        android:layout_height="26dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="62dp"
        android:layout_marginBottom="113dp"
        android:id="@+id/passsword"/>

    <androidx.cardview.widget.CardView
    android:layout_height="56dp" android:layout_width="348dp"
    android:layout_marginBottom="40dp" android:layout_marginEnd="20dp"
    android:layout_alignParentBottom="true" android:layout_alignParentEnd="true"
    app:cardElevation="10dp" app:cardCornerRadius="30dp"
    app:cardBackgroundColor="@color/DeepPink" android:layout_marginRight="28dp"
    android:layout_marginStart="20dp"
        android:layout_alignParentRight="true"
    android:layout_alignParentStart="true">


    <RelativeLayout android:layout_height="73dp" android:layout_width="373dp"
    android:layout_marginBottom="100dp" android:layout_marginEnd="50dp">

        <Button
            android:id="@+id/register"
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="129dp"
            android:layout_marginBottom="17dp"
            android:fontFamily="sans-serif"
            android:freezesText="true"
            android:text="REGISTER"
            android:background="@android:color/transparent"
            android:textColor="@color/Black"
            android:textSize="24dp" />

    </RelativeLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

There is error in this section of my code.

register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String NAME = name.getText().toString().trim();
                String EMAIL = email.getText().toString().trim();
                String PHONENO =phone.getText().toString().trim();
                String username = usname.getText().toString().trim();
                String password = passsword.getText().toString().trim();
sank
  • 133
  • 7
  • 1
    what is the error you getting? – Wahdat Jan Nov 21 '20 at 13:17
  • can you show your logs what error you are facing – Amit pandey Nov 21 '20 at 13:18
  • Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.moviebookingapp2.NewUserActivity.onCreate(NewUserActivity.java:34) – sank Nov 21 '20 at 13:19

2 Answers2

5

You are using Wrong id of button which will led to the null pointer exception Change this

register= (Button) findViewById(R.id.textView2);

to

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

To know about null pointer exception check this What is a NullPointerException, and how do I fix it?

You can use View binding to replaces all findviewbyId https://developer.android.com/topic/libraries/view-binding

Wahdat Jan
  • 3,988
  • 3
  • 21
  • 46
1
  name = (EditText) findViewById(R.id.name);
    email = (EditText) findViewById(R.id.email);
    phone = (EditText) findViewById(R.id.phone);
    usname = (EditText) findViewById(R.id.usname);
    passsword = (EditText) findViewById(R.id.passsword);
    register= (Button) findViewById(R.id.textView2);

here check the register button id you add textView2 id here your mistake

Amit pandey
  • 1,149
  • 1
  • 4
  • 15
  • and one more thing next time when ever you set a id please right like this : tvName,etUserName,or btnRegister like this so its make eassy to know what it is or ike et_name,tv_name – Amit pandey Nov 21 '20 at 15:13