title: Putting a Battleship' position into an array [java]
body: <p>I've been having this slight issue with my Battleship program. Within my tester I have created a boat object [a destroyer (holds 2 spots)that is vertical with the starting position of pos1] as well as some testers:</p>
  
  <pre><code>public class BoatTester {
  
      public static void main(String[] args) {
          Position pos1 = new Position(4, 5);
          Position pos2 = new Position(3, 5);
          Boat ship1 = new Boat("Destroyer", pos1, "vertical");
  
          //Hits the starting position as it is a definite hit
          System.out.println("Hitting...");
          ship1.hit(pos1);
  
          System.out.println("Starting Position Hit? " + ship1.isHit(pos1)); //returns true   
  
          System.out.println("Ship Sunk? " + ship1.sunk(pos2)); //false, as the destroy has 2 spots
          System.out.println("");
          System.out.println("Hitting Other Position(s)...");
  
          //This doesn't work
          ship1.onBoat(pos2);
  
  }}
  </code></pre>
  
  <p>In my actual boat class(which has the code to run all this), the constructor takes the type of boat it is, the starting position (position one) and the orientation, then it fills an array with Position objects to the correct amount of spots. </p>
  
  <p>*Note that the Position obj, just has methods to give the x and y positions of the boat. </p>
  
  <p>**Also note that the code will set the array position [0] as the starting position (position one) and generate from there on.</p>
  
  <pre><code>public class Boat {
      private String name;
      private Position location;
      private String orientation;
  
      private Position[] boat;
  
      public Boat(String ship, Position p, String orien){
          name = ship;
          location = p;
          orientation = orien;
          boat = new Position[size()];
          boat[0] = p;
          if(orientation.equals("Vertical"))  {
              for(int i = 1; i &lt; size(); i++) {
                  hold = new Position((location.rowIndex() + i), location.columnIndex());
                  boat[i] =  hold;
              }
          }//Just the flipped of vertical 
          else if(orientation.equals("Horizontal"))   {
              for(int i = 1; i &lt; size(); i++) {
                  hold = new Position(location.rowIndex(), (location.columnIndex() + i));
                  boat[i] =  hold;
              }
          }
  
      }
  </code></pre>
  
  <p>Now in this Boat class, when I call another method (not included) to see if the second position that I passed in from the tester is there (it's suppose to be there) the program crashes with an error saying that the array is out of bounds. What I don't understand is why there is no second position to the array, as it seems like my constructor isn't making the second Position obj. </p>
  
  <p>*Note I tried to follow etiquette and keep this as concise as possible, and the full code can be found here: <a href="https://docs.google.com/document/d/16Hp7Uu_-UZE3PwfQPurNIxl7CAzH8t2nEhUyB6Te_qc/edit?usp=sharing" rel="nofollow">https://docs.google.com/document/d/16Hp7Uu_-UZE3PwfQPurNIxl7CAzH8t2nEhUyB6Te_qc/edit?usp=sharing</a> </p>
  
  <p>**Note 2, I'm a student, and may not know the very intricate workings of java, so please explain like I'm five.</p>
  
  <p>Edit 1: My bad, here is the exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at Boat.onBoat(Boat.java:80)
      at BoatTester.main(BoatTester.java:15)
  </code></pre>
  
  <p>The one at Boat.onBoat is just my test of the boat[1] (second position) of the array, which shows there being nothing there.</p>
  
  <p>Edit 2: Here's the onBoat exception to clarify.</p>
  
  <pre><code>public boolean onBoat(Position spot){
          //Throws and exception here when I check the second index of the array
          System.out.println(boat[1].rowIndex()); //Specifically line 80
          System.out.println(boat[1].columnIndex());
  }
  </code></pre>