-1

I have an error code that I do not have a resolver, and I looked for several ways here in stackoverflow but I did not succeed.

the error that is displayed:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at softcode.tattostudio.BudgetFragment$1GetImage.onPostExecute(BudgetFragment.java:250) at softcode.tattostudio.BudgetFragment$1GetImage.onPostExecute(BudgetFragment.java:240)

Basic the lines that give the error are:

private void getImage(String urlToImage){
    class GetImage extends AsyncTask<String,Void,Bitmap> {  //This line is 240

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imgvShowTattoIMG.setImageBitmap(bitmap);  //This line is 250
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            URL url = null;
            Bitmap image = null;

            String urlToImage = params[0];
            try {
                url = new URL(urlToImage);
                image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return image;
        }
    }
    GetImage gi = new GetImage();
    gi.execute(urlToImage);
}

Anyone have any idea what it could be? Need to post the full code?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – user207421 May 06 '18 at 06:15

1 Answers1

0

Anyone have any idea what it could be?

imgvShowTattoIMG is null.

Beyond that, I strongly recommend that you use an existing image-loading library (e.g., Picasso, Glide) rather than implementing your own code for that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • but is not null, was declared as '' ImageView imgvShowTattoIMG = view.findViewById(R.id.imgvShowTattoIMG); '' – Jhonathan Candeu May 05 '18 at 21:53
  • @JhonathanCandeu: "but is not null" -- according to your stack trace, it is. "was declared as" -- then perhaps `view` does not contain a widget with the ID of `imgvShowTattoIMG`. – CommonsWare May 05 '18 at 21:55
  • the ImageView widget is in my layout.xml, I honestly do not understand how it can be null if I declare it by id – Jhonathan Candeu May 05 '18 at 22:00
  • @JhonathanCandeu: "the ImageView widget is in my layout.xml" -- perhaps `view` is not coming from that layout resource. – CommonsWare May 05 '18 at 22:01
  • in this same context I edit other textview of the same layout, the problem lies in that imageview that the code says to be null – Jhonathan Candeu May 05 '18 at 22:33
  • @JhonathanCandeu: You might consider asking a separate Stack Overflow question, where you show your layout and your code that uses the layout. – CommonsWare May 05 '18 at 23:04
  • I can understand the code, what happens is that the inflated view does not contain the widget, it is in another view inflated in the same context, but if I inflate view 2 and declare my widget, the error will continue because my "getImage "is on view 1. Do you understand what I mean? How can I declare my widget from another layout in the same inflated view? – Jhonathan Candeu May 05 '18 at 23:34
  • @JhonathanCandeu: "Do you understand what I mean?" -- no, sorry. – CommonsWare May 05 '18 at 23:45
  • I posted another question in https://stackoverflow.com/questions/50195037/android-how-to-declare-a-widget-from-another-layout – Jhonathan Candeu May 05 '18 at 23:52