title: How to tell Java that a variable cannot possibly be null?
body: <p>I have a program that basically looks like this:</p>
  
  <pre><code>boolean[] stuffNThings;
  int state=1;
  for(String string:list){
     switch(state){
        case 1:
           if(/*condition*/){
              // foo
              break;
           }else{
              stuffNThings=new boolean[/*size*/];
              state=2;
           }
        // intentional fallthrough
        case 2:
           // bar
           stuffNThings[0]=true;
     }
  }
  </code></pre>
  
  <p>As you, a human, can see, case 2 only ever happens when there was previously a state 1 and it switched to state 2 after initialising the array. But Eclipse and the Java compiler don't see this, because it looks like pretty complex logic to them. So Eclipse complains:</p>
  
  <blockquote>
    <p>The local variable stuffNThings may not have been initialized."</p>
  </blockquote>
  
  <p>And if I change "<code>boolean[] stuffNThings;</code>" to "<code>boolean[] stuffNThings=null;</code>", it switches to this error message:</p>
  
  <blockquote>
    <p>Potential null pointer access: The variable stuffNThings may be null at this location.</p>
  </blockquote>
  
  <p>I also can't initialise it at the top, because the size of the array is only determined after the final loop in state 1.</p>
  
  <p>Java thinks that the array could be null there, but I know that it can't. Is there some way to tell Java this? Or am I definitely forced to put a useless <code>null</code> check around it? Adding that makes the code harder to understand, because it looks like there may be a case where the value doesn't actually get set to <code>true</code>.</p>