Results 1 to 14 of 14
Thread: What is wrong with this code
- 10-30-2008, 05:11 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
What is wrong with this code
Hi... I have created an array of point objects. But after adding points if I try to retrieve data, I get NullPointer exception. Why is this so?
Java Code:class polygon { private int vert_count; private point vertice[] = new point[5]; polygon() { initialize(); } public void initialize() { vert_count=0; for (int i=0; i <vertice.length;i++) { vertice[i]=null; } } public void add(point p1) { vertice[++vert_count]=p1; } public int at(int index) { int at=0; at=index % vert_count; return at; } public void get_vertices() { int loc; int x = 0; int y=0; for (int i=vert_count;i<6;i++) { loc = [B]at(i)[/B]; x = vertice[loc].getX(); y=vertice[loc].getY(); System.out.printf("%nVertice = ("+x+","+y+")"); } } } // From Point class // public int getX() // { // return xpt; // } // public int getY() // { // return ypt; // } Main method { point p1 = new point(8,12); point p2 = new point(6,10); point p3 = new point(5,11); polygon result4 = new polygon(); result4.add(p1); result4.add(p2); result4.add(p3); System.out.printf("%nVertice count = "+result4.get_vert_count()); result4.get_vertices(); }
-
You appear to have an array of 5 points, you instantiate 3 of them, and then try to loop through 6 of them. I would guess that the NPE exception will get you first, but if it didn't an array index out of bounds would bite you next.
- 10-30-2008, 05:35 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
yes, but that is why I have the at function.
see if there are 3 elements, if I enter 3 it should the element at 0 position, 4-> 1 position etc. so if I ask for 5 it will return the last element
-
You may wish to post compilable code, else who knows how this thing is supposed to run.
-
One problem I see:
When does this increment? What implications does this have for vertice[0]?Java Code:vertice[++vert_count] = p1;
why not just use an arraylist?
- 10-30-2008, 05:51 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
Ive changed the code a bit... than the above
Java Code:class point {//Point class private int xpt,ypt; public point() {//Constructor xpt=0; ypt=0; } public point(int m, int n) {//Constructor xpt=m; ypt=n; } public int getX() { return xpt; } public int getY() { return ypt; } } class polygon { private int vert_count; private point vertice[] = new point[5]; polygon() { initialize(); } public int get_vert_count() { return vert_count; } public void get_vertices() { int loc; int x = 0; int y=0; for (int i=vert_count;i<6;i++) { loc = at(i); x = vertice[loc].getX(); y=vertice[loc].getY(); System.out.printf("%nVertice = ("+x+","+y+")"); } } public void initialize() { vert_count=0; for (int i=0; i <vertice.length;i++) { vertice[i]=null; } } public void add(point p1) { vertice[++vert_count]=p1; if (vert_count < 3) System.out.printf("%n Enter more than 3 vertices"); } public int at(int index) { int at=0; at=rangeCheck(index); return at; } private int rangeCheck(int index) { int location = index - vert_count; int at=0; int curr = 0; if (index <= (2*vert_count-1) && location >= 0) { at = curr + location; curr=at; } else if(index <= (2*vert_count-1) && location <0) { at = vert_count+location; curr=at; } else if(index > (2*vert_count-1) ) { System.out.printf("%nSize too large"); } else {} return at; } } public class test {// Main class public static void main (String[] args) { point p1 = new point(8,12); point p2 = new point(6,10); point p3 = new point(5,11); polygon result4 = new polygon(); result4.add(p1); result4.add(p2); result4.add(p3); System.out.printf("%nVertice count = "+result4.get_vert_count()); result4.get_vertices(); } }Last edited by rosh72851; 10-30-2008 at 07:19 AM.
- 10-30-2008, 07:29 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
For some reason the point is not getting added into the array, that is why Im getting a null pointer exception..
When I try to manually add the points in the class function itself, it displays the array successfully.
-
Again, what's the difference between ++x and x++?
- 10-30-2008, 12:12 PM #9
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
- 10-30-2008, 02:07 PM #10
The end results are the same. But what value is returned to the expression the the variable is used in. Read your textbook.
- 10-30-2008, 04:22 PM #11
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
that is exactly what Im saying the output is a Null pointer.
If I were to manually assign each element in the array, I get results. ie, if I were to call array elements using the method at, I see th results.
But not when Im adding values from the main method.
Cant figure whats wrong.
- 10-30-2008, 05:29 PM #12
Add some println() statements to you code to show the values of the pointers you are using.
Did you see the discussion about the difference between:
array[ix] // use ix with no change
array[++ix] // incr ix and use the new value
array[ix++] // use ix and incr after the usage
You are using the second of the above. What value does ix have before the usage, what value is returned by ix to use as an index and what is the value of ix after the usage. The answers for each of these questions is different.
- 10-31-2008, 01:46 AM #13
Member
- Join Date
- Sep 2008
- Posts
- 58
- Rep Power
- 0
See but the thing is if you notice how many slots are available in the array. It is a lot. So that is not the reason for Null pointer exception
-
Please re-read the answers given. We are saying nothing about the slots at all, and you may be missing the point. The point is, what is the difference between these two bits of code:
Java Code:int i = 0; int[] myArray = new int[10]; myArray[i++] = 4;
Shoot, here's a whole program for you to run and test. The array item initialized and set is not 0. If this were an array of Objects, the program would crash with a NPE if done incorrectly:Java Code:int i = 0; int[] myArray = new int[10]; myArray[++i] = 4;
Now do you see?Java Code:public class ArrayTest { public static void postIncrement() { int i = 0; int[] myArray = new int[3]; myArray[i++] = 4; for (int j = 0; j < myArray.length; j++) { System.out.println(myArray[j]); } } public static void preIncrement() { int i = 0; int[] myArray = new int[3]; myArray[++i] = 4; for (int j = 0; j < myArray.length; j++) { System.out.println(myArray[j]); } } public static void main(String[] args) { postIncrement(); System.out.println(); preIncrement(); } }Last edited by Fubarable; 10-31-2008 at 01:56 AM.
Similar Threads
-
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM -
Is there somethign wrong with this code?
By Soda in forum New To JavaReplies: 1Last Post: 12-08-2007, 04:46 PM -
Whats wrong with my code???
By Soda in forum New To JavaReplies: 2Last Post: 12-06-2007, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks