1

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.

Nisath
  • 55
  • 1
  • 8
  • 1
    You are using wrong id of `Button` inside your `ProfileDetailsActivity` The problem is here **`selectpfbirthdate = findViewById(R.id.btn_pf_dob)`** in your layout file your button id is **`selectpfbirthdate`** and your using **`btn_pf_dob`** in `findViewById` inside your `ProfileDetailsActivity` – AskNilesh Dec 21 '18 at 12:21
  • 1
    Use this **`selectpfbirthdate = findViewById(R.id.selectpfbirthdate)`** it will work – AskNilesh Dec 21 '18 at 12:24
  • 1
    You guys are absolutely right. I just figured it out and I was gonna delete the question. Thank you. – Nisath Dec 21 '18 at 12:31
  • `I was gonna delete the question` than please read this [What can I do when getting “We are no longer accepting questions/answers from this account”?](https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th) – AskNilesh Dec 21 '18 at 12:33
  • Jeez why is stackoverflow so harsh. I did do a lot of research. I spent like an hour with trials and errors. this app i am doing is quite large, has firebase auth, database and storage with over 20 activities. I didn't show any of them in the question on purpose, and they confused me so much. but I figured out the answer after I posted it anyway. it is possible that errors can be something as simple as an ID being different. It should be easy for others to figure out the solution too. isnt stackoverflow for helping others anyway? or is there any other site better than this? – Nisath Dec 21 '18 at 14:45

0 Answers0