title: Android Studio: Attempt to invoke virtual method 'void android.view.View.setOnClickListener' on a null object reference
body: <p>I keep getting NullPointerException. </p>
  
  <pre><code>Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
  </code></pre>
  
  <p>I know it is because  (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle) is null, but I don't understand why it is the case and how to solve it. </p>
  
  <p>I tried this <a href="https://stackoverflow.com/questions/32924971/attempt-to-invoke-virtual-method-on-a-null-object-reference">solution</a>. but it didn't work. 
  Below are my Fragment.java and xml files. </p>
  
  <pre><code> @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);
          surfaceview = (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle);
          return view;
  
      }
  
  
      @Override
      public void onViewCreated(final View view, Bundle savedInstanceState) {
  
          view.findViewById(R.id.picture).setOnClickListener(this);
          view.findViewById(R.id.info).setOnClickListener(this);
  
          mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
          //view.findViewById(R.id.surfaceview_show_rectangle).setOnClickListener(this);
  
  
          //surfaceview = (SurfaceView)view.findViewById(R.id.surfaceview_show_rectangle);
          surfaceview.setZOrderOnTop(true);  
          surfaceview.getHolder().setFormat(PixelFormat.TRANSPARENT);  
          surfaceHolder = surfaceview.getHolder(); 
  
  
          surfaceview_result = (SurfaceView)view.findViewById(R.id.surfaceview_show_result);
          surfaceview_result.setZOrderOnTop(true);  
          surfaceview_result.getHolder().setFormat(PixelFormat.TRANSPARENT);  
          surfaceResultHolder = surfaceview_result.getHolder(); 
      }
  </code></pre>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;SurfaceView
          android:id="@+id/surfaceview_show_rectangle"
          android:layout_width="match_parent"
          android:layout_height="483dp"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:layout_marginStart="0dp"
          android:layout_marginTop="0dp"
          /&gt;
  
      &lt;SurfaceView
          android:id="@+id/surfaceview_show_result"
          android:layout_width="match_parent"
          android:layout_height="483dp"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:layout_marginStart="0dp"
          android:layout_marginTop="0dp" /&gt;
  
      &lt;com.example.android.camera2basic.AutoFitTextureView
          android:id="@+id/texture"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"
          android:layout_marginStart="0dp"
          android:layout_marginTop="0dp" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/control"
          android:layout_width="match_parent"
          android:layout_height="112dp"
          android:layout_alignParentBottom="true"
          android:layout_alignParentStart="true"
          android:background="@color/control_background"&gt;
  
          &lt;Button
              android:id="@+id/picture"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:visibility="invisible"
              android:text="@string/picture" /&gt;
  
          &lt;ImageButton
              android:id="@+id/info"
              android:contentDescription="@string/description_info"
              style="@android:style/Widget.Material.Light.Button.Borderless"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical|right"
              android:padding="20dp"
              android:visibility="invisible"
              android:src="@drawable/ic_action_info" /&gt;
  
      &lt;/FrameLayout&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentStart="true"
          android:layout_alignParentBottom="true"
          android:layout_marginStart="69dp"
          android:layout_marginBottom="37dp"
          android:text="Camera2Dome"
          android:textColor="#fff"
          android:textSize="35sp" /&gt;
  
  &lt;/RelativeLayout&gt;
  
  
  </code></pre>
  
  <p>Below are my error messages. </p>
  
  <pre><code>D/AndroidRuntime: Shutting down VM
  E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.android.camera2basic, PID: 9168
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.camera2basic/com.example.android.camera2basic.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.SurfaceView.setZOrderOnTop(boolean)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
          at android.app.ActivityThread.access$1000(ActivityThread.java:150)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5444)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.SurfaceView.setZOrderOnTop(boolean)' on a null object reference
          at com.example.android.camera2basic.Camera2BasicFragment.onViewCreated(Camera2BasicFragment.java:565)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1430)
          at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
          at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
          at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
          at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
          at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
          at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3231)
          at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3181)
          at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
          at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:572)
          at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
          at android.app.Activity.performStart(Activity.java:6317)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2392)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
          at android.app.ActivityThread.access$1000(ActivityThread.java:150) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5444) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
  I/Process: Sending signal. PID: 9168 SIG: 9
  Application terminated.
  </code></pre>
  
  <p>I really appreciate any help. Thanks in advance!!</p>