Object not initialized error when looping over a List in Dynamics AX?
-
03-06-2021 - |
Question
The Problem:
- I have a list with Objects as elements.
- I use enumerator to loop over the list.
- While looping I assign elements to a variable object with the same type as the element.
- When I call a method on the object, but I get the following error-message:
MyClass object not initialized.
I think I must cast, but don't know how to do this in Dynamics AX.
I develop in MS Dynamics AX 2012.
MyExampleDataContract exampleDataContract = new MyExampleDataContract();
while (listEnumerator.moveNext())
{
exampleDataContract = listEnumerator.current();
info(exampleDataContract.parmCustomerId()); //This gives an error.
}
Solution
A list element can be null.
List l = new List(Types::Class);
;
l.addEnd(null);
info(int2str(l.elements()));
OTHER TIPS
The solution was: Before inserting elements in the list they should be instantiated first.
for (counter = 1; counter <= 3; counter++)
{
exampleDataContract = new MyExampleDataContract(); // This is what I missed :)
exampleDataContract.init("DEV-000000000" + int2str(counter));
myList.addEnd(exampleDataContract);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow