Sure. This is anAS3 example, but you can still do the same thing with VOs in AS2 as well.
Jason Merrill Instructional Technology Architect Bank of America Global Learning Join the Bank of America Flash Platform Community and visit our Instructional Technology Design Blog (Note: these resources are only available for Bank of America associates) -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Rodrigo Augusto Guerra Sent: Thursday, July 01, 2010 12:11 PM To: Flash Coders List Subject: Re: [Flashcoders] passing an array as a method = TypeError:Error#1009: Cannot access a property or method of a null object reference. Thanks for your insights Jason. At this moment I'm migrating some AS2 code to AS3 OOP, and as seen, I'm not that familiar with OOP, that´s why these directions are *VERY* helpfull & welcome. thanks. ----- Original Message ----- From: "Merrill, Jason" <[email protected]> To: "Rodrigo Augusto Guerra" <[email protected]>; "Flash Coders List" <[email protected]> Sent: Thursday, July 01, 2010 12:22 PM Subject: RE: [Flashcoders] passing an array as a method = TypeError: Error#1009: Cannot access a property or method of a null object reference. > From your loop, you're accessing an array element that doesn't exist. I > would actually advise a different approach to the problem. > > Don't use pure arrays within pure arrays (called multidimensional > arrays). I find it very hard to understand and maintain that code. It > can get confusing easily and doesn't easily allow for scalability. I > would have an array store class instances which are just classes with > public properties only (called value objects or VOs). One or more of > those properties of the VO instance can be an array, so you can have > nested logic and data, but you are effectively using an "object > array >> object > array" structure, where the objects are defined by you, the > class-writer, instead of an "array > array > array" structure where you > have to do crazy ways to access the data. By using value object > instances, you are describing the data and structure. By using only > arrays within arrays, and no objects, you have neither structure > definition or data definition. > > When you loop through an array, instead of this approach: > >> var tamanho = p1.length - 1 // o indice comeca com zero entao > tem 1 >> elemento a menos. >> >> for (var r = 0; r <= tamanho; r++ ) { >> data = p1[r]; > > Assuming you create an array of class instances, I would recommend this > instead for simplicity's sake because I think the problem is the way you > are looping through the array: > > var questionVO1:QuestionVO = new QuestionVO(); > questionVO.question = "Which fruit is yellow?"; > questionVO.answer = "Banana"; > > var questionVO2:QuestionVO = new QuestionVO(); > questionVO2.question = "What color is the sky?"; > questionVO2.answer = "Blue"; > > var myQuestions:Array = [questionVO1, questionVO2]; > > //So alternatively (not knowing exactly what you wrote your loop for), > as an example, you can > //loop through myQuestions and set all the VO's score property to 0, > like this: > > for each (var questionVO:QuestionVO in myQuestions) > { > //set any property of each questionVO here > questionVO.score = 0; > } > > //etc. I'd actually store the question information in XML and loop > through that to create these VO instances, but you get the point. > //So to check to see if the answer is right: > > if(userResponseTextField1.text == questionVO1.answer) > { > questionVO.score++; > } > > I'd actually take a more elegant approach than that for a quiz, but this > just shows you the basic form of how to check for the user's right > response. > > > Jason Merrill > > Instructional Technology Architect > Bank of America Global Learning > > Join the Bank of America Flash Platform Community and visit our > Instructional Technology Design Blog > (Note: these resources are only available for Bank of America > associates) > > > > > > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Rodrigo > Augusto Guerra > Sent: Thursday, July 01, 2010 10:28 AM > To: 'Flash Coders List' > Subject: Re: [Flashcoders] passing an array as a method = TypeError: > Error#1009: Cannot access a property or method of a null object > reference. > > thanks for your reply cor, you are correct, p1 is a parameter for the > constructor (as string) and for the method (as array). > > Still, if i change the datatype of data to string the error persists > > public function setGabarito(p1:Array) { > > var tamanho = p1.length - 1 // o indice comeca com zero entao tem 1 > elemento a menos. > var data:String; > > //copia os membros do array > for (var r = 0; r <= tamanho; r++ ) { > > data = p1[r]; > //data = "hello"; > > aRespGabarito[r] = data; <-any of the declarations above for > data results in an error. > > } > > } > > > ----- Original Message ----- > From: "Cor" <[email protected]> > To: "'Rodrigo Augusto Guerra'" <[email protected]>; "'Flash Coders > List'" <[email protected]> > Sent: Thursday, July 01, 2010 10:42 AM > Subject: RE: [Flashcoders] passing an array as a method = TypeError: > Error > #1009: Cannot access a property or method of a null object reference. > > >> Data is an Array and p is a String >> >> -----Original Message----- >> From: [email protected] >> [mailto:[email protected]] On Behalf Of >> Rodrigo Augusto Guerra >> Sent: donderdag 1 juli 2010 15:36 >> To: Flash Coders List >> Subject: [Flashcoders] passing an array as a method = TypeError: Error >> #1009: Cannot access a property or method of a null object reference. >> >> Hi all, >> >> I'm creating a simple class that has an method that will receive an >> array as a parameter and store it inside the class in a property. This > >> gives me the error below and I'm stuck. Also I would like to know why >> if I change the code inside the method setGabarito to just ( >> aRespGabarito = p1 ) it works... >> >> >> TypeError: Error #1009: Cannot access a property or method of a null >> object reference. >> at trueFalse/setGabarito() >> at tf_teste_fla::MainTimeline/frame1() >> >> >> this is the class: >> >> package { >> >> import flash.display.Sprite; >> >> public class trueFalse extends Sprite{ >> >> //propriedades >> var nome:String; >> var aRespGabarito:Array; //tera o gabarito do exercicio >> >> //construtor >> public function trueFalse(p1:String) { >> nome = p1; >> } >> >> >> public function setGabarito(p1:Array) { >> >> var tamanho = p1.length - 1 // o indice comeca com zero entao > tem 1 >> elemento a menos. >> >> for (var r = 0; r <= tamanho; r++ ) { >> var data:Array = p1[r]; >> aRespGabarito[0] = data; <<-- ERROR HERE >> trace(aRespGabarito[0]); >> } >> >> } >> >> } >> >> } >> >> >> >> and this is the code inside the test movie: >> >> var usuario:trueFalse = new trueFalse("aa"); >> var arrQuestao:Array = new Array(); >> >> arrQuestao[0] = ["btV1","btX1","btV1",0]; >> arrQuestao[1] = ["btV2","btX2","btX2",0]; >> arrQuestao[2] = ["btV3","btX3","btX3",0]; >> >> usuario.setGabarito(arrQuestao); >> >> >> thanks for any help, >> rodrigo. >> _______________________________________________ >> Flashcoders mailing list >> [email protected] >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >> Geen virus gevonden in het binnenkomende-bericht. >> Gecontroleerd door AVG - www.avg.com >> Versie: 9.0.830 / Virusdatabase: 271.1.1/2974 - datum van uitgifte: >> 06/30/10 >> 20:38:00 >> >> > > > _______________________________________________ > Flashcoders mailing list > [email protected] > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > _______________________________________________ Flashcoders mailing list [email protected] http://chattyfig.figleaf.com/mailman/listinfo/flashcoders _______________________________________________ Flashcoders mailing list [email protected] http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

