nullpointerexception when retrieving a rectangle from the array of rectangles

Jesal Patel

So I have a class dealing with buttons and I have an array of rectangles containing 2 individual rectangular shapes. Now when I make a variable which retrieves say the 0th index of the array it will give me a nullpointerexception I have been scratching my head over this I have clearly declared and initialised the array and made it the appropriate size for 2 rectangles to be contained and have assigned these to the indexes. I must be missing something really small which I can't seem to figure out.

Below I have put the relevant code for this:

public class MenuButton {

private int height;
private int width;
private float positionX;
private float positionY;

//private ArrayList<Rectangle> rects;
private Rectangle rects[];

private Rectangle play;
private Rectangle touchToPlay;

private boolean isTouched;

public MenuButton(int height, int width, float positionX, float positionY){

    this.height = height;
    this.width = width;
    this.positionX = positionX;
    this.positionY = positionY;

    isTouched = false;
    Rectangle rects[] = new Rectangle[2];
    play = new Rectangle(positionX, positionY, width, height);
    touchToPlay = new Rectangle(positionX, positionY, width, height);


    //can clean this up by introducing initButtons() to assign buttons to
    //indexes of the array
    rects[0] = play;
    rects[1] = touchToPlay;

}   


public boolean isClicked(int index,float screenX, float screenY){

    //ERROR IS BELOW THIS LINE  
    Rectangle rect = rects[0];

    return rect.contains(screenX, screenY);
} 
Reimeus

Youre shadowing the variable rects. Replace

Rectangle rects[] = new Rectangle[2];

with

rects = new Rectangle[2];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

nullpointerexception when retrieving a rectangle from the array of rectangles

From Dev

Find remaining outer rectangles from Rectangle.Intersection

From Dev

Retrieving a variable from an array

From Dev

Retrieving Array from Parse

From Dev

Ruby Retrieving from Array

From Dev

Retrieving array from MySQL

From Dev

Android Studio crash when retrieving content from array

From Dev

Retrieving class from hibernate throws nullpointerexception

From Dev

NullPointerException while retrieving Extras from Intent

From Dev

Filter Rectangles with 2:1 Aspect Ratio from an array of rectangles

From Dev

How to remove random rectangle from rectangle array?

From Dev

NullPointerException Error when Reading an Array Element from a File - Java

From Dev

NullPointerException when calling a method from a dynamic object array

From Dev

NullPointerException when writing to an initialized array from text document in Java

From Dev

NullPointerException When trying to get string element from array

From Dev

Retrieving data from firebase Array

From Dev

Retrieving image from character array

From Dev

Retrieving elements from array javascript

From Dev

Retrieving elements from a nested array

From Dev

Retrieving elements from a Dynamic array

From Dev

Retrieving an array of values from a Key

From Dev

Retrieving data from json array

From Dev

Retrieving data from a multidimensional array

From Dev

Retrieving values of an object from an array?

From Dev

retrieving values from complex array

From Dev

Retrieving an array from within an a JSONArray

From Dev

Fitting equal rectangles into larger rectangle

From Dev

Get the bounding rectangle of multiple rectangles

From Dev

Retrieving bitmap image from a URL ,getting nullpointerexception randomly in android

Related Related

  1. 1

    nullpointerexception when retrieving a rectangle from the array of rectangles

  2. 2

    Find remaining outer rectangles from Rectangle.Intersection

  3. 3

    Retrieving a variable from an array

  4. 4

    Retrieving Array from Parse

  5. 5

    Ruby Retrieving from Array

  6. 6

    Retrieving array from MySQL

  7. 7

    Android Studio crash when retrieving content from array

  8. 8

    Retrieving class from hibernate throws nullpointerexception

  9. 9

    NullPointerException while retrieving Extras from Intent

  10. 10

    Filter Rectangles with 2:1 Aspect Ratio from an array of rectangles

  11. 11

    How to remove random rectangle from rectangle array?

  12. 12

    NullPointerException Error when Reading an Array Element from a File - Java

  13. 13

    NullPointerException when calling a method from a dynamic object array

  14. 14

    NullPointerException when writing to an initialized array from text document in Java

  15. 15

    NullPointerException When trying to get string element from array

  16. 16

    Retrieving data from firebase Array

  17. 17

    Retrieving image from character array

  18. 18

    Retrieving elements from array javascript

  19. 19

    Retrieving elements from a nested array

  20. 20

    Retrieving elements from a Dynamic array

  21. 21

    Retrieving an array of values from a Key

  22. 22

    Retrieving data from json array

  23. 23

    Retrieving data from a multidimensional array

  24. 24

    Retrieving values of an object from an array?

  25. 25

    retrieving values from complex array

  26. 26

    Retrieving an array from within an a JSONArray

  27. 27

    Fitting equal rectangles into larger rectangle

  28. 28

    Get the bounding rectangle of multiple rectangles

  29. 29

    Retrieving bitmap image from a URL ,getting nullpointerexception randomly in android

HotTag

Archive