title: Null Pointer Exception - Getting EditText Value
body: <p>This is my first question on here I will apologize in advance if I didn't ask this question very well. I have looked at my other questions similar to my problem, but I have not found a good solution to satisfy what is going on in my program.</p>
  <p>So my problem is I am trying to assign a value to a variable called, <code>num1</code> from my <code>EditText</code> field called, <code>num1TextField</code>, but I am not having any luck so far.</p>
  <p>The segment inside my java file that throws the exception is:</p>
  <pre><code>EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
  num1 = num1Field.getText().toString();
  </code></pre>
  <p>I appreciate every contribution to this problem in advance.</p>
  <hr />
  <p>Here is my <code>fragment_compute.xml</code> file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot; &gt;
  
  &lt;TextView
      android:id=&quot;@+id/textView1&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_alignParentTop=&quot;true&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;14dp&quot;
      android:text=&quot;@string/compute_activity&quot;
      android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/textView2&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_below=&quot;@+id/textView1&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;44dp&quot;
      android:text=&quot;@string/num_1&quot;
      android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot; /&gt;
  
  &lt;EditText
      android:id=&quot;@+id/num1TextField&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_below=&quot;@+id/textView2&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:ems=&quot;10&quot;
      android:inputType=&quot;number&quot;
      android:gravity=&quot;center&quot; &gt;
  
      &lt;requestFocus /&gt;
  &lt;/EditText&gt;
  
  &lt;EditText
      android:id=&quot;@+id/num2TextField&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_alignLeft=&quot;@+id/num1TextField&quot;
      android:layout_below=&quot;@+id/textView3&quot;
      android:ems=&quot;10&quot;
      android:inputType=&quot;number&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:gravity=&quot;center&quot; &gt;
  &lt;/EditText&gt;
  
  &lt;TextView
      android:id=&quot;@+id/textView4&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_below=&quot;@+id/num2TextField&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;22dp&quot;
      android:text=&quot;@string/result&quot;
      android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/textView3&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_below=&quot;@+id/num1TextField&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;22dp&quot;
      android:text=&quot;@string/num_2&quot;
      android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/result&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_below=&quot;@+id/textView4&quot;
      android:layout_centerHorizontal=&quot;true&quot;
      android:layout_marginTop=&quot;22dp&quot; /&gt;
  
  &lt;Button
      android:id=&quot;@+id/compute_multiply&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_alignRight=&quot;@+id/num2TextField&quot;
      android:layout_below=&quot;@+id/result&quot;
      android:layout_marginTop=&quot;40dp&quot;
      android:text=&quot;@string/multiply&quot; /&gt;
  
  &lt;Button
      android:id=&quot;@+id/compute_add&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_alignBaseline=&quot;@+id/compute_multiply&quot;
      android:layout_alignBottom=&quot;@+id/compute_multiply&quot;
      android:layout_alignLeft=&quot;@+id/num2TextField&quot;
      android:text=&quot;@string/add&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <hr />
  <p>Here is my <code>ComputeFragment.java</code> code:</p>
  <pre><code>public class ComputeFragment extends Fragment {
  
      String num1;
      String num2;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment_compute, parent, false);
  
          // Add Button
          Button addButton = (Button)v.findViewById(R.id.compute_add);
          addButton.setOnClickListener(new View.OnClickListener() {
                              
              @Override
              public void onClick(View v) {
                  // ERROR STARTS HERE!
                  EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
                  num1 = num1Field.getText().toString(); // Null Pointer Exception is thrown here!
              
                  EditText num2Field = (EditText)v.findViewById(R.id.num2TextField);
                  num2 = num2Field.getText().toString();
                  // ERROR ENDS HERE!
              
                  Intent i = new Intent(getActivity(), AddActivity.class);
                  i.putExtra(&quot;x&quot;, num1);
                  i.putExtra(&quot;y&quot;, num2);
                  startActivityForResult(i,0);
              }
          });
  
          return v; 
      }
  }
  </code></pre>