title: Get number of not-null fields in Java String array
body: <p>I created a String array with 100 fields.</p>
  
  <p>Then, I read in values from a file.</p>
  
  <p>There were only 7 values, but this number could vary anywhere from 1 to 100 so I left the String array with 100 fields.</p>
  
  <p>Essentially, I have a key value that I will use to identify other values that are equal.</p>
  
  <p>However, when I try to check the array for values equal to the key value, I receive a null pointer exception:</p>
  
  <pre><code>for(i = 0; i &lt;= memberType.length; i++)
  {
      if(memberType[i].equalsIgnoreCase(memberTypeInput))
      {
          out.println(memberCode[i] + " " + memberType[i] + " " + firstName[i] + " " +
                  lastName[i] + " " + age[i] + " " + gender[i]);
      }// END IF
  }// END FOR
  </code></pre>
  
  <p>From reading several other similar (but not the same) questions/answers, and Googling this problem for a few hours, I now understand that the null pointer exception occurs because I am trying to check my key value against some values which are null.</p>
  
  <p>My question is: what is the best way to avoid the null pointer exception in this case?</p>
  
  <p>More specifically: how do I get Java to find the number of not-null fields?
  I think if I can find the number of not-null fields, I can get Java to check my key value against only not-null fields, thus avoiding the null pointer exception.</p>
  
  <p>I tried finding the number of not-null fields with:</p>
  
  <pre><code>int index = -1;
  for (i = 0; i &lt; memberType.length; i++)
                  {
                      if (!memberType[i].equals(null))
                      {
                          index = i;
                      }
                  }
  </code></pre>
  
  <p>And then I used "index" in the for loop to search the array for values equal to the key value.</p>
  
  <p>However, the above code throws me another null pointer exception.</p>
  
  <p>In case you want to check it out, all of my code is below. There is some other stuff going on, but I wanted to leave it in so you could get the whole picture. The null pointer exception points to line 157. It's an IF statement.</p>
  
  <pre><code>package Phase2;
  
  import java.io.*;
  import java.util.*;
  
  import javax.swing.JOptionPane;
  
  import static java.lang.System.out;
  
  public class Phase2
  {
  
  // DEFINING ARRAYS &amp; VARIABLES //
  
  
  
  /////////////////
  // MAIN METHOD //
  /////////////////
  public static void main (String[] args)
  {
      int menuSelection;
  
      start_program();
  
      menuSelection = Menu();
  
      switch(menuSelection)
      {
          case 1:
              Modify_member();
              break;
  
          case 2:
              Modify_registration();
              break;
  
          case 3:
              Report();
              break;
  
          case 4:
              end_program();
              System.exit(0);
  
      }// END SWITCH CASE STATEMENT
  
  }// END MAIN METHOD
  
  
  ////////////////////////////////////////////
  // METHOD to run once at start of program //
  ////////////////////////////////////////////
  public static void start_program()
  {
  
      //VARS &amp; ARRAYS FOR start_program() METHOD
      String newLine;
  
      int count = -1;
      int[] memberCode = new int[100];
      String[] memberType = new String[100];
      String[] firstName = new String[100];
      String[] lastName = new String[100];
      int[] age = new int[100];
      char[] gender = new char[100];
  
      int menuSelection = 0;
      String menuOutput;
      int i;
  
      String memberTypeInput;
  
      try
      {
          BufferedReader membersFile = new BufferedReader(new FileReader("members.dat"));
  
          while ((newLine = membersFile.readLine()) != null)
              {
                  StringTokenizer delimiter = new StringTokenizer(newLine, "#");
                  count = count+1;
                  memberCode[count] = Integer.parseInt(delimiter.nextToken());
                  memberType[count] = delimiter.nextToken();
                  firstName[count] = delimiter.nextToken();
                  lastName[count] = delimiter.nextToken();
                  age[count] = Integer.parseInt(delimiter.nextToken());
                  gender[count] = delimiter.nextToken().charAt(0);
  
              }// END WHILE
  
          membersFile.close();
      }// END TRY
  
      catch(IOException error)
      {
          out.println("There was an error on file read " + error);
      }// END CATCH
  
  
      //////////
      // MENU //
      //////////
      menuOutput =    "\tMoon Area YMCA" + "\n" +
                      "1. All Member Info" + "\n" +
                      "2. All Members of a Specific Type" + "\n" +
                      "3. All Members Between a Specific Age Range" + "\n" +
                      "4. All Members of a Specific Gender" + "\n" +
                      "5. Report 5" + "\n" +
                      "6. Report 6" + "\n" +
                      "7. Report 7" + "\n" +
                      "8. Report 8" + "\n" +
                      "9. Report 9" + "\n" +
                      "10. Exit Report Menu" + "\n\n" +
                      "Please Make Your Selection";
  
      while(menuSelection != 10)
      {
          menuSelection = Integer.parseInt
                  (JOptionPane.showInputDialog(null, menuOutput, "Main Menu", JOptionPane.QUESTION_MESSAGE));
  
          switch(menuSelection)
          {
          case 1:
              for (i=0; i&lt;=count; ++i)
              {
                  out.println(memberCode[i] + " " + memberType[i] + " " + firstName[i] + " " +
                  lastName[i] + " " + age[i] + " " + gender[i]);
              }// END FOR
              break;
  
          case 2:
              memberTypeInput = JOptionPane.showInputDialog
                  (null, "Please enter member type", "Info by Member Type", JOptionPane.QUESTION_MESSAGE);
  
              if (memberTypeInput.equalsIgnoreCase("single") ||
                  memberTypeInput.equalsIgnoreCase("family") ||
                  memberTypeInput.equalsIgnoreCase("couple"))
              {   
                  // getting number of non-null fields in memberType array
  
                  //// NEED TO FIND NUMBER OF NOT NULL FIELDS
                  /*int index = -1;
  
                  for (i = 0; i &lt; memberType.length; i++)
                  {
                      if (!memberType[i].equals(null))
                      {
                          index = i;
                          break;
                      }
                  }*/
  
                  // getting index number of each memberTypeInput value in array
  
                  for(i = 0; i &lt;= memberType.length; i++)
                  {
                      if(memberType[i].equalsIgnoreCase(memberTypeInput))
                      {
                          out.println(memberCode[i] + " " + memberType[i] + " " + firstName[i] + " " +
                                  lastName[i] + " " + age[i] + " " + gender[i]);
                      }// END IF
                  }// END FOR
  
              }// END IF
              else
              {
                  JOptionPane.showMessageDialog
                      (null, "Please choose 'single', 'couple', or 'family'", "Error", JOptionPane.INFORMATION_MESSAGE);
              }// END ELSE
              break;
  
          case 3:
              break;
  
          case 4:
              break;
  
          case 5:
              break;
  
          case 6:
              break;
  
          case 7:
              break;
  
          case 8:
              break;
  
          case 9:
              break;
  
          case 10:
              break;
  
          }// END SWITCH
      }// END WHILE
  
  
  }// END start_program METHOD
  
  
  //////////////////////////////////////////////////////////
  // METHOD to print menu and return user input selection //
  //////////////////////////////////////////////////////////
  public static int Menu()
  {
      String menuSelectionString;
      int menuSelection = 0;
      String output;
  
      while(menuSelection == 0)
      {
          output =    "Moon Area YMCA\n\n" +
                      "1. Add/Modify Member Information\n" +
                      "2. Add/Modify Class Registrations\n" +
                      "3. Report Section\n" +
                      "4. Exit the System\n\n" +
                      "Please make your selection:" ;
  
          menuSelectionString = JOptionPane.showInputDialog(null, output, "Main Menu", JOptionPane.QUESTION_MESSAGE);
          if (menuSelectionString.equals("1") ||
              menuSelectionString.equals("2") ||
              menuSelectionString.equals("3") ||
              menuSelectionString.equals("4"))
          {
              menuSelection = Integer.parseInt(menuSelectionString);
          }// END IF STATEMENT
          else
          {
              JOptionPane.showMessageDialog(null, "Please select a number between 1 and 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
              menuSelection = 0;
          }// END ELSE STATEMENT
  
      }// END WHILE LOOP
  
      return menuSelection;
  
  }// END Menu METHOD
  
  
  ////////////////////////////////////////////////////////////
  // METHOD to add, delete, change basic member information //
  ////////////////////////////////////////////////////////////
  public static void Modify_member()
  {
      out.println("Modify_member() method has executed.");
  }// END Modify_member METHOD
  
  
  //////////////////////////////////////////////////////////////////
  // METHOD to add, delete, change class registration information //
  //////////////////////////////////////////////////////////////////
  public static void Modify_registration()
  {
      out.println("Modify_registration() method has executed.");
  }// END Modify_registration METHOD
  
  
  /////////////////////////////
  // METHOD to print reports //
  /////////////////////////////
  public static void Report()
  {
      out.println("Report() method has executed.");
  }// END Report METHOD
  
  
  //////////////////////////////////////////
  // METHOD to run once at end of program //
  //////////////////////////////////////////
  public static void end_program()
  {
      out.println("end_program() method has executed.");
  }// END end_program METHOD
  
  }// END Phase2 CLASS
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>