tags:

views:

89

answers:

4

 public boolean addPoint(Point p){
  points.add(p);
  return points.add(p);
        extremes();
 }

Alright so when I run this code the main class calls addPoint and passes it a Point, however when it gets to the line "points.add(p);" it gives me a "java.lang.NullPointerException" error. FYI: "points" is an arrayList.

Also on a side note, am I using the "return points.add(p);" right to return a boolean value? And on another side note I don't seem to be calling "extremes();" right, as I get an Unreachable code error.

Thanks for all the help! :)

+2  A: 

You are correct that extremes() will never get called. Either reorder the last two lines, or if the order is important then do this:

public boolean addPoint(Point p){
     boolean result = points.add(p);
     extremes();
     return result;
}

I'd also recommend that you format your code neatly so that it is easier to read, and use more descriptive names than for example extremes. It is not clear whether this method is searching for the extremes and returning them or if you are calling it for some side-effect. If it is the former then it is strange that you are not actually using the return value - perhaps this is another error in your code.

Mark Byers
+2  A: 

You get an unreachable code error because your call to extremes() is AFTER the return. Your method will always return and NEVER get there.

I think what you want is

 public boolean addPoint (Point p) {
     boolean didAdd = points.add(p);
     extremes();
     return didAdd;
 }

no need to call add twice.

hvgotcodes
Will it still put my point in my arrayList if I only run that return statement?
Adam
@adam, I dont understand your question. You call the method addPoint, all the statements in the the method execute. You cannot 'only run that return statement'
hvgotcodes
@hvgotcodes: there is no need to use Object type here (i.e. Boolean instead of boolean).
Roman
@roman, sure, fixed
hvgotcodes
+6  A: 

You probably forgot to initialize your ArrayList (i.e. points). Just do something like this:

class YourClass {

   private final List<Point> points = new ArrayList<Point>();

   public boolean addPoint (Point p) {
      boolean result = points.add(p);
      extremes();
      return result;
   }
}
Roman
+1 for answering the heart of the question wrt the NRE
Kirk Woll
Well at the begining I state: "private ArrayList<Point> points;" is that enough?
Adam
Wait just kidding, I see it. Thanks!
Adam
Added the call to `extremes()` to make this a complete answer +1
Erick Robertson
+1  A: 

Your NullPointerException is because points is null - did you new it in your constructor?

Unless it's your intent to stick the point in your list twice, you don't want to call points.add(p) twice. What you probably want to do is:

public boolean addPoint (Point p) {
  boolean result = points.add(p);
  extremes();
  return result;
}
Sbodd
No I didn't, I run the statement "private ArrayList<Point> points;" up at the top, however I can't figure out how to put the "new" part only in the constructor without just putting "private ArrayList<Point> points = new ArrayList<Point>();" up at the top. Does that make sense?
Adam
Yes, you can do this. You can also put `private ArrayList<Point> points;` in the declaration and `this.points = new ArrayList<Point>();' in the constructor.
Erick Robertson