"nil argument" when I use class data member

Question from a Mu programmer:

> It seems if I define a class member variable with a custom type, such as,
> float[6] _sliderPerc;
> I would get a run-time error if this variable is used in any of the
> class methods.
> -----------------------------------
> ERROR: Uncaught Exception: nil argument to function.
> 0: float[6].[] (float&; float[6], int)
> 0: float[6]
> 1: int
> 1: operator = (float&; float&, float)
> 0: float&
> 1: float
> ------------------------------------
> Why is that?

I know this is confusing, but it's because the variable has not been
initialized until it's constructor is called. The line you quote
kind of declares a reference, but it's empty until you initialize
it. If you cast it to a string and print it out, you'll see
something like

    <#opaque 0>

BTW, practically everything in Mu can be printed this way:

    print ("%s\n" % _sliderPerc);

To fix this, in the constructor of your class, you need to do
something like:

    _sliderPerc = float[6]();


1 条评论

  • 0
    Alan Trombla