0

The Title suggests, I am receiving a Null Pointer Exception in Android Studio when Parsing JSON information to a text view that is also inside of a Fragment.

Error Log:

   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.test.test.Model.OpenWeatherMap.getName()' on a null object reference
    at com.test.test.Tab1Fragment$GetWeather.onPostExecute(Tab1Fragment.java:184)
    at com.test.test.Tab1Fragment$GetWeather.onPostExecute(Tab1Fragment.java:150)
    at android.os.AsyncTask.finish(AsyncTask.java:695)
    at android.os.AsyncTask.access$600(AsyncTask.java:180)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

So I am receiving a Null Exception, however I do not believe the objects is Null?

Lines where the error occurs:

txtCity.setText(String.format("%s, %s",openWeatherMap.getName(), openWeatherMap.getSys().getCountry()));

        txtLastUpdate.setText(String.format("Refreshed: %s", Common.getDateNow()));
        txtDescription.setText(String.format("%s", openWeatherMap.getWeather().get(0).getDescription()));
        txtHumidity.setText(String.format("%d%%", openWeatherMap.getMain().getHumidity()));
        txtTime.setText(String.format("%s/%s", Common.unixTimeStampToDateTime(openWeatherMap.getSys().getSunrise()),Common.unixTimeStampToDateTime(openWeatherMap.getSys().getSunset())));
        txtCelcius.setText(String.format("%.2f °C", openWeatherMap.getMain().getTemp()));
        Picasso.with(getActivity())
                .load(Common.getImage(openWeatherMap.getWeather().get(0).getIcon()))
                .into(imageView);

My Model Class file:

    import java.util.List;

public class OpenWeatherMap {
    private Coord cood;
    private List<Weather> weather;
    private String base;
    private Main main;
    private Wind wind;
    private Rain rain;
    private Clouds clouds;
    private int dt;
    private Sys sys;
    private int id;
    private String name;
    private int cod;

    public OpenWeatherMap() {
    }

    public OpenWeatherMap(Coord cood, List<Weather> weatherList, String base, Main main, Wind wind, Rain rain, Clouds clouds, int dt, Sys sys, int id, String name, int cod) {
        this.cood = cood;
        this.weather = weatherList;
        this.base = base;
        this.main = main;
        this.wind = wind;
        this.rain = rain;
        this.clouds = clouds;
        this.dt = dt;
        this.sys = sys;
        this.id = id;
        this.name = name;
        this.cod = cod;
    }

    public Coord getCood() {
        return cood;
    }

    public void setCood(Coord cood) {
        this.cood = cood;
    }

    public List<Weather> getWeather() {
        return weather;
    }

    public void setWeather(List<Weather> weather) {
        this.weather = weather;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public Main getMain() {
        return main;
    }

    public void setMain(Main main) {
        this.main = main;
    }

    public Wind getWind() {
        return wind;
    }

    public void setWind(Wind wind) {
        this.wind = wind;
    }

    public Rain getRain() {
        return rain;
    }

    public void setRain(Rain rain) {
        this.rain = rain;
    }

    public Clouds getClouds() {
        return clouds;
    }

    public void setClouds(Clouds clouds) {
        this.clouds = clouds;
    }

    public int getDt() {
        return dt;
    }

    public void setDt(int dt) {
        this.dt = dt;
    }

    public Sys getSys() {
        return sys;
    }

    public void setSys(Sys sys) {
        this.sys = sys;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

So getName() Should not be sending back a Null Pointer Exception, is this correct? With the lines below the error line, will this also happen with them? I don't seem to be able to understand why I am getting this error?

I believe the information in the Model Class file should be sending back JSON Information to be parsed to the text view.

I am following a tutorial to learn Parsing JSON information to an application and I am also attempting to send the information to a Fragment View. It seems most answers and discussions I have found online discuss the above with no fragments, but I believe it is still possible.

  • 6
    Believing that is not null isn't the solution. Try to debug it or print a log. As per your code `openWeatherMap` is null – Rohit5k2 Sep 18 '18 at 11:27
  • Please give section of java code for json – seyed ali ziaei Sep 18 '18 at 11:30
  • you likely didn't initialize `openWeatherMap` variable – Vladyslav Matviienko Sep 18 '18 at 11:31
  • Where you initialized `OpenWeatherMap` ? It seems that object is null and the `getName` method can not be invoked. Try add break point to this line and check that this object is null or not – Shayan Tabatabaee Sep 18 '18 at 11:35
  • `Attempt to invoke virtual method 'java.lang.String com.test.test.Model.OpenWeatherMap.getName()' on a null object reference` just means doing something like "`null.getName()`". Your `openWeatherMap` is not initialized... – deHaar Sep 18 '18 at 11:36

0 Answers0