I understand this question has been asked many times. but most of them are specific. In my case, tried all the fixes I could. Any help appreciated? p.s. I am a beginner and I have not shown some code from java that I am sure that it doesnt affect the issue here. Also the main function I am aiming to do is, to open a dialog box with a calender to pick a date when user clicks a button (btn_pf_dob), and when user selects it, the chosen date is displayed on the same button.
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.name.appname/com.example.name.appname.ProfileDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Java:
public class ProfileDetailsActivity extends AppCompatActivity {
Button selectpfbirthdate;
DatePickerDialog datePickerDialog;
int year;
int month;
int dayOfMonth;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_details);
selectpfbirthdate = findViewById(R.id.btn_pf_dob);
selectpfbirthdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(ProfileDetailsActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
selectpfbirthdate.setText(day + "/" + (month + 1) + "/" + year);
}
}, year, month, dayOfMonth);
//datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
});
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/colorPrimaryDark"
style="@style/traNav"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProfileDetailsActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:background="@color/colorPrimaryDark">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_weight="1"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_weight="1"
android:gravity="start"
android:padding="10dip"
android:text="@string/date_of_birth"
android:textColor="@android:color/background_light"
android:textSize="18sp"
/>
<Button
style="@style/et_background"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAlignment="center"
android:gravity="start"
android:padding="10dip"
android:textColor="@android:color/background_dark"
android:background="#d3d4d5"
android:textSize="18sp"
android:hint="@string/pick_date"
android:textAllCaps="false"
android:id="@+id/selectpfbirthdate"/>
</TableRow>
</TableLayout>
</LinearLayout>
<Scrollview>
</LinearLayout>
EDIT: My bad, I use the wrong ID. Thank you.