Question

I'm getting a nullpointerexception when I click on the signup button on the Login class it gives that error. I've tried to tinker around with the local and global variables and nothing seems to fix the error, I might have got mixed up on the onClickListener as when I remove that code it seems to work.

Signup.class.

public class Signup extends Activity {

    /*List Variables */ 
    private ScrollView scrollView1;

    private EditText editTextUN;

    private EditText editTextPW1;

    private EditText editTextPW2;

    private EditText editTextEMAIL;

    private Button buttonSignup;

    private Button buttonCancelSignup;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        /*set ContentView */
        setContentView(R.layout.signup);

        /*Populate fields */
        populate();


    }

    private void populate() {
        // TODO Auto-generated method stub

        /*initialize the ScrollView */
        ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);

        /*initialize the EditText for the FIRST username field */
        final EditText editTextUN = (EditText) findViewById(R.id.editTextUN);

        /*initialize the FIRST password field */
        final EditText editTextPW1 = (EditText) findViewById(R.id.editTextPW1);

        /*initialize the SECOND password field */
        final EditText editTextPW2 = (EditText) findViewById(R.id.editTextPW2);

        /*initialize the signup Button */
        Button buttonSignup = (Button) findViewById(R.id.btsignup);

        /*initialize the cancel Button */
        Button buttonCancelSignup = (Button) findViewById(R.id.buttonCancelSignup);

        /*initialize the email EditText */
        final EditText editTextEMAIL = (EditText) findViewById(R.id.editTextEMAIL);

        /*setup OnClickListener */
        buttonSignup.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                /*check to see if it's the signup button */
                if(v.getId()== R.id.buttonSignup) {

                    /*get everything to a string */
                    String user = editTextUN.getText().toString();

                    String pass1 = editTextPW1.getText().toString();

                    String pass2 = editTextPW2.getText().toString();

                    String email = editTextEMAIL.getText().toString();


                    /*check to see if there's Text in all the Edit Text's */
                    if(user !=null && user.length()>5 && pass1.length()>5 && pass1 !=null && pass2.length()>5 && pass2 !=null && email.length()>5 && email !=null) {

                        /*send to comHelper */
                        ComHelper.sendSignUp(user, pass1, pass2, email);

                        while(ComHelper.sendSignUp(null, null, null, null)) {

                        }


                    }


                } else {
                    finish();
                }


            }
        });
    }





}

LogCat:

06-06 17:15:30.045: E/AndroidRuntime(1197): FATAL EXCEPTION: main
06-06 17:15:30.045: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.qwuik/com.gta5news.qwuik.Signup}: java.lang.NullPointerException
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.os.Looper.loop(Looper.java:137)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invoke(Method.java:511)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at dalvik.system.NativeStart.main(Native Method)
06-06 17:15:30.045: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
06-06 17:15:30.045: E/AndroidRuntime(1197):     at com.gta5news.qwuik.Signup.populate(Signup.java:70)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at com.gta5news.qwuik.Signup.onCreate(Signup.java:40)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.Activity.performCreate(Activity.java:4465)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 17:15:30.045: E/AndroidRuntime(1197):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

EDIT: This is on the x86 emulator.

Was it helpful?

Solution

all the above statements are right but I think NullPointerException is due to :

as in Button buttonSignup = (Button) findViewById(R.id.btsignup); right id is R.id.buttonSignup

so looks buttonSignup is null there

as you used at

........................................

/*check to see if it's the signup button */

            if(v.getId()== `R.id.buttonSignup`) {

...........................................

OTHER TIPS

I have updated your code and added comments, now it's working fine with me

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;

public class Signup extends Activity {

    /*List Variables */ 
    private ScrollView scrollView1;
    private EditText editTextUN;
    private EditText editTextPW1;
    private EditText editTextPW2;
    private EditText editTextEMAIL;
    private Button buttonSignup;
    private Button buttonCancelSignup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        /*Populate fields */
        populate();
      }

    private void populate() {


        /*initialize the ScrollView */
        ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
        /*initialize the EditText for the FIRST username field */
        final EditText editTextUN = (EditText) findViewById(R.id.editTextUN);
        /*initialize the FIRST password field */
        final EditText editTextPW1 = (EditText) findViewById(R.id.editTextPW1);
        /*initialize the SECOND password field */
        final EditText editTextPW2 = (EditText) findViewById(R.id.editTextPW2);
        /*initialize the signup Button */

        /* here.........i have added final .........*/
       final  Button buttonSignup = (Button) findViewById(R.id.btsignup);
        /*initialize the cancel Button */
        Button buttonCancelSignup = (Button) findViewById(R.id.buttonCancelSignup);
        /*initialize the email EditText */
        final EditText editTextEMAIL = (EditText) findViewById(R.id.editTextEMAIL);

        /*setup OnClickListener */
        buttonSignup.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                /*check to see if it's the signup button */
                //if(v.getId()== R.id.btsignup) {

                /* here.........i have added if condition .........*/
                    if(v==buttonSignup){
                        Toast.makeText(getApplicationContext(), "button has been pressed", Toast.LENGTH_SHORT).show();


                    /*get everything to a string */
                    String user = editTextUN.getText().toString();
                    String pass1 = editTextPW1.getText().toString();
                    String pass2 = editTextPW2.getText().toString();
                    String email = editTextEMAIL.getText().toString();


                    /*check to see if there's Text in all the Edit Text's */
                    if(user !=null && user.length()>5 && pass1.length()>5 && pass1 !=null && pass2.length()>5 && pass2 !=null && email.length()>5 && email !=null) {

                   }
                } 
                else 
                {
                    finish();
                }
            }
        });
    }
}

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top