title: "Attempt to invoke virtual method on a null object reference" when trying to getLayoutParams of a CardView in a DialogFragment?
body: <p>I'm trying to set the size of a <code>CardView</code> inside of a <code>DialogFragment</code>, but I get this error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.support.v7.widget.CardView.getLayoutParams()' on a null object reference</p>
  </blockquote>
  
  <p>This is my code:</p>
  
  <pre><code>        CardView cardView;
          onCreate(...){
              cardView = (CardView) findViewById(R.id.cardViewInDialogFragment);
          }
  
          private void openDialogFragment() {
  
              DisplayMetrics displayMetrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  
              BlankFragment blankFragment = new BlankFragment();
              blankFragment.show(getSupportFragmentManager(),"blankFragment");
  
              CardView.LayoutParams layoutParams = (CardView.LayoutParams) cardView.getLayoutParams();
              layoutParams.height = displayMetrics.heightPixels - 20;
              layoutParams.width = displayMetrics.widthPixels - 20;
              cardView.setLayoutParams(layoutParams);
          }
  </code></pre>
  
  <p>Is it not possible or am I doing something wrong?</p>
  
  <p><strong>UPDATE:</strong>
  The cardView is inside the fragment.</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>When changing the code to bellow:</p>
  
  <p>BlankFragment:</p>
  
  <pre><code>public static View view;
  pulic void onCreateView(...) {
  
      view = inflater.inflate(R.layout.fragment_blank, container, false);
      return view;
  }
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>CardView cardView;
  protected void onCreate(...){
      ...
  
      cardView = (CardView) BlankFragment.view.findViewById(R.id.cardViewInDialogFragment);
      openDialogFragment();
  }
  
  private void openDialogFragment() {
  
      DisplayMetrics displayMetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
  
      BlankFragment blankFragment = new BlankFragment();
      blankFragment.show(getSupportFragmentManager(),"blankFragment");
  
      CardView.LayoutParams layoutParams = (CardView.LayoutParams) cardView.getLayoutParams();
      layoutParams.height = displayMetrics.heightPixels - 20;
      layoutParams.width = displayMetrics.widthPixels - 20;
      cardView.setLayoutParams(layoutParams);
  }
  </code></pre>
  
  <p>, I get this error:</p>
  
  <blockquote>
    <p>FATAL EXCEPTION: main
                                                                                            Process: com.sim.buttombarnavigationandfragment, PID: 10685
                                                                                            java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.sim.buttombarnavigationandfragment/com.sim.buttombarnavigationandfragment.MainActivity}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'android.view.View android.view.View.findViewById(int)' on a null
    object reference
                                                                                                at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2680)
                                                                                                at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741)
                                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                                at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:6176)
                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
                                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual
    method 'android.view.View android.view.View.findViewById(int)' on a
    null object reference
                                                                                                at
    com.sim.buttombarnavigationandfragment.MainActivity.onCreate(MainActivity.java:56)
                                                                                                at android.app.Activity.performCreate(Activity.java:6679)
                                                                                                at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                                at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
                                                                                                at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741) 
                                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                                at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:6176) 
                                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)</p>
  </blockquote>