title: java.lang.NullPointerException: Attempt to invoke virtual method android
body: <p>i am a beginner in the android, and i dont know what is the problem in my code ! and i need your help, this is my code:
  i am trying to do a calculator = - / * ..... and i removed the if view==pbtn for example...just to try the onclick function...and i cant find what is my problem</p>
  
  <pre><code>package com.michael.mikematta.calculator;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import static com.michael.mikematta.calculator.R.id.FirstNum;
  import static com.michael.mikematta.calculator.R.id.HelokBtn;
  import static com.michael.mikematta.calculator.R.id.KafolBtn;
  import static com.michael.mikematta.calculator.R.id.MinusBtn;
  import static com.michael.mikematta.calculator.R.id.PlusBtn;
  import static com.michael.mikematta.calculator.R.id.SecondNum;
  import static com.michael.mikematta.calculator.R.id.TheResult;
  
  
  public class MainActivity extends Activity implements View.OnClickListener {
  
      EditText firstnumx;
      EditText secondnum;
      Button pbtn;
      Button minusbtn;
      Button kafolbtn;
      Button helokbtn;
      TextView theresult;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button pbtn = (Button) findViewById(R.id.PlusBtn);
          Button minusbtn = (Button) findViewById(R.id.MinusBtn);
          Button kafolbtn = (Button) findViewById(R.id.KafolBtn);
          Button helokbtn = (Button) findViewById(R.id.HelokBtn);
          EditText firstnum = (EditText) findViewById(R.id.FirstNum);
          EditText secondnum = (EditText) findViewById(R.id.SecondNum);
          TextView theresult = (TextView) findViewById(R.id.TheResult);
  
          minusbtn.setOnClickListener(this);
          pbtn.setOnClickListener(this);
          kafolbtn.setOnClickListener(this);
          helokbtn.setOnClickListener(this);
      }
  
      @Override
      public void onClick(View view) {
  
  
          theresult.setText("hi");
  
  
      }
  }
  </code></pre>
  
  <p>my xml:</p>
  
  <pre><code> &lt;LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/ic_launcher"
          android:orientation="vertical"
  
          tools:context=".MainActivity"&gt;
  
          &lt;TextView
              android:text="Hello Calculator"
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:gravity="center"
              android:textSize="20dp"
              android:textColor="#000"
              android:textStyle="bold"
              /&gt;
          &lt;EditText
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:hint="Enter First Num"
              android:background="#fff"
              android:id="@+id/FirstNum"
              android:layout_margin="10dp"
              /&gt;
          &lt;EditText
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:hint="Enter Second Num"
              android:background="#fff"
              android:id="@+id/SecondNum"
              android:layout_margin="10dp"
              /&gt;
          &lt;Button
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="+"
              android:background="#ffeeeeee"
              android:textSize="22dp"
              android:id="@+id/PlusBtn"
              android:layout_margin="10dp"
              /&gt;
          &lt;Button
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="-"
              android:background="#ffeeeeee"
              android:textSize="22dp"
              android:id="@+id/MinusBtn"
              android:layout_margin="10dp"
              /&gt;
          &lt;Button
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="*"
              android:background="#ffeeeeee"
              android:textSize="22dp"
              android:id="@+id/KafolBtn"
              android:layout_margin="10dp"
              /&gt;
          &lt;Button
              android:layout_width="200dp"
              android:layout_height="50dp"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="/"
              android:background="#ffeeeeee"
              android:textSize="22dp"
              android:id="@+id/HelokBtn"
              android:layout_margin="10dp"
              /&gt;
          &lt;TextView
              android:text="Here will be your result"
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:gravity="center"
              android:textSize="20dp"
              android:textColor="#fff"
              android:textStyle="bold"
              android:background="#ffff714f"
              android:id="@+id/TheResult"
              /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
  02-08 03:09:50.468    1648-1666/com.michael.mikematta.calcu2 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeeff540, error=EGL_SUCCESS
  02-08 03:09:50.538    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 33 frames!  The application may be doing too much work on its main thread.
  02-08 03:09:51.177    1648-1648/com.michael.mikematta.calcu2 I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
  02-08 03:09:53.984    1648-1648/com.michael.mikematta.calcu2 D/AndroidRuntime﹕ Shutting down VM
  02-08 03:09:54.000    1648-1648/com.michael.mikematta.calcu2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.michael.mikematta.calcu2, PID: 1648
      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
              at com.michael.mikematta.calcu2.MainActivity.onClick(MainActivity.java:47)
              at android.view.View.performClick(View.java:4756)
              at android.view.View$PerformClick.run(View.java:19749)
              at android.os.Handler.handleCallback(Handler.java:739)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5221)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  02-08 03:09:57.267    1648-1648/com.michael.mikematta.calcu2 I/Process﹕ Sending signal. PID: 1648 SIG: 9
  </code></pre>
  
  <p>any help please?</p>