0

I am passing an int array from one class to another but am getting an error when I try to access values from it. I have no idea why, hopefully someone can enlighten me?

Here's the first class that calls the second:

public class ConvertToGrid extends Activity{

DrawGrid v;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...code...
    int[] binArray = {Color.RED, Color.WHITE, Color.YELLOW, ...};

    v = new DrawGrid(this, binArray);
    setContentView(v);
}}

This calls my DrawGrid View:

public class DrawGrid extends View{

private int[] binary;

public DrawGrid(Context context) {
    super(context);
}

public DrawGrid(Context context, int[] inBinary) {
    super(context);
    binary = inBinary.clone();
}

int sq00c = binary[0];
...etc}

What am I doing wrong such that it cannot access this int array called binary? If I move the int array into DrawGrid it accesses the cells without any trouble, but passing it through with my construct seems to make it inaccessible. In case anyone asks, I can't just define the array in DrawGrid as it is defined by the code in ConvertGrid.

Perhaps I am going about this in the wrong way and there is a better way to pass the int array? Thanks

Edit:

LogCat:

E/AndroidRuntime(12035): FATAL EXCEPTION: main
E/AndroidRuntime(12035): java.lang.RuntimeException: Unable to start activity ComponentInfo{bras2756.ox.ac.uk.colourgrid/bras2756.ox.ac.uk.colourgrid.ConvertToGrid}: java.lang.NullPointerException
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
David
  • 169
  • 3
  • 13

2 Answers2

4

You can't do that because your int sq00c = binary[0]; type statements are outside a method body, and hence get executed before your constructor is called, which makes the binary array empty. So when you try to access data in it, you'll get an ArrayIndexOutOfBounds exception.

Try using:

public class DrawGrid extends View{

    private int[] binary;
    private int sq00c;
    etc....

    public DrawGrid(Context context) {
        super(context);
    }

    public DrawGrid(Context context, int[] inBinary) {
        super(context);
        binary = inBinary;
        sq00c = binary[0];
        ...etc
    }
}

I've split the int declaration and the assignment into two. The ints are still declared at the class level, but are only given a value when the constructor is called.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 3
    Aaaarrrrrghhhh. Another outbreak of the "global" disease. We need an antidote now. This is not global, it's a class level variable. A global, by definition, has scope and lifetime equal to the application. If we don't care about being accurate and correct, then why do we try to develop software? – Simon Mar 28 '13 at 22:03
  • @Simon Better? My apologies for the incorrect terminology. Completely self taught, so dont' really know what to call them. I do know what's what though. – Raghav Sood Mar 28 '13 at 22:05
  • 1
    Yeh, +1 for being a good Overflower, a quick fix and being self taught ;) (PS. I voted for you) – Simon Mar 28 '13 at 22:15
  • 1
    That's great, thanks. Teaching myself also so I really appreciate the odd clear explanation. – David Mar 28 '13 at 22:24
0

By seeing the exception seems like this is your problem

Clone() create a shallow copy check this link

I think following modification will solve your problem

public DrawGrid(Context context, int[] inBinary) {
super(context);
binary = inBinary; //update to this
}
Community
  • 1
  • 1
minhaz
  • 4,233
  • 3
  • 33
  • 49