title: How to solve Cannot invoke "String.compareTo(String)" because "<parameter1>[<local3>]" is null?
body: <pre><code>import java.util.Scanner;
  
  
  public class Search {
      static Scanner scanner = new Scanner(System.in);
      static Scanner kb = new Scanner(System.in);
      static Scanner kb2 = new Scanner(System.in);
      public static void main (String[] args)
      {
  
          int choice;
          System.out.print(&quot;Choose a number of students: &quot;);
          int n = scanner.nextInt();  
          String name[] = new String[n+1];
          String course[] = new String[n+1];
          int ID[] = new int[n+1];
  
          for(int i=1;i &lt;= n; i++)
          {
              System.out.print(&quot;Enter ID number &quot; + i + &quot;: &quot;);
              ID[i] = scanner.nextInt();
              System.out.print(&quot;Enter Student name &quot; + i + &quot;: &quot;);
              name[i] = kb.nextLine();
              System.out.print(&quot;Enter Student course &quot; + i + &quot;: &quot;);
              course[i] = kb2.nextLine();
              System.out.println(&quot;----------------------------------------&quot;);
          }
  
         
        
          do
          {
              choice = menu();
              if(choice == 1)
              {
                  sortID(ID);
                  printValues(ID);
  
              }else if(choice == 2)
              {
                  nameSort(name,n);
                  printName(name,n);
              }else if(choice == 3)
              {
  
              }
          }while(choice !=0);
      }
  
      public static int menu()
      {
          System.out.print(&quot;\n1. Sort by ID\n2. Sort by Name\n3. Search by ID\n4. Search by Name\n5. Search by Course\n6. Display Records In table Form.\nYour Choice: &quot;);
          return scanner.nextInt();
      }
  
      public static void sortID(int []id)
      {
          int temp;
          int index, counter;
          for (counter=0; counter &lt; id.length -1; counter++) {
              for (index=0; index &lt; id.length - 1 - counter; index++) {
                  if (id[index] &gt; id[index+1]) {
                      temp = id[index];
                      id[index]=id[index+1];
                      id[index+1]=temp;
                  }
              }
          }
      }
  
      public static void printValues (int[]array) {
          
          System.out.println (&quot;\nSorted Id Number: &quot;);
          for(int i = 1; i &lt; array.length; i++){
              System.out.print (&quot;\n&quot; + array[i]);            
          }
              
      }
  
      public static void printName (String[]array,int a) {
          
          for (int i = 0; i &lt;= a - 1; i++) 
          {
              System.out.print(array[i] + &quot;, &quot;);
          }
              
      }
  
     public static void nameSort(String[] name,int a)
      {
          String temp;
          for (int i = 0; i &lt; a; i++) 
          {
              for (int j = i + 1; j &lt; a; j++) { 
                  if  (name[i].compareTo(name[j])&gt;0) 
                  {
                      temp = name[i];
                      name[i] = name[j];
                      name[j] = temp;
                  }
              }
          }
      }
  
  
  
  
  
  
  }
  </code></pre>
  <p>The sorting works on the id but i have problems in my names it wont push through the bubble sort and say its null, im just starting learning this language and it would be a great help. Ive been working on this since last night and ive tried transffering it under the if else(choice == 2) still it says null.</p>
  <p>Choose a number of students: 2
  Enter ID number 1: 123
  Enter Student name 1: Mark JAw
  Enter Student course 1: JSJS
  ----------------------------------------
  Enter ID number 2: 221
  Enter Student name 2: Ak akw
  Enter Student course 2: jdj
  ----------------------------------------</p>
  <pre><code>1. Sort by ID
  2. Sort by Name
  3. Search by ID
  4. Search by Name
  5. Search by Course
  6. Display Records In table Form.
  Your Choice: 1
  
  Sorted Id Number:
  
  123
  221
  1. Sort by ID
  2. Sort by Name
  3. Search by ID
  4. Search by Name
  5. Search by Course
  6. Display Records In table Form.
  Your Choice: 2
  Exception in thread &quot;main&quot; java.lang.NullPointerException: Cannot invoke &quot;String.compareTo(String)&quot; because &quot;name[i]&quot; is null
          at Search.nameSort(Search.java:95)
          at Search.main(Search.java:41)
  PS C:\Users\Bingus\Documents\Projects&gt; 
  </code></pre>