1
votes
String nametxt;
String bloodtxt;
String agetxt;
String mobiletxt;
Button register;
EditText name;
EditText age;
EditText blood;
EditText mobile;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    name = (EditText) findViewById(R.id.name);
    age = (EditText) findViewById(R.id.age);
    blood = (EditText)findViewById(R.id.blood);
    age = (EditText)findViewById(R.id.age);
    register =(Button)findViewById(R.id.register);
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nametxt = name.getText().toString();
            agetxt = age.getText().toString();
            bloodtxt = blood.getText().toString();//logcat pointing exception here//
            mobiletxt = mobile.getText().toString();
            if (nametxt.equals("") && agetxt.equals("")) {
                Toast.makeText(getApplicationContext(),
                        "Please complete the sign up form",
                        Toast.LENGTH_LONG).show();
            }
            else
            {
                ParseUser user = new ParseUser();
                user.setUsername(nametxt);
                user.setPassword(agetxt);
                user.put("blood", bloodtxt);
                user.put("Mobile",mobiletxt);
                user.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "Succesfully registered", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "hey %d you are already registered in blood database.." + nametxt, Toast.LENGTH_LONG).show();


                        }
                    }
                });
            }

        }
    });

}

I'm getting this error:

Process: com.blooddata.lemuriadigitallab.blooddata, PID: 26845 java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.blooddata.lemuriadigitallab.blooddata.Register$1.onClick(Register.java:43) at android.view.View.performClick(View.java:5181) at android.view.View$PerformClick.run(View.java:20887) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

2
which line are you getting the error?One of your edittext field seems to be null.Either the id is wrong or it doesn't exist - Renjith

2 Answers

3
votes

Initialize "mobile" EditText variable.

The NullPointerException is raised from here:

  mobile.getText().toString();
1
votes

You are initializing "age" twice:

name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
blood = (EditText)findViewById(R.id.blood);
age = (EditText)findViewById(R.id.age);

So here:

mobiletxt = mobile.getText().toString();

You get a null pointer

Instead, one of the 2 should be "mobile"

name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
blood = (EditText)findViewById(R.id.blood);
mobile = (EditText)findViewById(R.id.mobile);