title: Crash: Attempt to invoke virtual method 'void android.view.View.dispatchWindowVisibilityChanged(int)' on a null object reference
body: <p>I have a huge application I got from a developer team and I'm trying to just simply run it. It's a piece of trash really code-wise, but I have this crash immediately after spending hours to just make it install (which I did):</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchWindowVisibilityChanged(int)' on a null object reference
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1309)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>aka the least useful error message ever.</p>
  
  <p>How would you find the cause of this? I see this can be throws for multiple things, but unfortunately I have no idea of its a list or something else, since I don't even know what class would cause the issue (there are cc. 300 classes in this code...)</p>
  
  <p>Edit:</p>
  
  <p>so after some debugging the issue seem to be this: a custom view has only 7 children yet the mChildrenCount is shown as 8:<a href="https://i.stack.imgur.com/4QpaP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4QpaP.png" alt="enter image description here"></a></p>
  
  <p>The loop which goes through all the children reaches the 8th one (which will be null) and throws an exception here:</p>
  
  <pre><code>@Override
  public void dispatchWindowVisibilityChanged(int visibility) {
      super.dispatchWindowVisibilityChanged(visibility);
      final int count = mChildrenCount;
      final View[] children = mChildren;
      for (int i = 0; i &lt; count; i++) {
          children[i].dispatchWindowVisibilityChanged(visibility); //Exception here on non-existen child...
      }
  }
  </code></pre>
  
  <p>which is in ViewGroup.java.</p>
  
  <p>So what could be the problem here?</p>