DelphiFAQ Home Search:
General :: Programming :: Java


Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

java.lang.NullPointerException when using arrays?

Question:

Why do I keep getting a "java.lang.NullPointerException" when using arrays?

Answer:

Two possible explanations:
  1. You've indexed past the array bounds
  2. You haven't initialized the array correctly

   // example of an incorrectly initialized array:
 
        public class Test {
           public static void main(String args[]) {
              String sa[] = new String[5];
              sa[0].charAt(0);
           }
        }
 
     // example of a correctly initialized array:
 
        public class Test {
           public static void main(String args[]) {
              String sa[] = new String[5];
 
              for(int i = 0; i < 5; i++) {
                 sa[i] = new String();
              }
  
              sa[0].charAt(0);
           }
        }
 

Generated 0:01:51 on Jul 22, 2017