0

I have this method inside an Activity which implements an ImageLoader class:

public void imageLazy(final ImageView image,Project pro)
    {
         String imageurl = pro.smallImageUrl;
        imageLoadery.displayImage(imageurl, activity,image);
    }

but every time I run the Activity, the app crashed. There's a return null pointer on this line

imageLoadery.displayImage(imageurl, activity,image);

and it refers to the first line on this method on ImageLoader class:

if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
                        BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
                        Activity a=(Activity)photoToLoad.imageView.getContext();
                        a.runOnUiThread(bd);
                    }

All I know, what maybe null ist: tag, url, or image.

FYI, the method above is also applied in my listview adapter below. Maybe this can be a reference to help you to solve my case:

public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        // Inflate the view
        if (convertView == null) {

            convertView = mInflater.inflate(resource, null);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.textTitle = (TextView) convertView
                    .findViewById(R.id.txt_title);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Project project = items.get(position);

        holder.textTitle.setText(project.project_title);

        String imageurl = project.smallImageUrl;
        holder.image.setTag(imageurl);
        imageLoader.displayImage(imageurl, activity, holder.image);
        return convertView;

    }

What makes it difficult for me, the method in ListView Adapter apply layout inflater for the Tag, which I barely need on my activity above.

Updated

Stacktrace:

05-16 09:02:00.758: INFO/System.out(441): http://cdn.spendino.de/web/img/projects/home/1263830540.jpg
05-16 09:02:00.769: INFO/System.out(441): spendino.de.Main@43d17ab0
05-16 09:02:00.778: INFO/System.out(441): android.widget.ImageView@43d041f0
05-16 09:02:00.778: INFO/System.out(441): http://cdn.spendino.de/web/img/projects/home/1263997113.jpg
05-16 09:02:00.801: INFO/System.out(441): spendino.de.Main@43d17ab0
05-16 09:02:00.829: INFO/System.out(441): android.widget.ImageView@43d17bd0
05-16 09:02:00.829: INFO/System.out(441): http://cdn.spendino.de/web/img/projects/home/1290615697.jpg
05-16 09:02:00.829: INFO/System.out(441): spendino.de.Main@43d17ab0
05-16 09:02:00.829: INFO/System.out(441): android.widget.ImageView@43d18098
05-16 09:02:01.398: INFO/ActivityManager(33): Displayed activity spendino.de/.Main: 3891 ms (total 3891 ms)
05-16 09:02:01.948: WARN/dalvikvm(441): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
05-16 09:02:01.958: ERROR/AndroidRuntime(441): Uncaught handler: thread Thread-9 exiting due to uncaught exception
05-16 09:02:01.978: ERROR/AndroidRuntime(441): java.lang.NullPointerException
05-16 09:02:01.978: ERROR/AndroidRuntime(441):     at spendino.de.ImageLoaderCache$PhotosLoader.run(ImageLoaderCache.java:244)
10
  • 1
    After this line imageLoadery.displayImage(imageurl, activity,image); put these lines System.out.println(imageurl);System.out.println(activity);System.out.println(image); and let me know the output Commented May 16, 2011 at 8:27
  • wow, thx! I just found out that the 'activity' is null. Any clue what should I do next? Commented May 16, 2011 at 8:38
  • So I just add this line: activity = Main.this; inside onCreate, the system.out shows that activity is not null anymore but the app still crashed with the reason nullPointerException Commented May 16, 2011 at 8:41
  • done, please have a look Commented May 16, 2011 at 9:03
  • if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){BitmapDisplayer bd=new BitmapDisplayer(bmp,photoToLoad.imageView); Activity a=(Activity)photoToLoad.imageView.getContext(); a.runOnUiThread(bd);} ....... Print the out put of photoToLoad.imageView.getTag(),photoToLoad.url,imageView,bd Commented May 16, 2011 at 9:19

2 Answers 2

1

You missed the setTag method..Hope it will work . Changed the imageLazy method.

 public void imageLazy(final ImageView image,Project pro)
        {
             String imageurl = pro.smallImageUrl;
            image.setTag(imageurl);
            imageLoadery.displayImage(imageurl, activity,image);
        }
Sign up to request clarification or add additional context in comments.

1 Comment

works terrific! Awesome, thank you for your step-by-step in solving the problem. I learned a lot. Anyway would you mind to check this question of mine: stackoverflow.com/questions/6014809/… ? thanks
0

All I know, what maybe null ist: tag, url, or image.

Which one is that?

I know it's not a proper answer, but it is long enough to be such, and also may help you find the problem:

This is not so hard to debug NullPointerException most of the times. At least to bring the question to a level of "None of the parameters is null and yet I get a NullPointerException" or to a level of "Parameter X is null" but I can't understand why, here I'm initializing it" if you get NullPointerException, check if you sent a null parameter, or if the object accessed is null. If you don't know to use a debugger, you should learn how, but you could still add some checks to see if any parameter is null and print appropriate message to the log.

This will not only help us understand the problem and help you solve it, but it will help you find the cause and fix it next time.

1 Comment

I know I get it, I've initialized the variable (see my comments above) and debug it but the app still crashed with NullPointerException. Maybe can you help me? Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.