Good day!

I make a test weather application and take the data in OpenWeatherApi. I am transferring data to AsyncTask and in the onPostExecute method I want to use GSON to create a new object based on them.

When I use GSON separately, everything works. Should I put it in onPostExecute - everything breaks down and gives out

lang.NullPointerException: println needs a message 

The whole error looks like this.

The last thing the program does is display the resulting string in JSON format in Log. The command is just registered in onPostExecute.

Tell me, please, what I am doing wrong - I've already looked at many similar examples and it seems like everything should work.

Below is my code:

 public class MainActivity extends AppCompatActivity { TextView textView; //создаю классы для размещения данных public class Weather { public Main main; public long id; public String name; public int code; public void Weather () { Main main = new Main(); } } public class Main { public float temp; public int humidity; public int pressure; public float tempMax; public float tempMin; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textView); //вызываю асинхронный поток и даю ему ссылку DownloadTask task = new DownloadTask(); task.execute("http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=json&units=metric&cnt=7&appid=42b48587e1a4e48a2e14c453899f9cc4"); } public class DownloadTask extends AsyncTask <String, Void, String> { @Override protected String doInBackground(String... params) { String result=""; URL url; HttpURLConnection connection; try { url = new URL(params[0]); connection = (HttpURLConnection) url.openConnection(); InputStream in = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(in); int data = reader.read(); while (data != -1){ char ch = (char) data; result += ch; data = reader.read(); } return result; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.i("Result", result); GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Weather weather = new Weather(); weather = gson.fromJson(result, Weather.class); Log.i("Name", weather.name); Log.i("ТЕмпература", String.valueOf((weather.main.temp - 30)*5/9)); textView.setText(String.valueOf((weather.main.temp - 30) * 5 / 9)); } } } 

Thank you for attention!

  • on which line exactly falls? - Android Android
  • one
    fields do not seem to have been initialized in weather - ermak0ff
  • you have the wrong class structure - Chaynik
  • @ ermak0ff - tried - did not help. When GSON ran separately - it did not require their initialization - Anton Volkov

2 answers 2

You have the wrong class structure, according to the screenshot in JSON you have the key of the city which is the parent for Weather . Add a class

 public class City { public Weather city; } 

Then GSON it in GSON instead of Weather

NPE error crashes when accessing weather.name

 Log.i("Name", weather.name); 

Again, because you have the wrong class structure.

  • Thank you - I didn’t pay attention - I have a link there to view the weather a few days in advance - so it returns not the information that will fill the class - inattention - it all worked) - Anton Volkov

You have written the same reason for the error:

NullPointerException: println needs a message

in the onPostExecute method, from which we can assume (after all, we don’t know what you have in the MainActivity.java:95 line) that the error in this line is:

 Log.i("Name", weather.name); 

The weather variable is initialized, but for its field name , apparently there is none, which leads to NPE .