我尝试做一个简单的类,它读出在线.txt的内容并将其存储在DataClass中。
不知怎的,我在while循环中的第一个变量assign中得到一个Nullpointer异常,尽管line打印为“4”。
我的代码中有人知道问题吗?
public class GetFile {
public static List<Log> getFile(){
URL url = null;
List<Log> list = new ArrayList<Log>();
try {
url = new URL("http://test.net/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
Log log = new Log();
System.out.println("Line is: " + line);
**-->log.day = Integer.getInteger(line);<--**
line = in.readLine();
log.month = Integer.getInteger(line);
line = in.readLine();
log.year = Integer.getInteger(line);
line = in.readLine();
log.hour = Integer.getInteger(line);
line = in.readLine();
log.minute = Integer.getInteger(line);
line = in.readLine();
log.hum = Float.parseFloat(line);
line = in.readLine();
log.temp = Float.parseFloat(line);
list.add(log);
System.out.println("Item added");
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
return list;
}
}
.txt的内容如下:
4
2
2016
22
14
49.5999984741
19.2999992371
4
2
2016
22
14
49.5999984741
19.2999992371
4
2
2016
...
答案 0 :(得分:0)
您应该使用的方法是parseInt()。尝试更改此行:
log.day = Integer.getInteger(line);
到此:
log.day = Integer.parseInt(line);
答案 1 :(得分:-1)
看起来与此问题相同:
Why does int num = Integer.getInteger("123") throw NullPointerException?
基本上:&#34;整数getInteger(String)没有做你认为它做的事情&#34;