I tried to do a simple class that reads out the contents of a online .txt and stores it in a DataClass.
Somehow I get an Nullpointer exception in the first variable assign in the while loop, although line is printed out as being "4".
Anyone know the Problem in my code?
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;
}
}
Content of the .txt is following:
4
2
2016
22
14
49.5999984741
19.2999992371
4
2
2016
22
14
49.5999984741
19.2999992371
4
2
2016
...