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 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!

weather- ermak0ff