-4

I am implementing Graham Scan Algorithm for convex hull in Java.

I am getting this error while running the code. For input string: "10 18"

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Graham.SelectMin(Graham.java:110)
    at Graham.GrahamScan(Graham.java:78)
    at Graham.main(Graham.java:41)

Can anyone help me out to solve this error?

Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
S U
  • 5
  • 2

2 Answers2

1

java.lang.ArrayIndexOutOfBoundsException: 0

This means that you're trying to access an element of an empty array. (An array of size 0.)

You need to have a non-negative size of the array to be able to access element at index 0.

For reference, this code for instance, produces the same error:

int initialSize = 0;
int[] arr = new int[initialSize];
System.out.println(arr[0]);
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Yeah. Check your `SelectMin` method and before accessing the array, check if `arrayName.length > 0` before doing anything. I would also suggest that you change the first letter of the name of every one of your methods to lowercase. – Anthony Accioly May 20 '11 at 20:28
  • /** * * * */ public class Point { public int x; public int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Point(int x_,int y_){ x = x_; y = y_; } public String toString(){ StringBuffer sb = new StringBuffer(); sb.append(x); sb.append(","); sb.append(y); return sb.toString(); } } – S U May 20 '11 at 21:50
  • import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import java.io.*; – S U May 20 '11 at 21:55
  • public class Graham { private Point p[]; private int size; private List list = new ArrayList(); public static void main(String[] args) { List list2 = new ArrayList(); Graham g = new Graham(); BufferedReader br ; try{ br = new BufferedReader(new FileReader(args[0])); String buf = br.readLine(); while (buf!=null&&buf!=""){ StringTokenizer st = new StringTokenizer(buf,","); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); list2.add( new Point(x,y)); buf = br.readLine(); } – S U May 20 '11 at 21:56
  • }catch(Exception e){ System.out.println(e.getMessage()); } g.setSize(list2.size()); Point[] p = new Point[list2.size()]; Iterator it1 = list2.iterator(); int i=0; while(it1.hasNext()){ p[i++]= (Point)it1.next(); } – S U May 20 '11 at 21:56
  • g.setP(p); g.GrahamScan(); List list = g.getList(); Iterator it = list.iterator(); while(it.hasNext()){ Point point = (Point)it.next(); System.out.println(point); } } public List getList() { return list; } public void setList(List list) { this.list = list; } public Point[] getP() { return p; } public void setP(Point[] p) { this.p = p; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } – S U May 20 '11 at 21:57
  • boolean GrahamScan () { SelectMin(); SortPoints(); if( size > 2){ int stk[] = new int[size]; int top = 2; stk[0] = 0; stk[1] = 1; stk[2] = 2; for (int i = 3; i < size; i++) { while (Area(p[stk[top-1]], p[stk[top]], p[i]) < 0) top--; stk[++top] = i; } for (int i = 0; i <= top; i++) list.add(p[stk[i]]); }else{ for (int i=0;i< size;i++){ list.add(p[i]); } } return true; } – S U May 20 '11 at 21:58
  • static int Area (Point p0, Point p1, Point p2) { int dx1 = p1.x - p0.x, dy1 = p1.y - p0.y; int dx2 = p2.x - p0.x, dy2 = p2.y - p0.y; return dx1 * dy2 - dx2 * dy1; } void SelectMin () { int ym = p[0].y; int m = 0; for (int i = 1; i < size; i++) { if (ym > p[i].y || (ym == p[i].y && p[m].x > p[i].x)) { ym = p[i].y; m = i; } } SwapPoints(0, m); } – S U May 20 '11 at 21:59
  • // It Sorts the points void SortPoints () { for (int i = 1; i < size - 1; i++) for (int j = i + 1; j < size; j++) if (Area(p[0], p[i], p[j]) < 0) SwapPoints(i, j); } void SwapPoints (int i, int j) { Point tmp; tmp = p[i]; p[i] = p[j]; p[j] = tmp; } } – S U May 20 '11 at 21:59
  • I added Graham.java in # of comments 1 by 1. – S U May 20 '11 at 22:00
  • 2 2 and 3 3 o/p is 1 1 and 3 3 – S U May 20 '11 at 22:02
0

it means that on line 110 of Graham.java you are trying to access an array index that is either < 0 or that is > the length of your array, can't tell exactly which array without code.

Also Graham.SelectMin(Graham.java:110) shows that you are naming method calls in a non-idiomatic Java way. In Java method calls should be in lowerCamelCase, such as Graham.selectMin();

In Java UpperCamelCase is reserved for ClassNames.

  • This is my code : /** * * * */ public class Point { public int x; public int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Point(int x_,int y_){ x = x_; y = y_; } public String toString(){ StringBuffer sb = new StringBuffer(); sb.append(x); sb.append(","); sb.append(y); return sb.toString(); } } Also========================== – S U May 20 '11 at 21:38
  • file name: case1.txt1 1 2 2 3 3 – S U May 20 '11 at 21:45