title: Java “String index out of range”
body: <p>I wrote this function that would work prefectly but I got an exception:</p>
  
  <blockquote>
    <p>StringIndexOutOfBoundsException: String index out of range:"String index out of range: (length of the string)" </p>
  </blockquote>
  
  <p>This is the code:</p>
  
  <pre><code>private static String inverse(String a) {
      char[] invers = null;
      String f;
      for (int i = 0; i &lt; a.length(); i++) {
          invers[i] = a.charAt(a.length() - (i - 1));
      }
      f = new String(invers);
      return (f);
  }
  
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      String chaine = sc.nextLine();
      String ch = inverse(chaine);
      System.out.println("l'inv de" + args[0] + "est" + ch);
      if (!args[0].equals(ch))
          System.out.println(args[0] + "n'est pas palind");
      else
          System.out.println(args[0] + "est palind");
  }
  </code></pre>