0

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
...
Markus Schmitz
  • 97
  • 3
  • 13

2 Answers2

0

The method you should be using is parseInt(). Try changing this line:

log.day = Integer.getInteger(line);

to this :

log.day = Integer.parseInt(line);
Nilay Ates
  • 115
  • 8
-1

Looks like the same as this issue:

Why does int num = Integer.getInteger("123") throw NullPointerException?

Basically: "Integer getInteger(String) doesn't do what you think it does"

Community
  • 1
  • 1
Robert
  • 1