title: Swapping elements of an array in Java
body: <p>I'm trying to write a code that returns a new array which swaps every pair of elements. If there is an odd number of elements, the last element remains the same. </p>
  
  <p>Currently what I have is this which doesn't seem to be working at all. I keep returning a nullpointerexception error when trying to test it </p>
  
  <pre><code>public static String[] pairSwap(String[] inputArray) {
      String[] outputArray = new String[inputArray.length];
      for (int i = 0; i &lt; inputArray.length; i += 2) {
  
          inputArray[i] = outputArray[i + 1];
          inputArray[i + 1] = outputArray[i];
  
          if (inputArray.length % 2 != 0) {
              inputArray[inputArray.length] = outputArray[inputArray.length];
          }
      }
      return outputArray;
  }
  </code></pre>