0

I need to make a Login Form using Java on Android Studio. And I got issue when register the new user.

RegisterActivity.java

package com.example.asus.loginpage;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    DatabaseHelper helper = new DatabaseHelper(this);
    private EditText name2, email2, uname2, pass12, pass22;
    private String namestr2, emailstr2, unamestr2, pass1str2, pass2str2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

    }
    public void onRegisterClick (View v)
    {
        View mView = getLayoutInflater().inflate(R.layout.activity_register, null);
        if (v.getId()==R.id.breg) {
            System.out.println("pencet");
            name2 = (EditText) mView.findViewById(R.id.etfullname);
            email2 = (EditText) mView.findViewById(R.id.etremail);
            uname2 = (EditText) mView.findViewById(R.id.etruname);
            pass12 = (EditText) mView.findViewById(R.id.etrpassword);
            pass22 = (EditText) mView.findViewById(R.id.etconfpass);

                emailstr2 = email2.getText().toString();
                unamestr2 = uname2.getText().toString();
                pass1str2 = pass12.getText().toString();
                pass2str2 = pass22.getText().toString();

                if (namestr2.length() == 0) {
                    name2.setError("Nama kosong, mohon di isi");
                } else if (emailstr2.length() == 0) {
                    email2.setError("Email kosong, mohon di isi");
                } else if (unamestr2.length() == 0) {
                    uname2.setError("username kosong, mohon di isi");
                } else if (!pass1str2.equals(pass2str2)) {
                    Toast.makeText(RegisterActivity.this, "Password Unmatch", Toast.LENGTH_SHORT).show();
                } else {
                    Contact c = new Contact();
                    c.setName(namestr2);
                    c.setEmail(emailstr2);
                    c.setUname(unamestr2);
                    c.setPass(pass1str2);
                    helper.insertContact(c);
                    finish();
                    Toast.makeText(RegisterActivity.this,

                            "Username & password berhasil dibuat",

                            Toast.LENGTH_SHORT).show();

                }

        }
    }

}

And here's the xml code

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:background="@drawable/background"
    tools:context=".RegisterActivity">

    <TextView
        android:id="@+id/tvreg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="REGISTER"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/etremail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ems="10"
        android:hint="Email"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/etruname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="199dp"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/etfullname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="138dp"
        android:ems="10"
        android:hint="Full Name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/etrpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="205dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/etconfpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="142dp"
        android:ems="10"
        android:hint="Confirm Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/breg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="65dp"
        android:text="Register"
        android:onClick="onRegisterClick"/>

</RelativeLayout>

Here's the logcat

10-07 22:30:20.840 25805-25805/com.example.asus.loginpage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.loginpage, PID: 25805
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6897) 
at android.widget.TextView.performClick(TextView.java:12693) 
at android.view.View$PerformClick.run(View.java:26101) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6897) 
at android.widget.TextView.performClick(TextView.java:12693) 
at android.view.View$PerformClick.run(View.java:26101) 
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

The issue is, if I tap on Register Button, it show error at

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)

Code in line 31 is pass12 = (EditText) mView.findViewById(R.id.etrpassword); I think there's no problem at that syntax. Can anyone assist me solving this?

Kevin M
  • 61
  • 7

3 Answers3

1

You are getting NullPointerException because you are checking length of Null value here

if (namestr2.length() == 0) {
        name2.setError("Nama kosong, mohon di isi");
    }

You forgot to assign value to namestr2

Add this line to your code

namestr2 = name2 .getText().toString();
Bunny
  • 1,044
  • 12
  • 23
0

Do note that NullPointerException is a very common error, one that all developers make, even most experienced ones. A good advice on correcting it is: A) If possible debug your code, go step by step until you find ou the problem. B) Check the exception and understanding when it could happen.

From your question, I see that:

Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at  com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)

Looks like you try to call getText in an object that is not set.

       name2 = (EditText) mView.findViewById(R.id.etfullname);
        email2 = (EditText) mView.findViewById(R.id.etremail);
        uname2 = (EditText) mView.findViewById(R.id.etruname);
        pass12 = (EditText) mView.findViewById(R.id.etrpassword);
        pass22 = (EditText) mView.findViewById(R.id.etconfpass);

I think that some of those variables (name2, email2, uname2, pass12, pass22) is null (the call findViewById return null).

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • I had the login button that have `getText().toString()` in `onClick` method , and it's fine. There's no error like `getText().toString()` in `onRegisterClick` method – Kevin M Oct 08 '18 at 11:44
0

Simplify your onRegisterClickmethod with the below code. Your activity_register already set as Activity layout, so you don't need to inflate it again.

public void onRegisterClick (View v) {
    System.out.println("pencet");
    name2 = (EditText) findViewById(R.id.etfullname);
    email2 = (EditText) findViewById(R.id.etremail);
    uname2 = (EditText) findViewById(R.id.etruname);
    pass12 = (EditText) findViewById(R.id.etrpassword);
    pass22 = (EditText) findViewById(R.id.etconfpass);

    emailstr2 = email2.getText().toString();
    unamestr2 = uname2.getText().toString();
    pass1str2 = pass12.getText().toString();
    pass2str2 = pass22.getText().toString();

    if (namestr2.length() == 0) {
        name2.setError("Nama kosong, mohon di isi");
    } else if (emailstr2.length() == 0) {
        email2.setError("Email kosong, mohon di isi");
    } else if (unamestr2.length() == 0) {
        uname2.setError("username kosong, mohon di isi");
    } else if (!pass1str2.equals(pass2str2)) {
        Toast.makeText(RegisterActivity.this, "Password Unmatch", Toast.LENGTH_SHORT).show();
    } else {
        Contact c = new Contact();
        c.setName(namestr2);
        c.setEmail(emailstr2);
        c.setUname(unamestr2);
        c.setPass(pass1str2);
        helper.insertContact(c);
        finish();
        Toast.makeText(RegisterActivity.this,

                "Username & password berhasil dibuat",

                Toast.LENGTH_SHORT).show();

    }
}
Kunu
  • 5,078
  • 6
  • 33
  • 61
  • i just simplified it like your code, but it still got me a NullPointerExcepition. I had the login button that had `getText().toString()` to and the button is fine – Kevin M Oct 08 '18 at 11:42