Android

Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getUsername()' on a null object reference

I am trying to get current user data using parse, every time i run this activity I get this message Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getUsername()' on a null object reference

public class Setting extends Activity {

    Button finishButton;
    String usernametxt;

    String FirstNametxt;
    String LastNametxt;
    String Emailtxt;
    String FFirstNametxt;
    String FLastNametxt;
    String FEmailtxt;
    String FMobileNumbertxt;

    EditText username;
    EditText FirstName;
    EditText LastName;
    EditText Email;
    EditText FFirstName;
    EditText FLastName;
    EditText FEmail;
    EditText FMobileNumber;

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

        ParseUser currentUser = ParseUser.getCurrentUser();
        usernametxt = currentUser.getUsername();
        Emailtxt = currentUser.getEmail();
        FirstNametxt = currentUser.get("FirstName").toString();
        LastNametxt = currentUser.get("LastName").toString();
        FFirstNametxt = currentUser.get("FFirstName").toString();
        FLastNametxt = currentUser.get("FLastName").toString();
        FEmailtxt = currentUser.get("FEmail").toString();
        FMobileNumbertxt = currentUser.get("FMobileNumber").toString();

        username = (EditText) findViewById(R.id.Username);
        FirstName = (EditText) findViewById(R.id.FirstName);
        LastName = (EditText) findViewById(R.id.LastName);
        Email = (EditText) findViewById(R.id.Email);
        FFirstName = (EditText) findViewById(R.id.FFirstName);
        FLastName = (EditText) findViewById(R.id.FLastName);
        FEmail = (EditText) findViewById(R.id.FEmail);
        FMobileNumber = (EditText) findViewById(R.id.FMobileNumber);

        username.setText(usernametxt);
        Email.setText(Emailtxt);
        FirstName.setText(FirstNametxt);
        LastName.setText(LastNametxt);
        FFirstName.setText(FFirstNametxt);
        FLastName.setText(FLastNametxt);
        FEmail.setText(FEmailtxt);
        FMobileNumber.setText(FMobileNumbertxt);

        finishButton= (Button)findViewById(R.id.finishbtn);

        finishButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                usernametxt = username.getText().toString();
                FirstNametxt = FirstName.getText().toString();
                LastNametxt = LastName.getText().toString();
                Emailtxt = Email.getText().toString();
                FFirstNametxt = FFirstName.getText().toString();
                FLastNametxt = FLastName.getText().toString();
                FEmailtxt = FEmail.getText().toString();
                FMobileNumbertxt = FMobileNumber.getText().toString();

                ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> objects, ParseException e) {
                        if (usernametxt.equals("")  &&
                                LastNametxt.equals("") && Emailtxt.equals("")  &&
                                FirstNametxt.equals("")&& FFirstNametxt.equals("")  &&
                                FLastNametxt.equals("") && FEmailtxt.equals("")  &&
                                FMobileNumbertxt.equals("")) {

                            AlertDialog.Builder builder = new AlertDialog.Builder(Setting.this);

                            AlertDialog dialog = builder.create();
                            dialog.show();

                        } else {

                            //Update user
                            ParseUser user = ParseUser.getCurrentUser();
                            user.put("username", usernametxt);
                            user.put("FirstName", FirstNametxt);
                            user.put("LastName", LastNametxt);
                            user.put("Email", Emailtxt);

                            user.put("FFirstName", FFirstNametxt);
                            user.put("FLastName", FLastNametxt);
                            user.put("FEmail", FEmailtxt);
                            user.put("FMobileNumber", FMobileNumbertxt);

                            user.saveInBackground(new SaveCallback() {

                                @Override
                                public void done(ParseException e) {

                                    if (e == null) {
                                        Intent intent = new Intent(
                                                Setting.this,
                                                MainMenu.class);

                                        Toast.makeText(getApplicationContext(),
                                                "Updated!!",
                                                Toast.LENGTH_SHORT).show();
                                        startActivity(intent);

                                    } else {
                                        Toast.makeText(getApplicationContext(),
                                                "NOT Updated!!", Toast.LENGTH_SHORT)
                                                .show();
                                    }
                                }

                            });

                        }
                    }
                });
            }

        });
    }
}

Logcat

07-26 01:11:38.681 1513-1513/com.falldetection.android.falldetection E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.falldetection.android.falldetection, PID: 1513 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.falldetection.android.falldetection/com.falldetection.android.falldetection.Setting}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getUsername()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218) at android.app.ActivityThread.access$1000(ActivityThread.java:198) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6837) 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:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getUsername()' on a null object reference at com.falldetection.android.falldetection.Setting.onCreate(Setting.java:58) at android.app.Activity.performCreate(Activity.java:6500) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)             at android.app.ActivityThread.access$1000(ActivityThread.java:198)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:145)             at android.app.ActivityThread.main(ActivityThread.java:6837)             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:1404)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 55,916 Points

Hi,

It is difficult to see where the problem may lie - I suggest you upload this into Github or something similar so we can repicate the error. Just posting a huge block of code with no commentary about what you have tried, where you have put breakpoints, what you have proved works etc, makes this question hard to answer.

From what I can see, you are getting a Null Pointer when you call getUsername(). That seems to be the first line:

usernametxt = currentUser.getUsername();

This means that currentUser contains nothing. That might imply that:

ParseUser currentUser = ParseUser.getCurrentUser();

failed to bring anything back. What can you see in your back end? What tests have you done to check that the back end is being updated and/or accessed? Have you tried adding breakpoints anywhere to see what is bringing a Null back? Does your code reasonably expect a current user within the onCreate method? Why is that? I can't see where a user must exist; how does that work the first time the application is run? How would a user exist in that scenario? I assume that the activity never starts so the error is likely to be in onCreate?

Sorry for the list of questions but without htere being a glaring error in the code (which I haven't checked due to the number of lines posted) the questions will help narrow down the problem you are facing.

Steve.

thanks Steve, the problem i am facing is with currentuser, I have tried to delete getUsername line but still showing that getEmail is on null reference, I had to delete below 9 lines to run activity. I have tried to drop columns and rows from user class in parse I thought it may work but still showing null reference on my logcat. this code was working perfectly before adding FFirstName, FLastName, FEmail and FMobileNumber

(```)

ParseUser currentUser = ParseUser.getCurrentUser();

    usernametxt = currentUser.getUsername();

    Emailtxt = currentUser.getEmail();

    FirstNametxt = currentUser.get("FirstName").toString();

    LastNametxt = currentUser.get("LastName").toString();

    FFirstNametxt = currentUser.get("FFirstName").toString();

    FLastNametxt = currentUser.get("FLastName").toString();

    FEmailtxt = currentUser.get("FEmail").toString();

    FMobileNumbertxt = currentUser.get("FMobileNumber").toString();

(```)