title: button initialization in other function instead of OnCreate() method
body: <p>Can we define a function in OnCreate method which can be used to initialize buttons and all other widgets. I have tried the following but I am getting </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'void
    android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a
    null object reference error</p>
  </blockquote>
  
  <pre><code>package com.conceptkillers.www.electromania1;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.EditText;
  import android.widget.LinearLayout;
  import android.widget.Spinner;
  
  /**
   * Created by Nikesh on 11/02/2016.
   */
  public class OhmsLawActivity extends AppCompatActivity {
  
      int checkedCount = 0;
      LinearLayout vlayout,ilayout,rlayout,playout;
      CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
      Spinner spinnerV,spinnerI,spinnerR,spinnerP;
      EditText edittextv,edittexti,edittextr,edittextp;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.setTitle("Ohms Law");
          InitializeAllWidgets();
  
      }
  
      public void InitializeAllWidgets() {
  
          checkBox1 = (CheckBox) findViewById(R.id.checkbox_v);
          checkBox2 = (CheckBox) findViewById(R.id.checkbox_i);
          checkBox3 = (CheckBox) findViewById(R.id.checkbox_r);
          checkBox4 = (CheckBox) findViewById(R.id.checkbox_p);
  
          edittextv = (EditText) findViewById(R.id.edittext_v);
          edittexti = (EditText) findViewById(R.id.edittext_i);
          edittextr = (EditText) findViewById(R.id.edittext_r);
          edittextp = (EditText) findViewById(R.id.edittext_p);
  
          String[] valuesVoltageSpinner = {"YV","ZV","EV","PV","TV","GV","MV","KV"
                  ,"V","mV","uV","nV","pV","fV","aV","zV","yV"};
          String[] valuesCurrentSpinner = {"YA","ZA","EA","PA","TA","GA","MA","KA"
                  ,"A","mA","uA","nA","pA","fA","aA","zA","yA"};
          String[] valuesResistanceSpinner = {"YOhm","ZOhm","EOhm","POhm","TOhm","GOhm","MOhm","KOhm"
                  ,"Ohm","mOhm","uOhm","nOhm","pOhm","fOhm","aOhm","zOhm","yOhm"};
          String[] valuesPowerSpinner = {"YW","ZW","EW","PW","TW","GW","MW","KW"
                  ,"W","mW","uW","nW","pW","fW","aW","zW","yW"};
  
          spinnerV = (Spinner) findViewById(R.id.spinner_v);
          spinnerI = (Spinner) findViewById(R.id.spinner_i);
          spinnerR = (Spinner) findViewById(R.id.spinner_r);
          spinnerP = (Spinner) findViewById(R.id.spinner_p);
  
          ArrayAdapter&lt;String&gt; voltageAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, valuesVoltageSpinner);
          voltageAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
          spinnerV.setAdapter(voltageAdapter);
  
          ArrayAdapter&lt;String&gt; currentAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, valuesCurrentSpinner);
          currentAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
          spinnerI.setAdapter(currentAdapter);
  
          ArrayAdapter&lt;String&gt; resistanceAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, valuesResistanceSpinner);
          resistanceAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
          spinnerR.setAdapter(resistanceAdapter);
  
          ArrayAdapter&lt;String&gt; powerAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, valuesPowerSpinner);
          powerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
          spinnerP.setAdapter(powerAdapter);
  
          checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  handleCheckBox1();
              }
          });
  
          checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  handleCheckBox2();
              }
          });
  
          checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  handleCheckBox3();
              }
          });
  
          checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  handleCheckBox4();
              }
          });
      }
  
      public void onButtonClicked(View v) {
  
          switch(v.getId()) {
              case R.id.simple_voltage:
                  checkBox1.setVisibility(v.INVISIBLE);
                  checkBox2.setVisibility(v.VISIBLE);
                  checkBox3.setVisibility(v.VISIBLE);
                  checkBox4.setVisibility(v.VISIBLE);
                  checkedCount = 0;
                  break;
              case R.id.simple_current:
                  checkBox1.setVisibility(v.VISIBLE);
                  checkBox2.setVisibility(v.INVISIBLE);
                  checkBox3.setVisibility(v.VISIBLE);
                  checkBox4.setVisibility(v.VISIBLE);
                  checkedCount = 0;
                  break;
              case R.id.simple_resistance:
                  checkBox1.setVisibility(v.VISIBLE);
                  checkBox2.setVisibility(v.VISIBLE);
                  checkBox3.setVisibility(v.INVISIBLE);
                  checkBox4.setVisibility(v.VISIBLE);
                  checkedCount = 0;
                  break;
              case R.id.simple_power:
                  checkBox1.setVisibility(v.VISIBLE);
                  checkBox2.setVisibility(v.VISIBLE);
                  checkBox3.setVisibility(v.VISIBLE);
                  checkBox4.setVisibility(v.INVISIBLE);
                  checkedCount = 0;
                  break;
          }
      }
  
      private void handleCheckBox1() {
  
          if(checkedCount &lt; 2) {
  
              if (checkBox1.isChecked()) {
                  checkedCount++;
                  edittextv.setVisibility(View.VISIBLE);
                  spinnerV.setVisibility(View.VISIBLE);
              } else {
                  checkedCount--;
                  edittextv.setVisibility(View.INVISIBLE);
                  spinnerV.setVisibility(View.INVISIBLE);
              }
  
          }
      }
  
      private void handleCheckBox2() {
  
          if(checkedCount &lt; 2) {
              if (checkBox2.isChecked()) {
                  checkedCount++;
                  edittexti.setVisibility(View.VISIBLE);
                  spinnerI.setVisibility(View.VISIBLE);
              } else {
                  checkedCount--;
                  edittexti.setVisibility(View.INVISIBLE);
                  spinnerI.setVisibility(View.INVISIBLE);
              }
          }
      }
  
      private void handleCheckBox3() {
  
          if(checkedCount &lt; 2) {
              if (checkBox3.isChecked()) {
                  checkedCount++;
                  edittextr.setVisibility(View.VISIBLE);
                  spinnerR.setVisibility(View.VISIBLE);
              } else {
                  checkedCount--;
                  edittextr.setVisibility(View.INVISIBLE);
                  spinnerR.setVisibility(View.INVISIBLE);
              }
          }
      }
  
      private void handleCheckBox4(){
  
          if(checkedCount &lt; 2) {
              if (checkBox4.isChecked()) {
                  checkedCount++;
                  edittextp.setVisibility(View.VISIBLE);
                  spinnerP.setVisibility(View.VISIBLE);
              } else {
                  checkedCount--;
                  edittextp.setVisibility(View.INVISIBLE);
                  spinnerP.setVisibility(View.INVISIBLE);
              }
          }
      }
  }
  </code></pre>