【发布时间】:2012-11-10 23:17:27
【问题描述】:
我正在尝试使用 opencsv (http://opencsv.sourceforge.net/)。 opencsv 的下载中包含示例。以下是他们创建动态数组示例的摘录:
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
}
它读取的CSV文件如下:
Joe Demo,"2 Demo Street, Demoville, Australia. 2615",joe@someaddress.com
Jim Sample,"3 Sample Street, Sampleville, Australia. 2615",jim@sample.com
Jack Example,"1 Example Street, Exampleville, Australia. 2615",jack@example.com
如果我将 println 语句移到 while 循环之外,我会在 Eclipse 中收到错误:“空指针访问:变量 nextLine 在此位置只能为空。”
我的猜测是 nextLine 有一个指针当前指向它的最后一个位置或超过它的最后一个位置。我想我的问题是,我如何控制那个指针?
【问题讨论】:
-
不,你误解了这里的错误。由于您的循环条件是一直持续到
nextLine == null,因此之后您可以保证它将是null,这就是Eclipse 抱怨的原因。您必须在循环中使用变量或更改您的条件。
标签: java arrays pointers dynamic