问个有关数组的错误
//Algo 计算描点所需数组array[][]
public class DDA
{
int array[][] = new int[1000][];
public void DDAline(int xa,int ya,int xb,int yb)
{
int x0 ,y0, x1, y1 ;
int x , dx, dy;
float k,y;
x0 = xa;y0 = ya;x1 = xb;y1 = yb;
dx = x1 - x0;dy = y1 - y0;
k = (float)dy/(float)dx;
y = y0;
int i = 0;
for (x = x0; x <= x1; x++)
{
System.out.print(x+ " ");
//array[i][0] = x;
//System.out.print(array[i][0]);
int z=(int) (y+0.5); System.out.println(z);
//array[i][1] = z;
i++;
y+=k;
}
}
}
class A
{
public static void main(String arg[])
{
DDA dda = new DDA();
dda.DDAline(1,1,10,5);
}
}
把注释部分去掉,即给数组附值后输出就会在run的时候出错(编译时不出错)
Exception in thread "main "
java.lang.NullPointerException at DDA.DDAline(DDA.java:22)
at A.main(DDA.java:63)
是什么问题呢?
------解决方案--------------------//array[i][0] = x;
//System.out.print(array[i][0]);
----------------------------------------
修改为:
array[i]=new int[2];
array[i][0] = x;
System.out.print(array[i][0]);
------解决方案--------------------1>把 public class DDA{ }改为 class DDA{ }
而把 class A{ } 改为 public class A{ }
2>把文件名记得也改为A.java
这样改后程序可以运行,不知道是不是你要的结果!
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
/*运行结果*/
------解决方案--------------------main()函数要放在public类中的
不然就出错