Re: NullPointer Exception
Code:
Point[] p = new Point[corpsDestroyed];
corpsDestroyed has to be an int
Code:
p[k].x = (int)(s.charAt(j)-48);
what type is variable k?
There are too many unknowns to answer your question accurately. My suggestion would be post all the variables/methods/constructs that this statement uses, if your sure it's this snippet that's causing your problems.
Re: NullPointer Exception
As well as kolt007's comments please post the full exception message (it tells us what has gone wrong) and always post code in code tags.
Re: NullPointer Exception
Code:
import java.awt.Point;
public class BananaCorp {
public static double distance(Point p1, Point p2){
double sqX = (p2.x - p1.x)*(p2.x - p1.x);
double sqY = (p2.y - p1.y)*(p2.y - p1.y);
double sum = sqX + sqY;
double dist = Math.sqrt(sum);
return dist;
}
public static int index(String s){
int count=0,ind=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == ':')
count++;
if(count == 3){
ind = i; break;
}}return ind;
}
public static void getPoints(String s){
int count=0;
int corpsDestroyed=0;
int ind = index(s); /*index of the 3rd ":"*/
for(int i=0; i<s.length(); i++)
{
if(s.charAt(i) == ':')
count++;
if(count == 2)
{
if(ind == i+3){
corpsDestroyed = (s.charAt(i+1) - 48)*10 + (s.charAt(i+2) - 48); /*-48 to convert ascii keys*/
}
else corpsDestroyed = (s.charAt(i+1) - 48);
System.out.println(corpsDestroyed);
break;
}}
Point[] p = new Point[corpsDestroyed];
for(int j=ind+3,k=0; j<s.length(); j+=6,k++)
{
p[k].x = (int)(s.charAt(j)-48);
p[k].y = (int)(s.charAt(j+2)-48);
if(j == s.length() - 4)
break;
}
for(int l=0; l<corpsDestroyed; l++)
System.out.print("("+p[l].x+","+p[l].y+")"+"\t");
getDistances(p);
}
public static void getDistances(Point[] p)
{
int size = 0;
for(int k=0; k<p.length; k++)
size = size + k;
double[] d = new double[size];
for(int i=0; i<p.length; i++)
{
for(int j=i+1; j<p.length; j++)
d[i] = distance(p[i], p[j]);
}
for(int k=0; k<size; k++)
System.out.print("\n"+d[k]+"\t");
typeOfDistances(d);
}
public static void typeOfDistances(double[] d)
{
int count = 0;
for(int i=0; i<d.length; i++)
{
for(int j=1; j<d.length; j++)
{
if(d[i] == d[j])
continue;
else
count++;
}
}
System.out.println(count);
}
public static void equalDistances(double[] d)
{
}
public static void main(String[] args) {
// TODO code application logic here
getPoints("{6:7:14:{{2,1},{6,6},{4,2},{2,5},{2,6},{2,7},{3,4},{6,1},{6,2},{2,3},{6,3},{6,4},{6,5},{6,7}}");
}
}
Exception.....
Code:
14
Exception in thread "main" java.lang.NullPointerException
at banana.corp.BananaCorp.getPoints(BananaCorp.java:51)
at banana.corp.BananaCorp.main(BananaCorp.java:100)
Java Result: 1
code where problem exist....is the one i posted earlier....
Re: NullPointer Exception
The problem is you have created a Point array but haven't filled it with any Point objects.
You need to do something like:
Code:
Point[] p = new Point[corpsDestroyed];
for(int j=ind+3,k=0; j<s.length(); j+=6,k++)
{
p[k] = new Point((int)(s.charAt(j)-48), (int)(s.charAt(j+2)-48));
if(j == s.length() - 4)
break;
}
Re: NullPointer Exception
Quote:
The problem is you have created a Point array but haven't filled it with any Point objects.
but that's what i'm doing in the following code.....
Code:
p[k].x = (int)(s.charAt(j)-48);
p[k].y = (int)(s.charAt(j+2)-48);
this is also a correct assignment.....Right??
anyways thanks.....i'll try your code and reply......
Re: NullPointer Exception
Quote:
Originally Posted by
darkmaster
but that's what i'm doing in the following code.....
No, you are not doing what keang said. You created an array of Point with
Code:
Point[] p = new Point[corpsDestroyed];
but that's just an array with all its elements == null. You still need to set the elements you intend to use to reference an instance of Point.
In the for following that, you just attempt to use a Point element that is still null and that is why you get the NullPointerException, while if you used what keang suggested it would be creating a Point instance and at the same time assigning the values of x and y as you want.
Re: NullPointer Exception