0

I have a simple Android app with 2 activities and a base activity where I store a global array of images.

The ImageGrid activity displays a grid of images that the user can touch/click to display a larger version of that image.

When clicked, ImageGrid sends the selected index of the image that was touched to the selectedImage activity.

However, everytime it does that, I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at SelectedImage.onCreate(SelectedImage.java:19)

Line 19 is this:

int arrNum = arrPosition;

I've stepped through the debugger, and I do see the index position getting passed and set, so I'm at a loss as to why I'm getting a NullPointerException.

Here is the BaseActivity:

public class BaseActivity extends AppCompatActivity {

    Integer[] ApocImages =  {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
            R.drawable.image9, R.drawable.image10, R.drawable.image11, R.drawable.image12,
            R.drawable.image13, R.drawable.image14, R.drawable.image15, R.drawable.image16};
}

ImageGrid:

public class ImageGrid extends BaseActivity {

    ImageView gridThumbnail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_grid);
        GridView grid = findViewById(R.id.gridView);
        final ImageView selectedImage = findViewById(R.id.imgLarge);
        grid.setAdapter(new ImageAdapter(this));

        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent myIntent = new Intent(ImageGrid.this, SelectedImage.class);
                int arrPos = position;
                myIntent.putExtra("selectedImage", arrPos);
                startActivity(myIntent);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;

        public ImageAdapter(Context c) {
            context = c;
        }

        public int getCount() {
            return ApocImages.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        //gets the view for each item in the array
        public View getView(int position, View contentView, ViewGroup parent) {
            // populate gridview with imageview of each photo in array
            gridThumbnail = new ImageView(context);
            gridThumbnail.setImageResource(ApocImages[position]);
            gridThumbnail.setScaleType(ImageView.ScaleType.FIT_XY);
            gridThumbnail.setLayoutParams(new GridView.LayoutParams(330, 300));
            return gridThumbnail;
        }

    }
}

SelectedImage:

public class SelectedImage extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context appContext = getApplicationContext();
        final ImageView selectedImage = findViewById(R.id.imgLarge);

        Intent intent = getIntent();
        int arrPosition = intent.getIntExtra("selectedImage", 1);

        int arrNum = arrPosition;
        selectedImage.setImageResource(ApocImages[arrNum]);
        selectedImage.setClickable(true);

        selectedImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(SelectedImage.this, ImageGrid.class));
                finish();
            }
        });
    }
}
SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • It looks like line 19 is actually: selectedImage.setImageResource(ApocImages[arrNum]); and that selectedImage is the null object, not the array. The view may not be inflated at that point. – Bakon Jarser May 03 '18 at 17:43
  • You did not call `setContentView()` in `SelectedImage`. – ADM May 03 '18 at 17:45

2 Answers2

2

In SelectedImage (I would advise naming it SelectedImageActivity) you make the call final ImageView selectedImage = findViewById(R.id.imgLarge); but never specified a setContentView(R.layout.activity_image_grid);. You must set a content view with the id: R.id.imgLarge contained inside of it in order for that activity to be able to find it.

HaydenKai
  • 871
  • 7
  • 31
1

In your onCreate(), setContentView() is missing.

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126