title: How to set onclicklistener button which is inside a custom dialog
body: <p>I am new to android and I am getting null pointer exception when calling onclicklistener of a button inside the dialog. what should I do.</p>
  
  <pre><code>btn_add.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  Dialog d=new Dialog(MainActivity.this);
                  d.setContentView(R.layout.activity_main);
                  d.setTitle("Add content");
                  d.show();
                  btnsubmit = (Button) findViewById(R.id.btn_submit);
                  btnsubmit.setOnClickListener(new OnClickListener() {
  
                      @Override
                      public void onClick(View v) {
  
                          String name = etName.getText().toString();
                          String phoneNo = etPhone.getText().toString();
  
                          String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('"
                                  + name + "','" + phoneNo + "')";
                          sqlHandler.executeQuery(query);
                          showList();
                          etName.setText("");
                          etPhone.setText("");
  
                      }
                  });
              }
          });
  </code></pre>
  
  <p>I will really appreciate your help.
  this is how my dialog xml looks like:-</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".MainActivity"&gt;
      &lt;TableLayout
  
              android:layout_width="match_parent"
  
              android:layout_height="wrap_content" &gt;
  
  
  
          &lt;TableRow
  
                  android:id="@+id/tableRow1"
  
                  android:layout_width="wrap_content"
  
                  android:layout_height="wrap_content" &gt;
  
  
  
              &lt;TextView
  
                      android:id="@+id/textView1"
  
                      android:layout_width="wrap_content"
  
                      android:layout_height="wrap_content"
  
                      android:text="@string/name" /&gt;
  
  
  
              &lt;EditText
  
                      android:id="@+id/et_name"
  
                      android:layout_width="wrap_content"
  
                      android:layout_height="wrap_content"
  
                      android:ems="10" &gt;
  
              &lt;/EditText&gt;
  
          &lt;/TableRow&gt;
  
  
  
          &lt;TableRow
  
                  android:id="@+id/tableRow2"
  
                  android:layout_width="wrap_content"
  
                  android:layout_height="wrap_content" &gt;
  
  
  
              &lt;TextView
  
                      android:id="@+id/textView1"
  
                      android:layout_width="wrap_content"
  
                      android:layout_height="wrap_content"
  
                      android:text="@string/phone" /&gt;
  
  
  
              &lt;EditText
  
                      android:id="@+id/et_phone"
  
                      android:layout_width="wrap_content"
  
                      android:layout_height="wrap_content"
  
                      android:ems="10" &gt;
  
              &lt;/EditText&gt;
  
          &lt;/TableRow&gt;
  
          &lt;Button
                  android:id="@+id/btn_submit"
                  android:layout_width="80dp"
                  android:layout_height="40dp"
                  android:text="@string/submit"
                  android:layout_centerVertical="true"
                  android:layout_centerHorizontal="true"/&gt;
      &lt;/TableLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>