I have an array of 5 images (R.drawable.one, R.drawable.two,R.drawable.three, R.drawable.four, and R.drawable.five). Previously, my program would pick 5 random images from these and display them randomly on the screen without any errors. I then added myImgCount
and totalImgCount
because I want to give each image an int value. For example: R.drawable.one would be equal to 1, R.drawable.two would be equal to 2...etc. I then want my for
loop to keep track of the images placed using myImgCount
and then run until totalImgCount
is equal to 50. After I added myImgCount
and totalImgCount
I now have an error:
"11-26 18:22:16.750: E/AndroidRuntime(13971): FATAL EXCEPTION: Thread-33401 E/AndroidRuntime(13971): java.lang.NullPointerException"
on Bitmap bmp = BitmapFactory.decodeResource(getResources(), array1[i]);
Here is the code I have so far:
public void surfaceCreated(SurfaceHolder holder) {
ArrayList<Integer> list = new ArrayList<Integer>();
Random r = new Random();
int[] images = new int[] { R.drawable.one, R.drawable.two,
R.drawable.three, R.drawable.four, R.drawable.five };
int totalImgCount = 0;
for (int i = 0; i<50; i++)
{
while (true)
{
int myImgCount = r.nextInt(5);
totalImgCount += myImgCount + 1;
if (totalImgCount<50) {
list.add(images[myImgCount]); //do it again if not yet at 50
break;
}
totalImgCount -= (myImgCount + 1);
}
}
array1 = convertIntegers(list);
}
The next part of my code that is used to give the images int values:
public static int[] convertIntegers(List<Integer> integers) {
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < ret.length; i++) {
ret[i] = iterator.next().intValue();
}
return ret;
}
Before getting this error my program would run without errors and display 5 random images on the screen. As I mentioned above I am trying to create a loop that keeps displaying random images until the total sum of the images is 50. Let me know if you need any more info! I appreciate any help and thanks in advance!
EDIT: Left out my render
method where I"m getting the error:
public void render(Canvas canvas) {
if (canvas != null) {
Random random = new Random();
canvas.drawRGB(234, 237, 235);
for (int i=0; i<50; ++i) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
array1[i]); //This is where I am currently getting the error
canvas.drawBitmap(bmp,random.nextInt(canvas.getWidth()-bmp.getWidth
()), random.nextInt
(canvas.getHeight()-bmp.getHeight()),null);
}
}
}
It sounds like you're not restricting your random numbers to the array indexes (0-4). Start with this to constrain your numbers between 0 and 4.
int range = images.length-1;
int myImgCount = 0;
Random random = new Random();
for( int i = 0; i < 50; ++i)
{
myImgCount = random.nextInt(range);
list.add(images[myImgCount]);
}
Adjust your render method, see if this will help:
if (canvas != null)
{
Random random = new Random();
canvas.drawRGB(234, 237, 235);
//iterate over your integer array
//there should be 50 resource ids available
for(int resourceId : array1)
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resourceId);
//This is where I am currently getting the error
canvas.drawBitmap(bmp,
random.nextInt(canvas.getWidth()-bmp.getWidth()),
random.nextInt(canvas.getHeight()-bmp.getHeight()), null);
}
}
I would also change the following:
for (int i = 0; i<50; i++)
{
while (true)
{
int myImgCount = r.nextInt(5);
totalImgCount += myImgCount + 1;
if (totalImgCount<50) {
list.add(images[myImgCount]); //do it again if not yet at 50
break;
}
totalImgCount -= (myImgCount + 1);
}
}
Possible change:
int i = 0;
while(i < 50)
{
//calculate random resource ids from your resource array here and add them to your array1
i++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.