Results 1 to 6 of 6
- 12-01-2010, 09:57 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
The null message is driving me crazy
Hey , I am writing a code at the moment that represents a polygon.
In the following method: addPolygon I'm trying to set a new point with the x and y cordinates to the polygon and place it in the available place, which hasn't got a point already.
If the placement was done successfully return true and if all places are taken return false.
I am getting a null compiler error.
Does anyone know where I went wrong?
class Polygon{
private Point _vertices [];
private int _noOfVertices = 0;
final int MAX = 10;
public Polygon (){
Point _vertices [] = new Point [MAX];
}
public boolean addVertex (int x,int y){
Point pnt = new Point (x,y);
for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);
if (_vertices [_noOfVertices] ==null && _noOfVertices <MAX)
_vertices [_noOfVertices] = pnt;
if (_noOfVertices<MAX)
return true;
return false;
}
Thank you for your help.
- 12-01-2010, 10:37 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
The compiler messages are generally very helpful. What is it?: copy and post the entire thing and indicate which line of your code it is referring to.
- 12-02-2010, 11:09 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
I got this mesage
java.lang.NullPointerException
at Polygon.addVertex(Polygon.java:14)
at Tester.main(Tester.java:6)
- 12-02-2010, 03:14 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
create an instance of private _vertices it will be fine.
private Point _vertices []; is being referenced in the add method and it has no instance to it.
- 12-02-2010, 03:21 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 59
- Rep Power
- 0
thank you that's solved!
now I'm trying to get a false return if the number of vertices is not less than the MAX variable, for some reason it still thinks of it as true...
- 12-02-2010, 07:03 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Look closely at line 14 to try and see what is null when it shouldn't be.
You don't say, but I am guessing that line is
Java Code:for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);
The only candidate for being null here is the array _vertices. It might be a good idea to check (and if there were other things being dereferenced with the "dot" operator, then you would have to check)
Java Code:System.out.println("About to start the for loop _vertices=" + _vertices); for (_noOfVertices=0;_vertices [_noOfVertices] !=null && _noOfVertices<MAX; _noOfVertices++);
If it turns out that _vertices is null then you have to ask yourself why. In particular where did you think you had initialised _vertices? And why did that not happen?
Similar Threads
-
image does not refresh-driving me crazy
By jambon in forum AWT / SwingReplies: 1Last Post: 04-09-2010, 04:25 PM -
A crazy gui match
By amarenash23 in forum New To JavaReplies: 8Last Post: 12-30-2009, 03:39 PM -
THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
By syntrax in forum New To JavaReplies: 2Last Post: 12-18-2009, 04:27 AM -
Loop driving me loopy!!!!!
By soc86 in forum New To JavaReplies: 8Last Post: 01-16-2009, 01:00 AM -
Errors driving me crazy! although compiles fine
By irishsea2828 in forum New To JavaReplies: 1Last Post: 04-08-2008, 03:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks