title: How do I get my array out of try/catch block?
body: <p>I've been kicking this code around for a while and I think I know what the general problem is, I can't figure out how to correct it.  I'm getting the following error:</p>
  
  <pre><code>C:\Documents and Settings\Joe King\My Documents\418.85A Java\Project5&gt;javac Project5.java
  Project5.java:408: error: variable boatNames might not have been initialized
              boatArray1 = new Boat[boatNames.length];
                                    ^
  1 error
  </code></pre>
  
  <p>The problem is my boatNames array is in a try/catch block and I think this is isolating it from the rest of the code.  How can I get the boatNames array out of the try/catch block?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>class Project5{
  
  public static void main(String[] args){
  
      String[] boatNames;
      Boat[] boatArray1;
      Boat[] boatArray2;
      String result = " "; 
      String name = null;
      char firstChar;
      char firstLetter;
      char secondLetter;
      int totalRead;
      int i;
      int j;
      int k;
      int l;
      int m;
  
      Path inPath = Paths.get("C:/Documents and Settings/Joe King/My Documents/418.85A Java/Projects/Input").resolve("Boat Names.txt");
  
      if(!Files.exists(inPath)){
  
          System.out.println(inPath + " does not exist.  Terminating the program.");
          System.exit(1);
  
      }
  
      try(BufferedReader fileIn = Files.newBufferedReader(inPath, Charset.forName("UTF-16"))){
  
          totalRead = 0;
  
          while(fileIn.readLine() != null){
  
              name = fileIn.readLine();
              ++totalRead;
              name = null;
  
          }
  
          boatNames = new String[totalRead];
  
          for(i = 0 ; i &lt; boatNames.length ; ++i){
  
              name = fileIn.readLine();
              boatNames[i] = name;
              name = null;
  
          }
  
      }catch(IOException e){
  
          System.err.println("Error writing file: " + inPath);
          e.printStackTrace();
  
      }   
  
      Path outPath = Paths.get("C:/Documents and Settings/Joe King/My Documents/418.85A Java/Projects/Output").resolve("Fleet Registry.txt");
  
      try{
  
          Files.createDirectories(outPath.getParent());
  
      }catch(IOException e){
  
          System.err.println("Error creating directory: " + outPath.getParent());
          e.printStackTrace();
          System.exit(1);
  
      }
  
      boatArray1 = new Boat[boatNames.length];
  
      if(boatNames.length &gt; 0){
  
          try(BufferedWriter fileOut = Files.newBufferedWriter(outPath, Charset.forName("UTF-16"))){
  
              for(j = 0 ; j &lt; boatNames.length ; j++){
  
                  String delimiters = "[. ,]";
                  int limit = -1;
  
                  String[]tokens = boatNames[j].split(delimiters, limit);
  
                  for(k = 0 ; k &lt; tokens.length ; ++k){
  
                      firstChar = tokens[k].charAt(0);
                      firstChar = Character.toUpperCase(firstChar);
                      char[] tokenArray = tokens[k].toCharArray();                            
                      String text = new String(tokenArray, 1, (tokenArray.length - 1) );                          
                      tokens[k] = firstChar + text;                   
                      result = result + tokens[k] + " ";
  
                      if(k != tokens.length - 1){
  
                          continue;
  
                      }else{
  
                          result = result.trim();
                          boatNames[k] = result;
                          result = " ";
  
                      }
                  }       
  
                  firstLetter = boatNames[j].charAt(0);
  
                  if((firstLetter == 'B') || (firstLetter == 'C') || (firstLetter == 'N')){
  
                      boatArray1[j] = new RaceBoat();
  
  
                  }else{
  
                      boatArray1[j] = new SailBoat();
  
                  }
  
                  boatArray1[j].christenBoat(boatNames[j]);               
  
              }
  
              System.out.println("\n");
  
              for(l = 0 ; l &lt; boatNames.length ; ++l){            
  
                  secondLetter = Character.toUpperCase(boatNames[l].charAt(1));
  
                  if((secondLetter == 'A') || (secondLetter == 'E')){
  
                      if(l &gt; 0){
  
                          fileOut.newLine();
                      }                       
                      fileOut.write(boatArray1[l].goFast());
  
                  }else{
  
                      if(l &gt; 0){
  
                          fileOut.newLine();
                      }               
                      fileOut.write(boatArray1[l].goSlow());
  
                  }           
  
                  fileOut.newLine();
                  fileOut.write(boatArray1[l].launchBoat());
                  fileOut.newLine();
                  fileOut.write(boatArray1[l].whatIsBoatState());
                  fileOut.newLine();
  
              }
  
              boatArray2 = new Boat[3];
  
              boatArray2[0] = new SailBoat();
              boatArray2[1] = new RaceBoat("Endurance", true);
              boatArray2[2] = new RaceBoat(false);
  
              for(m = 0 ; m &lt; boatArray2.length ; ++m){
  
                  fileOut.newLine();
                  fileOut.write(boatArray2[m].toString());
                  fileOut.newLine();
                  fileOut.write(boatArray2[m].launchBoat());
                  fileOut.newLine();
                  fileOut.write(boatArray2[m].whatIsBoatState());
                  fileOut.newLine();
  
              }
  
              fileOut.newLine();
              fileOut.write("There are " + Boat.boatCount + " boats in the fleet this morning.");
  
  
          }catch(IOException e){
  
          System.err.println("Error writing outPath: " + outPath);
          e.printStackTrace();
  
          }
  
      }else{
  
          System.out.println("\n\n\nArgh!... you forgot to enter ship names scalawag!" +
              "\n\n\n\tPlease try again!");
  
      }
  
      System.out.println("\nThe Fleet Registry is completed, press ENTER to continue.\n");
  
      try{
  
          System.in.read();
  
      } catch(IOException e){
  
          return;
      }
  }
  }
  </code></pre>
  
  <p>Thank you everyone, I've entered the following:</p>
  
  <pre><code>    if(boatNames != null){
  
          boatArray1 = new Boat[boatNames.length];
  
          if(boatNames.length &gt; 0){
  
              try(BufferedWriter fileOut = Files.newBufferedWriter(outPath, Charset.forName("UTF-16"))){
  
                  for(j = 0 ; j &lt; boatNames.length ; j++){
  
                      String delimiters = "[. ,]";
                      int limit = -1;
  
                      String[]tokens = boatNames[j].split(delimiters, limit);
  
                      for(k = 0 ; k &lt; tokens.length ; ++k){
  
                          firstChar = tokens[k].charAt(0);
                          firstChar = Character.toUpperCase(firstChar);
                          char[] tokenArray = tokens[k].toCharArray();                            
                          String text = new String(tokenArray, 1, (tokenArray.length - 1) );                          
                          tokens[k] = firstChar + text;                   
                          result = result + tokens[k] + " ";
  
                          if(k != tokens.length - 1){
  
                              continue;
  
                          }else{
  
                              result = result.trim();
                              boatNames[k] = result;
                              result = " ";
  
                          }
                      }       
  
                      firstLetter = boatNames[j].charAt(0);
  
                      if((firstLetter == 'B') || (firstLetter == 'C') || (firstLetter == 'N')){
  
                          boatArray1[j] = new RaceBoat();
  
  
                      }else{
  
                          boatArray1[j] = new SailBoat();
  
                      }
  
                      boatArray1[j].christenBoat(boatNames[j]);               
  
                  }
  
                  System.out.println("\n");
  
                  for(l = 0 ; l &lt; boatNames.length ; ++l){            
  
                      secondLetter = Character.toUpperCase(boatNames[l].charAt(1));
  
                      if((secondLetter == 'A') || (secondLetter == 'E')){
  
                          if(l &gt; 0){
  
                              fileOut.newLine();
                          }
  
                          fileOut.write(boatArray1[l].goFast());
  
                      }else{
  
                          if(l &gt; 0){
  
                              fileOut.newLine();
                          }
  
                          fileOut.write(boatArray1[l].goSlow());
  
                      }           
  
                      fileOut.newLine();
                      fileOut.write(boatArray1[l].launchBoat());
                      fileOut.newLine();
                      fileOut.write(boatArray1[l].whatIsBoatState());
                      fileOut.newLine();
  
                  }
  
                  boatArray2 = new Boat[3];
  
                  boatArray2[0] = new SailBoat();
                  boatArray2[1] = new RaceBoat("Endurance", true);
                  boatArray2[2] = new RaceBoat(false);
  
                  for(m = 0 ; m &lt; boatArray2.length ; ++m){
  
                      fileOut.newLine();
                      fileOut.write(boatArray2[m].toString());
                      fileOut.newLine();
                      fileOut.write(boatArray2[m].launchBoat());
                      fileOut.newLine();
                      fileOut.write(boatArray2[m].whatIsBoatState());
                      fileOut.newLine();
  
                  }
  
                  fileOut.newLine();
                  fileOut.write("There are " + Boat.boatCount + " boats in the fleet this morning.");
  
  
              }catch(IOException e){
  
              System.err.println("Error writing outPath: " + outPath);
              e.printStackTrace();
  
              }
  
          }else{
  
              System.out.println("\n\n\nArgh!... you forgot to enter ship names scalawag!" +
                  "\n\n\n\tPlease try again!");
  
          }
  
          System.out.println("\nThe Fleet Registry is completed, press ENTER to continue.\n");
  
          try{
  
              System.in.read();
  
          } catch(IOException e){
  
              return;
          }
      }
  </code></pre>
  
  <p>Now I'm getting the following error:</p>
  
  <pre><code>C:\Documents and Settings\Joe King\My Documents\418.85A Java\Project5&gt;java Project5
  Exception in thread "main" java.lang.NullPointerException
         at Project5.main(Project5.java:424)
  </code></pre>
  
  <p>This doesn't make sense to me, in order to get to line 424 the boatNames array cannot be null because the length of boatNames was used several times before line 424.  What am I missing?</p>
  
  <p>Thanks</p>