NullPointerException:两个集合和一个 for 循环
在另一种方法 (ALGO_1) 中,我搜索
的元素并检查值 H_NAME 是否等于 main.c 中输入的值。当我尝试运行代码时,出现空指针异常。在尝试在代码片段中的每个 for 循环之后打印(使用 System.out.println 等)H_NAME 值时,我还会收到返回给我的 null 语句。
我相当确定该集合根本不存储扫描仪收集的数据。但是当我使用 size()
检查集合大小时,它的大小大约是正确的。
主要问题是:
- readBackground方法中的data.add在错误的地方?
- 该片段的结构是否错误?
当我使用 System.out.println 检查后台对象值名称、开始时间、增量等时,它们打印得很好。
for (Hydro hd: hydros) {
System.out.println(hd.H_NAME);
for (Background back : backgs) {
System.out.println(back.H_NAME);
if (back.H_NAME.equals(hydroName)) { //get error here
public static Collection<Background> readBackground(String url) throws IOException {
URL u = new URL(url);
InputStream is = u.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader b = new BufferedReader(isr);
String line ="";
Vector<Background> data = new Vector<Background>();
while ((line = b.readLine())!= null) {
Scanner s = new Scanner(line);
String name = s.next();
double starttime = Double.parseDouble(s.next());
double increment = Double.parseDouble(s.next());
double sum = 0;
double p = 0;
double nterms = 0;
while ((s.hasNextDouble())) {
p = Double.parseDouble(s.next());
nterms++;
sum += p;
}
double pbmean = sum/nterms;
Background SAMP = new Background(name, starttime, increment, pbmean);
data.add(SAMP);
}
return data;
}
In another method (ALGO_1) I search over elements of <background>
and check the value H_NAME equals a value entered in the main. When I attempt to run the code, I get a null pointer exception. Upon trying to print (with System.out.println
etc) the H_NAME value after each for loop in the snippet, I also get a null statement returned to me.
I am fairly certain that the collection is simply not storing the data gathered up by the Scanner. But when I check the collection size with size()
, it is about the right size.
Main questions are:
- from the readBackground method is the data.add in the wrong place?
- is the snippet simply structured wrongly?
When I use System.out.println
to check the Background object values name, start time, increment, etc., they print out fine.
for (Hydro hd: hydros) {
System.out.println(hd.H_NAME);
for (Background back : backgs) {
System.out.println(back.H_NAME);
if (back.H_NAME.equals(hydroName)) { //get error here
public static Collection<Background> readBackground(String url) throws IOException {
URL u = new URL(url);
InputStream is = u.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader b = new BufferedReader(isr);
String line ="";
Vector<Background> data = new Vector<Background>();
while ((line = b.readLine())!= null) {
Scanner s = new Scanner(line);
String name = s.next();
double starttime = Double.parseDouble(s.next());
double increment = Double.parseDouble(s.next());
double sum = 0;
double p = 0;
double nterms = 0;
while ((s.hasNextDouble())) {
p = Double.parseDouble(s.next());
nterms++;
sum += p;
}
double pbmean = sum/nterms;
Background SAMP = new Background(name, starttime, increment, pbmean);
data.add(SAMP);
}
return data;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在调用
back.H_NAME.equals(xxx)
时收到 NullPointerException (NPE),则可能意味着back
为 null 或back.H_NAME 为空。
您说当您打印
back.H_NAME
时,您会得到 null,因此这表明back.H_NAME
实际上是 null。您还没有向我们展示任何描述Background
类的代码,因此很难为您提供更多帮助。If you get a NullPointerException (NPE) when you call
back.H_NAME.equals(xxx)
, it probably means eitherback
is null orback.H_NAME
is null.You say that when you print
back.H_NAME
, you get null, so that suggests thatback.H_NAME
is actually null. You haven't shown us any code that describes the classBackground
, so it's hard to give you any more help.