title: Cast remote display android Nullpointer
body: <p>When we click on change color button nullpointer exception in CubeRender class.</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void  CubeRenderer.changeColor()' on a null object reference
  </code></pre>
  
  <p><strong>java class</strong></p>
  
  <pre><code>public class CubeRenderer implements GLSurfaceView.Renderer {
  
  private static final String TAG = "CubeRenderer";
  
  private static final float ANGLE_INCREMENT = 1.2f;
  private static final boolean CALCULATE_FPS = false;
  
  private Cube mCube;
  private float mAngle;
  private boolean mChangeColor;
  private long mLastTime;
  private long mFpsCounter;
  
  protected final float[] mMMatrix = new float[16];
  protected final float[] mMVMatrix = new float[16];
  protected final float[] mMVPMatrix = new float[16];
  protected final float[] mProjectionMatrix = new float[16];
  protected final float[] mViewMatrix = new float[16];
  protected final float[] mRotationMatrix = new float[16];
  
  public void onDrawFrame(GL10 unused) {
      if (CALCULATE_FPS) {
          long currentTime = SystemClock.uptimeMillis();
          if (mLastTime == 0) {
              mLastTime = currentTime;
          } else {
              mFpsCounter++;
              long diffTime = currentTime - mLastTime;
              if (diffTime &gt;= 1000) {
                  Log.d(TAG, "fps=" + mFpsCounter);
                  mFpsCounter = 0;
                  mLastTime = currentTime;
              }
          }
      }
  
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  
      // Set the camera position
      Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -10, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
  
      // Configure matrices for first cube
      Matrix.setIdentityM(mMMatrix, 0);
  
      Matrix.translateM(mMMatrix, 0, 0.0f, -0.5f, -1.5f);
  
      Matrix.setRotateM(mRotationMatrix, 0, 2 * mAngle, 0.0f, 1.0f, 1.0f);
      Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0);
  
      Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0);
      Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0);
  
      mCube.draw(mMVPMatrix, mChangeColor);
  
      // Configure matrices for second cube
      Matrix.setIdentityM(mMMatrix, 0);
  
      Matrix.translateM(mMMatrix, 0, 0.0f, 2.0f, 0.0f);
  
      Matrix.setRotateM(mRotationMatrix, 0, -mAngle, 0.0f, 1.0f, 1.0f);
      Matrix.multiplyMM(mMMatrix, 0, mRotationMatrix, 0, mMMatrix, 0);
  
      Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mMMatrix, 0);
      Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0);
  
      mCube.draw(mMVPMatrix, mChangeColor);
  
      mAngle += ANGLE_INCREMENT;
  }
  
  public void onSurfaceChanged(GL10 unused, int width, int height) {
      float ratio = (float) width / height;
      GLES20.glViewport(0, 0, width, height);
  
      // Configure perspective with field of view
      float fov = 30.0f;
      float near = 1.0f;
      float far = 100.0f;
      float top = (float) Math.tan(fov * Math.PI / 360.0f) * near;
      float bottom = -top;
      float left = ratio * bottom;
      float right = ratio * top;
      Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
  }
  
  public void onSurfaceCreated(GL10 unused, EGLConfig config) {
      // Set background color
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  
      // Depth handling
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      GLES20.glDepthFunc(GLES20.GL_LEQUAL);
  
      // Set anti-aliasing
      GLES20.glEnable(GLES20.GL_BLEND);
      GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  
      // Important to initialize the graphics on the GL thread
      mCube = new Cube();
  }
  
  /**
   * Utility method to allow the user to change the cube color.
   */
  public void changeColor() {
      mChangeColor = !mChangeColor;
  }
  }
  </code></pre>
  
  <p>Accessing  CubeRenderer class method mCubeRenderer.changeColor() exact place where null pointer exception need to handle</p>
  
  <pre><code>public class PresentationService extends CastRemoteDisplayLocalService {
  
  private static final String TAG = "PresentationService";
  
  // First screen
  private CastPresentation mPresentation;
  private MediaPlayer mMediaPlayer;
  private CubeRenderer mCubeRenderer;
  
  @Override
  public void onCreate() {
      super.onCreate();
      // Audio
      mMediaPlayer = MediaPlayer.create(this, R.raw.sound);
      mMediaPlayer.setVolume((float) 0.1, (float) 0.1);
      mMediaPlayer.setLooping(true);
  }
  
  @Override
  public void onCreatePresentation(Display display) {
      createPresentation(display);
  }
  
  @Override
  public void onDismissPresentation() {
      dismissPresentation();
  }
  
  private void dismissPresentation() {
      if (mPresentation != null) {
          mMediaPlayer.stop();
          mPresentation.dismiss();
          mPresentation = null;
      }
  }
  
  private void createPresentation(Display display) {
      dismissPresentation();
      mPresentation = new TVPresentation(this, display);
  
      try {
          mPresentation.show();
          mMediaPlayer.start();
      } catch (WindowManager.InvalidDisplayException ex) {
          Log.e(TAG, "Unable to show presentation, display was removed.", ex);
          dismissPresentation();
      }
  }
  
  /**
   * Utility method to allow the user to change the cube color.
   */
  public void changeColor() {
      mCubeRenderer.changeColor();
  }
  </code></pre>
  
  <p>}</p>