Invalid Field Access : Length

346 views
Skip to first unread message

Yusuf Fahry

unread,
Apr 11, 2016, 7:43:36 PM4/11/16
to Haxe
Hi,

I got an error that is not familiar to me. It says that Uncaught exception - Invalid field access : length.

The error is happened in line: 
var x:Int = aList[listpos].length;

from the createNew() function.

I don't understand about this error. I also have been looking the answer, but it seems like it's a "not fixed" problem - depends on the code. Can anyone help me?

This the snippet of the associated code.

package;

import haxe.ds.Vector;
import neko.Lib;
import haxe.io.Input;

class Main
{
 
static var aList = new Array();
 
static var idList = new Array();
 
static var done:Int;
 
static var selection:Int;
 
static var idn:Int;
 
 
static function main()
 
{
   createNew
();
 
}
 
 
static function createNew()
 
{
 
var done = 0;
 
var listpos:Int = Std.random(5);
 
var id:Int = Std.random(100);
 
 
var x:Int = aList[listpos].length;
 
var y:Int = idList.length;
 
 
// Determining position and checking
 
while (done == 0)
 
{
 
var pos:Int = Std.random(5);
 
done = 1;
 
 
if (x == 5)
 
{
 
done = 0;
 
}
 
 listpos
= pos;
 
}
 
 
done = 0;
 
 
// Determining ID wether if exist or not
 
while ( done == 0 )
 
{
 
var idn:Int = Std.random(100);
 
done = 1;
 
 
if (y != 0)
 
{
 
for (i in idList)
 
{
 
if ( i == idn )
 
{
 
done = 0;
 
}
 
}
 
}
 id
= idn;
 
}
 
 
var member = new Member(id, Std.random(1));
 aList
[listpos].insert(x, member);
 idList
.insert(y, member.id);
 
}
 
}

class Member
{
 
public var key:Int;
 
public var id:Int;
 
public var bottom:Int;
 
public var left:Int;
 
public var right:Int;
 
 
public function new(id, key)
 
{
 
this.id = id;
 
this.key = key;
 
}
}

Thanks.

Daniel Llewellyn

unread,
Apr 11, 2016, 7:51:20 PM4/11/16
to haxe...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Caveat: this is my best guess as a bystander who has passing
familiarity with HAXE code. I'm far from an expert. I may be entirely
wrong with my comments below...

On 12/04/16 00:43, Yusuf Fahry wrote:
> Hi,
>
> I got an error that is not familiar to me. It says that *Uncaught
> exception - Invalid field access : length*.

That error means that the runtime does not know what .length means on
your array slice.

> This the snippet of the associated code.
>
> package;
>
> importhaxe.ds.Vector; importneko.Lib; importhaxe.io.Input;
>
> classMain { staticvaraList =newArray();

I think you need to change this to:

static Array<String> aList = new Array<String>()

That will tell your runtime that the array contains strings and
therefore a slice is a single instance of String which _does_ have a
.length

- --
Daniel Llewellyn, Bowl Hat
PGP/GnuPG Key ID: 0x0349ED21
4C9C BFAD 0069 D679 9660 BCD5 40C2 D958 0349 ED21
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBCAAGBQJXDDhsAAoJEFx8XrpDpVlRDeMIAMI6Q2c2NRvgBwnsY+BpV7zC
GP9R1DGWPcCAKTmNoZbHs3leG2F1amWAmRfgmfo0T+/8+YrYp/WXwgQ3IOT0ExKF
OlGRjRW9ms6Qx8POgbhid3Q8Pfscjy7uKb6gt6++7MSYek9dtCgjrrTlvdjZIyRF
Gu90XPYqlZSqWEspMdNq+NBc6Bh9QuR5CAr+lX6aWnM+ysUtmg54TT8L1feE6aLD
/rCvDPeJxGzDNkPaoOmsC/AwLQPPjGe0lmyi8UjigFFE+uf2bAJqkkS25qlC9acK
9Di7C4edshiLBAZZGX5jw0kA/JStzJv15HwAAqV4DNfs6FFoxWiwgDUyrWRd11Q=
=L2Jl
-----END PGP SIGNATURE-----

Yusuf Fahry

unread,
Apr 11, 2016, 9:26:09 PM4/11/16
to Haxe
Thanks Daniel, you're right0 The runtime doesn't what is inside. :D

PSvils

unread,
Apr 12, 2016, 1:38:25 AM4/12/16
to Haxe
"
static var aList = new Array();
"
"
aList[listpos].insert(x, member);"

I think it resolved the type fine, but resolved it to your Member class since that's what you're putting in it, and you don't have a length field on it...so not sure why you were expecting to get a length read there in the first place :)

kobi2187

unread,
Apr 13, 2016, 1:40:15 PM4/13/16
to Haxe
I think you have a mistake because
when you type aList[listpos] you already get back one of the list's members.
then you do member.length, instead of list.length.
so it's like doing int.length or something.
unless the members inside respond to the length getter/variable you have a mistake.
Reply all
Reply to author
Forward
0 new messages