0

I am trying to get url from JSON data and use it to download image as set it as a imageView, but I am getting the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                                                                         at com.example.seemore.travel$LoadImagefromUrl.onPostExecute(travel.java:308)
                                                                         at com.example.seemore.travel$LoadImagefromUrl.onPostExecute(travel.java:294)

but I am sure that I get the correct url as after I print it out it is: https://img.evbuc.com/https%3A%2F%2Fimg.evbuc.com%2Fhttp%253A%252F%252Fcdn.evbuc.com%252Fimages%252F17170274%252F154231754459%252F1%252Foriginal.jpg%3Frect%3D0%252C84%252C1016%252C508%26s%3Dc24b1445c142e35c5fb65b08e7051304?h=200&w=450&s=7c716f6315512667258d7cee526783b6

My reduced code is:

public class travel extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler.postDelayed(runnable, 100);
    }

    private Runnable runnable = new Runnable() {
        public void run() {
        initiatePopupWindow();
        mHandler.postDelayed(runnable, 100);
        if (gps > latEnd){
            mHandler.removeCallbacks(runnable);
        }
    }
};

private PopupWindow pwindo;

private void initiatePopupWindow() {
    try {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                    (ViewGroup) findViewById(R.id.popup_element));
        pwindo = new PopupWindow(layout, 700, 1200, true);
        pwindo.setBackgroundDrawable(new BitmapDrawable());
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
        resId  = getResources().getIdentifier(words[number*6].toLowerCase(), "drawable", getPackageName());
        ((ImageView)pwindo.getContentView().findViewById(R.id.imageView)).setImageResource(resId);
        ((TextView)pwindo.getContentView().findViewById(R.id.txtView)).setText(words[3 + number * 6]);
        String event= ((new events()).getEventData(getApplicationContext(), words[1 + number * 6],words[2 + number * 6]));
        float count=0;
        try {
            JSONObject  jsonRootObject = new JSONObject(event);
            JSONObject mainObj=jsonRootObject.getJSONObject("pagination");
            count = Float.valueOf(mainObj.optString("object_count"));
            } catch (JSONException e) {e.printStackTrace();}
        if (count>0)
        {
            String description = "";
            JSONObject  jsonRootObject = new JSONObject(event);
            JSONArray jsonArray = jsonRootObject.optJSONArray("events");
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            JSONObject information = jsonObject.getJSONObject("name");
            String text = information.getString("text"); 
            ((TextView)pwindo.getContentView().findViewById(R.id.events)).setText(text);
            information = jsonObject.getJSONObject("logo");
            text = information.getString("url");
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
            (TextView)pwindo.getContentView().findViewById(R.id.events)).setText(text);
               new LoadImagefromUrl( ).execute(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    private class LoadImagefromUrl extends AsyncTask< Object, Void, Bitmap > {
        ImageView ivPreview = null;

        @Override
        protected Bitmap doInBackground( Object... params ) {
            this.ivPreview = (ImageView) findViewById(R.id.eventsImage);
            String url = (String) params[0];
            System.out.println(url);
            return loadBitmap( url );
        }

        @Override
        protected void onPostExecute( Bitmap result ) {
            super.onPostExecute( result );
            ivPreview.setImageBitmap( result );
        }
    }

    public Bitmap loadBitmap( String url ) {
        URL newurl = null;
        Bitmap bitmap = null;
        try {
            newurl = new URL( url );
            bitmap = BitmapFactory.decodeStream( newurl.openConnection( ).getInputStream( ) );
        } catch ( MalformedURLException e ) {
            e.printStackTrace( );
        } catch ( IOException e ) {

            e.printStackTrace( );
        }
        return bitmap;
    }
}

and xml file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
    android:id="@+id/txtView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5sp" />
<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <TextView
    android:id="@+id/taxi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <TextView
        android:id="@+id/weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/restaurant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/events"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/eventsImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Laurynas G
  • 573
  • 9
  • 30
  • The error message tells you the line number of the problem. What are the contents of that line? As you have reduced your code in the question it is impossible for us to tell from what you posted. – EkcenierK Dec 27 '15 at 01:46
  • 1
    why don't you've `this.ivPreview = (ImageView) findViewById(R.id.eventsImage);` in `main thread` than in `doInBackground` – Mohammad Tauqir Dec 27 '15 at 01:46

1 Answers1

0

Declare your ImageView ivPreview variable in Activity instead. onPostExecute executes on Activity that run the thread so ivPreview will be null.