-2

我在这个 Point3f 数组上有一个空指针异常错误,不知道为什么。它发生在选择位置 [0] 的第一遍。有人可以解释一下吗?谢谢。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.vecmath.Point3f;
public class Starter {

static int NoOfSides = 4;

public static void main(String[] args) {

    Point3f[] pointArray = new Point3f[NoOfSides];

    for(int i=0; i<NoOfSides; i++)
    {
        System.out.println(i+1+". Input x value: ");
        pointArray[i].x=readConsole();      //  Pointer Exception Here

        System.out.println(i+1+". Input y value: ");
        pointArray[i].y=readConsole();

        pointArray[i].z=(float)0.0;
    }
    //  Static call to work out area of polygon
    System.out.println("Area: "+PolyAreaTry.CalArea(pointArray));
}
public static float readConsole()
{
    String s = null;
    try
    {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        s = bufferRead.readLine();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    float f = (float)Float.parseFloat(s);

    return f;
}

}

4

1 回答 1

6

默认情况下,Object数组的元素是。null在尝试访问其字段之前初始化数组本身中的元素

for (int i = 0; i < noOfSides; i++) {
   pointArray[i] = new Point3f(); 
   ...
}
于 2013-09-03T23:32:46.383 回答