0

I am writing an app using Java and I can't understand the following thing: I have this listener:

public class SignUpActivity extends AppCompatActivity {
    private Button datePickerButton;
    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private String mydata;
    private TextView myDataShow;

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

        datePickerButton = findViewById(R.id.signup_date_picker);
        myDataShow = findViewById(R.id.signup_date_show)

        datePickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        SignUpActivity.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);

                String date = month + "/" + day + "/" + year;
                myDataShow.setText(date); /** myDataShow is considered null **/
                mydata = date; /** it works*/
                Log.d(TAG, "aaaa onDateSet: mm/dd/yyy: " + data);
            }
        };

    }

the problem is that myDataShow is null in my onDateSet method, and i don't understand this, I think it is a scope problem.

Moveover if i try to assign the string date to another string variable declared in onCreate method it works, why I have two different behaviours?

Thanks

ror
  • 2,847
  • 1
  • 17
  • 25
Bob Rob
  • 144
  • 9
  • 1
    Yes it is a scope problem try to make myDataShow as global and check – Jignesh Mayani Feb 21 '20 at 13:25
  • 1
    Is `myDataShow` an instance field? A local variable? (This confusion part of why I **always** use `this.` when accessing instance members.) If it's an instance member, no, it's not a scope problem. If it's a local in `onCreate` **and** your code above is in `onCreate`, it's not a scope problem. Otherwise, maybe it is. But I suspect a timing problem. – T.J. Crowder Feb 21 '20 at 13:27
  • 3
    I suggest going through the steps in [mcve] to narrow in on the problem. You'll likely figure it out, and if you don't, you'll have a **complete** but minimal (1-50 lines) example of the problem you can share and others can copy and run locally. – T.J. Crowder Feb 21 '20 at 13:29
  • I have edited the question, maybe now it is better – Bob Rob Feb 21 '20 at 13:36
  • You are missing a semicolon in `myDataShow = findViewById(R.id.signup_date_show)`. No real scope issue here I think - is it possible that `findViewById()` actually returns `null`? – Raven221221221 Feb 21 '20 at 13:54
  • findViewById(R.id.signup_date_show) can't return null because the element exists... What do you mean? – Bob Rob Feb 21 '20 at 14:05
  • 1
    show us the xml file `signup_date_show` – Blundell Feb 21 '20 at 14:07
  • "Can't return null" - as in you wrote the method and the method never returns `null` or you think it is not returning `null`? You are not touching the variable anywhere else, hence my suspicion it is returning `null`. – Raven221221221 Feb 21 '20 at 14:28
  • the Xml file is right, so how can findViewById() return null if my R.id.signup_date_show exists? – Bob Rob Feb 21 '20 at 14:55
  • I don't know - I have no idea what the function does. What I do know is that you can access a private field from an anonymous class like you are doing (so that's not an issue) and since the function call is the last assignment of the field and the field is `null` that's where I would look. – Raven221221221 Feb 21 '20 at 15:04

0 Answers0