#Java Help .-.

1 messages · Page 1 of 1 (latest)

sand nimbus
#

I have a FloatingActionButton on MainActivity that just calls this CreateEvent activity (AKA class)
and when I click that action button it crashes with this error

Unable to start activity ComponentInfo{com.example.library/com.example.library.CreateEvent}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Here is the code it said the error is at inside CreateEvent

etStart = findViewById(R.id.etCreateStart);
etStart.setOnClickListener(v -> {

            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            TimePickerDialog timePickerDialog = new TimePickerDialog(this, (view, hourOfDay, minute1) -> {

                String fix_00 = String.format("%02d", minute1);
                etStart.setText(hourOfDay + ":" + fix_00);

            }, hour, minute, true);

            timePickerDialog.show();
});
#

idk why it crashes, it was working just fine a bit ago

fathom sail
#

Could you show an XML file of a layout?

sand nimbus
#

of createevent?

fathom sail
#

It tries to invoke setOnClickListener() on a etStart but can't becuse etStart is null...
You may try to cast etStart

etStart = (EditText)findViewById(R.id.etCreateStart)

but I'm not sure if it'll help, I think it's redundant in newer Androids. It can also happen because R can't find etCreateStart in a Context, in which case running setContentView(R.layout.createevent) before the whole block may help

sand nimbus
#

oh....the contentView .-.

#

im dumb, lemme try that

#

YEP .-.

#

gg

#

thanks, again...

fathom sail
sand nimbus
#

Yeah, casting these seems to be not required anymore

#

got a time picker :D

sand nimbus