title: NullPointerException for JButton
body: <p>I am trying to assign text to buttons inside a loop but I am getting an NullPointerException.
  I have initialized the button array inside constructor of the class and after initilizatoin, I call the following method. Here is the code where I am getting error.</p>
  
  <pre><code>public class Alfred
  {
     private String names[]={"nfs","gta","maxpayne","hitman"};
     private JButton but[];
  
     public Alfred()
     {
      ...
       but= new JButton[4];
      AssignLettersToButtons();
     }
      private void AssignLettersToButtons()
      {       
          for(int i=0;i&lt;names.length;i++)
          {
              but[i].setText(names[i]); // error
              pane1.add(but[i]);
          }
      }
  }
  }
  </code></pre>
  
  <p>The length of button array and names array is same. What is the reason for this exception and how can I solve it?</p>
  
  <p>Regards</p>