-1

I want to use set and get methods in my android app. In MainActivity.java if i click button i want to set my email variable in UserEmail.java and then get it from PostEmail.java and make toast. But in this way, my toast is empty. If i copy this toast to my MainActivity.java, its working.

Code error is

int java.lang.String.length()' on a null object reference

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public void clickButton(View v) {

        String myEmail = "myemail123@gmail.com";

        UserEmail userEmail = new UserEmail();
        userEmail.setEmail(myEmail);
    }
}

UserEmail.java

public class UserEmail {

    public String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

PostEmail.java

public class PostEmail extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        UserEmail userEmail = new UserEmail();
        Toast.makeText(this,userEmail.getEmail(),Toast.LENGTH_LONG).show();
    }
}
Marcin Koziński
  • 9,758
  • 3
  • 43
  • 58
j22purikas
  • 55
  • 7
  • Can you post the full stack trace? – Zoe Jun 28 '16 at 19:38
  • In the `PostEmail` activity you are just creating a new `UserEmail` object and trying to show a toast of the email property, which is null. You should probably send the `PostEmail` activity your already existing `userEmail` object when you call `startActivity`. (By implementing parcelable in the `UserEmail` class) – Andrew Brooke Jun 28 '16 at 19:40
  • Finally i found right solution i wanted, if i put static before variable in UserEmail, then its working! Thanks for everyone. – j22purikas Jun 29 '16 at 18:33

3 Answers3

0

That's because you are initializing UserEmail object in your PostEmail Activity without setting its email value (like you do in your MainActivity), so his email String is null.

josemgu91
  • 744
  • 4
  • 8
0

You should send the "email" throught an Intent, something like this:

public void clickButton(View v) {

    Context context = v.getContext();
    String myEmail = "myemail123@gmail.com";

    Intent i = new Intent(context, PostEmail.class);
    i.putExtra("EMAIL", myEmail );

    startActivity(i);

}

And at your PostEmail Activity, you should do this:

protected void onCreate(Bundle savedInstanceState) {
    String email = getIntent().getStringExtra("EMAIL");
    Toast.makeText(this,email,Toast.LENGTH_LONG).show();
}

When you do UserEmail userEmail = new UserEmail(); at your onCreate method you are creating a new UserEmail instance, that's why you can't get the email, that you have setted before.

In other words the variable userEmail from MainActivity and the userEmail from PostEmail are not the same instance.

guisantogui
  • 3,608
  • 6
  • 45
  • 83
  • Yep, in this way it is working but if i don't want to startActivity , then i cant use that. – j22purikas Jun 29 '16 at 18:43
  • @j22purikas well you still can use sharedpreferences to share information, check this post: http://stackoverflow.com/questions/23024831/android-shared-preferences-example – guisantogui Jun 29 '16 at 19:36
0

josemgu91 is correct.

But it looks like you want to make UserEmail a field in MainActivity or in a global singleton class instead of in the clickButton method. Then either make that field a public singleton or make a getter that returns the UserEmail var or UserEmail var.getEmail()

or use guisantogui 's suggestion and pass the value using intents, but I would do it in a separate method, not in a cliclListener.

SBC
  • 112
  • 7