Sunday, 15 March 2015

Java object Array -



Java object Array -

i'm kind of new java , i've had problem working object arrays, reply pretty simple can't see it.

i have class named vectors , function named set_components() seek create array of objects this:

vectors[] vec = new vectors[2];

//then access function this:

vec[0].set_components();

however error: exception in thread "main" java.lang.nullpointerexception why that?

it works if 1 object.

vector vec = new vector(); vec.set_components();

your array has been constructed filled nothing, null references. if seek utilize item in array before you've filled array instances, you'll npe you're seeing. think of object array egg crate. must first fill eggs (vector objects) before can utilize them create omelette. done loop.

for (int = 0; < vectors.length; i++) { vectors[i] = new vector(); }

java arrays object

No comments:

Post a Comment