I am writing an android application, I use Aysnctask to get the current weather using openWeatherAPI. but it gives me a NullPointerException at this line :
results[0]=t;
this is the Aysnctask class I wrote:
private class GetWeather extends AsyncTask<String[], Void, String[]>{
@Override
protected String[] doInBackground(String... params) {
// TODO Auto-generated method stub
try {
params[0]=currentLatitude;
params[1]=currentLongitude;
String Url1="http://api.openweathermap.org/data/2.5/weather?lat="+currentLatitude+"&lon="+currentLongitude;
s1=getJson(Url1);
//Toast.makeText(getApplicationContext(), "the ss = "+s1, Toast.LENGTH_LONG).show();
Log.d("yes","yes");
if(s1!=null){
JSONObject jObj1 = new JSONObject(s1);
Log.d("jsonOOO","jsonOOO");
Tem= jObj1.getJSONObject("main").getDouble("temp");
Log.d("temmm","temmm="+String.valueOf(Tem));
pressure=jObj1.getJSONObject("main").getDouble("pressure");
humm=jObj1.getJSONObject("main").getDouble("humidity");
wind=jObj1.getJSONObject("wind").getDouble("speed");
desc=jObj1.getJSONObject("weather").getDouble("description");
double tem_c=Tem-273.15;
Log.d("tem11","tem11="+String.valueOf(tem_c));
String t=Double.toString(tem_c);
String t=String.valueOf(tem_c);
Log.d("tem22","tem22="+t);
results[0]=t;
Log.d("results[0]=","results[0]"+results[0]);
results[1]=String.valueOf(pressure);
results[2]=String.valueOf(humm);
results[3]=String.valueOf(wind);
results[4]=String.valueOf(desc);
}
} catch (Exception e) {
// TODO: handle exception
}
return results;
}//do in background
@Override
protected void onPostExecute(String[] results) {
temp.setText(results[0]+"°C");
hum.setText(results[1]+ "%");
press.setText(results[2]+ " hPa");
windSpeed.setText(results[3]+ " mps");
condDescr.setText(results[4]);
}
}
the value of temp,pressure, etc.. is not null. I tried to print them in logCat and they were printed.