java - NullPointerException 错误,有人知道吗?
问题描述
这是我要查看的代码,我已经与它斗争了一个小时,我认为这是扼杀我的进步的课程,底部显示的错误中的 30 是否代表第 30 行中的错误,但是从那以后是while循环是否意味着整个循环有缺陷?我正在努力解决这个问题,但没有运气。
import java.util.Scanner;
import java.io.File;
import java.util.regex.Pattern;
public class ContributorManager {
public static void main(String [] args) {
Scanner inputFile = null;
String name = null;
String city = null;
String country = null;
String phone = null;
double contribution = 0;
int id = 0;
Contributor c = null;
TheStack stack = new TheStack();
Node1 node = null;
//open contributors file
try {
inputFile = new Scanner(new File("contributors.csv"));
inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));
}
catch (Exception e) {
System.err.println("Error opening file.");
}
//create contributors object for each row, and add to the stack
while (inputFile.hasNext()) {
name = inputFile.next();
city = inputFile.next();
country = inputFile.next();
phone = inputFile.next();
contribution = inputFile.nextDouble();
id = inputFile.nextInt();
inputFile.nextLine(); //advance to the next line
c = new Contributor(name, city, country, phone, contribution, id);
node = new Node1(c); //create a node using the contributor object
stack.push(node); //add the node to the top of the stack
}
stack.pop(); //remove the last node from the top of the stack
stack.print(); //print the contents of the entire stack
}
}
这是我所看到的错误,我是新手,所以我没有最好的能力来发现自己的错误,因为它们经常发生,谢谢。
Error opening file.
Exception in thread "main" java.lang.NullPointerException
at ip1_second_try/Module.ContributorManager.main(ContributorManager.java:30)
解决方案
你得到 NullPointerException 因为有一个异常读取文件,inputFile
仍然null
是inputFile.hasNext()
你正在尝试做的null.hasNext()
。
将代码移动到 try 中的 try-catch 之后,这样如果您还没有打开文件,您将不会执行其余代码:
//open contributors file
try {
inputFile = new Scanner(new File("contributors.csv"));
inputFile.useDelimiter(Pattern.compile("(\\n)|(\\r)|,"));
//create contributors object for each row, and add to the stack
while (inputFile.hasNext()) {
name = inputFile.next();
city = inputFile.next();
country = inputFile.next();
phone = inputFile.next();
contribution = inputFile.nextDouble();
id = inputFile.nextInt();
inputFile.nextLine(); //advance to the next line
c = new Contributor(name, city, country, phone, contribution, id);
node = new Node1(c); //create a node using the contributor object
stack.push(node); //add the node to the top of the stack
}
stack.pop(); //remove the last node from the top of the stack
stack.print(); //print the contents of the entire stack
} catch (Exception e) {
System.err.println("Error opening file.");
}
注意:您不应该捕获父异常Exception
,因为您将无法区分来自Scanner
代码的其他部分或其他部分的异常。
推荐阅读
- mysql - 将数据从 mysql 获取到 Apache Spark (scala) 时出错
- cryptography - 椭圆曲线中带负数的标量乘法
- r - 如何向 plotly 添加第三个值,以便在 ggplotly 上显示行/观察 ID 变量
- amazon-web-services - aws lambda 在 s3 上写入文件 - javascript
- java - Grafana 正则表达式
- php - 有没有办法将类内部的变量传递给php外部的函数?
- c++ - MOCK_DECLARE_METHOD 和 MOCK_METHOD 有什么区别
- typescript - Typescript:是否可以为类型联合添加扩展方法,例如 T | 空 | 不明确的?
- java - Java.sql.date 存储以前的日期而不是给定的日期
- amazon-web-services - AWS:在多个无服务器应用程序中重用 amazon Cognito Userpool