0
votes

I am getting images from URL given in JSON Array. I have parsed whole data and sucessfully taken the data to next activity but when I am trying to decode the URL using Bitmap then it showing Null Pointer Exception at onPostExecute method.

The Result parameter that passed in OnPostExecute method returns stores the value "android.graphics.Bitmap@418aad30" and this I am getting only if I am not adding the statement to set the Bitmap to Image View i.e. "img.setImageBitmap(newBitmap);".

My Java File is:::

package com.ourcast.pocketweather;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class DisplayWeathernew extends Activity {

    String city, date, maximumTemp, minimumTemp, description, weatherImageUrl;
    ImageView img;
    ListView weatherList;
    List <Bean> bean;
    Bitmap myBitmap, newBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_weather);

        img = (ImageView) findViewById(R.id.imgweather);
        weatherList = (ListView) findViewById(R.id.lvWeather);

        for(int i=0; i<WeatherHome.arrayList.size(); i++)
        {
            city = WeatherHome.arrayList.get(i).getCity(); //WeatherHome.arrayList.get(index).getCity();
            date = WeatherHome.arrayList.get(i).getDate();
            maximumTemp = WeatherHome.arrayList.get(i).getMaximumTemprature();
            minimumTemp = WeatherHome.arrayList.get(i).getMinimumTemprature();
            description = WeatherHome.arrayList.get(i).getDescription();
            weatherImageUrl = WeatherHome.arrayList.get(i).getImageUrl();

            Toast.makeText(this, "City "+city+" Date "+date+" Minimum "+minimumTemp+" Maximum "+maximumTemp+" Desc "+description+" URL "+weatherImageUrl, Toast.LENGTH_LONG).show();
        }


        new ImageDownload().execute();



        /*Log.i("TAG", "FIIIIIIIIIIINNNNNNNNNNNNNNNNNEEEEEEEEEEEEEE");
        //bean.add(new Bean("Hello", "Hei", "Heya", "Hola", "Worked!!"));
        Log.i("TAG", "FIII");

        CustomBaseAdapter baseAdapter = new CustomBaseAdapter(this, bean);
        weatherList.setAdapter(baseAdapter);*/
    }

private class ImageDownload extends AsyncTask<String, Void, Bitmap>{

    protected Bitmap doInBackground(String... arg0){

        try{
            Log.e("src",weatherImageUrl);
            URL url = new URL(weatherImageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);

            Log.e("Bitmap","returned");
            return myBitmap;
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }

    }

    protected void onPostExecute(Bitmap result){

        Toast.makeText(DisplayWeathernew.this, "Result"+result, Toast.LENGTH_LONG).show();

         if(result!=null){
            img.setImageBitmap(result);
          }else
              {
                img.setImageResource(R.drawable.button_display_weather);
             }
    }
}



    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        WeatherHome.arrayList.clear();
        super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0,0,0,"Exit");

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if(item.getItemId()==0)
        {
            finish();
        }
        return super.onOptionsItemSelected(item);

    }
}

As soon as I add the statement it throws Null Pointer Exception and not showing even the toast of Result and closes the application.

Also in doInBackground method when I add TOAST just after "myBitmap = BitmapFactory.decodeStream(input);" then code below that doesn't execute neither TOAST get printed and result shows null value.

My Log is:::

03-22 01:16:01.670: E/src(3047): http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png
03-22 01:16:02.160: E/Bitmap(3047): returned
03-22 01:16:02.170: W/dalvikvm(3047): threadid=1: thread exiting with uncaught exception (group=0x4104a450)
03-22 01:16:02.190: E/AndroidRuntime(3047): FATAL EXCEPTION: main
03-22 01:16:02.190: E/AndroidRuntime(3047): java.lang.NullPointerException
03-22 01:16:02.190: E/AndroidRuntime(3047):     at com.ourcast.pocketweather.DisplayWeathernew$ImageDownload.onPostExecute(DisplayWeathernew.java:90)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at com.ourcast.pocketweather.DisplayWeathernew$ImageDownload.onPostExecute(DisplayWeathernew.java:1)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.os.Looper.loop(Looper.java:137)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at android.app.ActivityThread.main(ActivityThread.java:4802)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at java.lang.reflect.Method.invoke(Method.java:511)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-22 01:16:02.190: E/AndroidRuntime(3047):     at dalvik.system.NativeStart.main(Native Method)
03-22 01:16:08.090: E/Trace(3124): error opening trace file: No such file or directory (2)

Suggest me something that could help.

1
well, then img == null - donfuxx
Double check that imgweather is the id of an ImageView in activity_display_weather.xml. If it is then try cleaning your project. - codeMagic
an ImageView is a java object. Java objects can be null - donfuxx
look at documentation of findViewById Returns The view if found or null otherwise. - donfuxx
@codeMagic yeah it was imgWeather instead of imgweather. It worked displaying the image but I am getting only last image as in display rather I wanted to display all images I get in loop. - Saggy

1 Answers

1
votes

Looks like img is null. Check to make sure imgweather is in R.layout.activity_display_weather. You use a different convention for lvWeather so perhaps you meant imgWeather?