On 6/10/07, eduard aylon <eduard.aylon@gmail.com> wrote:
Hi list,
I am trying to make a list of objects where each object has
references to other objects, but I am not succeeding and I get a
NullPointerException. The piece of code below may represent what I am
trying to do.
One more reply to this, it might be obvious by now but I needed a workaround myself and was happy to find one, if a ugly one. Here's a edited version of the original demonstration code that gets around the bug. clearly this isn't going to work if the length of the array isn't defined in the code but is instead determined by the outcome of some opperation. Right now I don't think it's possible to have ChucK generate names for class instances as ChucK can't generate ChucK code, then run it. Somebody kindly corect me if I overlooked something in that regard. This would get quite anoying if -say- 50 "Items" were needed but if a dozen or so would do this might get people by untill this is fixed in a future version.
Yours,
kas.
==========================
class object
{
Std.rand2f(0., 1.) => float f_;
}
class Item extends object
{
2 => int size_;
object item_[size_];
fun void print()
{
for( 0=>int i; i<size_; i++ )
<<< "item [",i,"]", item_[i].f_>>>;
}
<<<"----------------------------------------------">>>;
<<<"calling print from within class definition">>>;
<<<"----------------------------------------------">>>;
print();
}
//4 => int n; (edited out by Kassen)
// ugly and less versatile workaround starts here
//this forces initiation to happen outside of the array
Item a;
Item b;
Item c;
Item d;
//now we asign these to the array
[a,b,c,d] @=> Item l[];
//end of workaround
// no problems up until here
<<<"----------------------------------------------">>>;
<<<"calling print from outside class definition">>>;
<<<"----------------------------------------------">>>;
//notice I had to replace "n" by "l.cap()" here
//this is functionally equivalent anyway
for( 0=>int i; i<
l.cap(); i++ )
l[i].print(); //offending line