title: Why did I get a NullPointerException when I did not use setter in main method?
body: <p>I have a question about using getters and setters, and null pointer exception.</p>
  
  <p>I basically called a simple function, <code>getSlope()</code> on a <code>Line</code> object, <code>l</code>, in my program which has <code>Line</code> and <code>Point</code> classes. </p>
  
  <p>When I called <code>l.getSlope()</code> with 
  <code>Point p1 = new Point(0.0, 3.0);</code>
  <code>Point p2 = new Point(5.0, 5.0);</code>
  <code>Line l = new Line(p1, p2);</code>, it gave me a null pointer exception. </p>
  
  <p>However, when I called <code>l.getSlope()</code> with 
  <code>Line l = new Line();</code>
  <code>l.setP1(0.0, 3.0);</code>
  <code>l.setP2(5.0, 5.0);</code>, it DID NOT give me a null pointer exception. </p>
  
  <p>Why is this? Thank you for your help. The code for <code>Point</code> and <code>Line</code> classes are posted below for your information. </p>
  
  <p><code>Point</code>:</p>
  
  <pre><code>public class Point {
      private double x;
      private double y;
  
      public Point() {
          this.x = 0.0;
          this.y = 0.0;
      }
      public Point(double x, double y) {
          this.x = x;
          this.y = y;
      }
      public void setX(double x) {
          this.x = x;
      }
      public void setY(double y) {
          this.y = y;
      }
      public double getX() {
          return this.x;
      }
      public double getY() {
          return this.y;
      }
      public String toString() {
          return "(" + this.x + ", " + this.y + ")";
      }
      public static void main(String[] args) {
          Point p = new Point(1.5, 3);
              System.out.println(p.toString());
      }
  }
  </code></pre>
  
  <p><code>Line</code>:</p>
  
  <pre><code>public class Line {
      private Point p1;
      private Point p2;
  
      public Line() {
          p1 = new Point(0,0);
          p2 = new Point(0,0);
      }
  
      public Line(Point p1, Point p2) {
          p1 = new Point(p1.getX(), p1.getY());
          p2 = new Point(p2.getX(), p2.getY());
      }
      public Point getP1() {
          return this.p1;
      }
      public Point getP2() {
          return this.p2;
      }
      public void setP1(double x, double y) {
          this.p1 = new Point (x, y);
      }
      public void setP2(double x, double y) {
          this.p2 = new Point(x, y);
      }
      public double getSlope() {
          return ((p2.getY() - p1.getY()) / (p2.getX() - p1.getX()));
      }
      public static void main(String[] args) {
  //      Point p1 = new Point(0.0, 3.0);
  //      Point p2 = new Point(5.0, 5.0);
  //      Line l = new Line(p1, p2);
  //      System.out.println(l.getSlope()); // These 4 lines produced errors
          Line l = new Line();
          l.setP1(0.0, 3.0);
          l.setP2(5.0, 5.0);
          System.out.println(l.getSlope());  // These 4 lines worked!
      }
  }
  </code></pre>