-1

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.

Christophe Herreman
  • 15,895
  • 9
  • 58
  • 86
roa.tah
  • 547
  • 2
  • 11
  • 25

4 Answers4

1

The results array needs to be declared and initialized (probably as a local variable in the doInBackground(String) method):

String[] results = new String[5];

Justin Muller
  • 1,283
  • 13
  • 21
0

It seems like you did not initialize String[] results before you called it. Hence causing the NullPointerException.

cokeby190
  • 609
  • 7
  • 14
0

You have to initialize your String Array like this : String[] results = new String[5];

Arthur Rey
  • 2,990
  • 3
  • 19
  • 42
0

results must be null. Null-check and initialize as below:

            Log.d("tem22","tem22="+t);
            if (results == null ) { //null-check
                   results = new String[5];  //initialize
            }
            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);

To avoid such exceptions, it is better to configure your IDE to warn you about potential null de-referencing

(e.g in Eclipse -> Preferences -> Java -> Compiler -> Errors -> Warnings ->Null analysis)

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51