Hello! Wrote the code for the quiz. Everything works, questions are asked randomly, but at some point the application stops and the following error is displayed: "TypeError: Error # 1009: Cannot call a property or method with reference to the object" null ".at Main / asking () at Main / onClick ( ) ". I tried to eliminate it, applied tracing, but did not find a bug. In addition, an error occurs every time in a new place: it may appear after the first question, or after the tenth or any other. I fight the second day, I did not find the answer in the search engine. Here is the code itself:
package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.text.*; import TfFormat; public class Main extends Sprite { var externalXML:XML;//Π·Π°Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ XML var numOfQuestion:Number;//ΡΠΈΡΠ»ΠΎ Π²ΠΎΠΏΡΠΎΡΠΎΠ² var currentXML:XML=new XML;//XML ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ Π²ΠΎΠΏΡΡΠΎΠ° var currentElementText:String='';//ΡΠ΅ΠΊΡΡΠΈΠΉ Π²Π°ΡΠΈΠ°Π½Ρ ΠΎΡΠ²Π΅ΡΠ° var i;// Π½ΠΎΠΌΠ΅Ρ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ Π²ΠΎΠΏΡΠΎΡΠ° var massiv:Array=[];// ΠΌΠ°ΡΡΠΈΠ² Ρ ΠΎΡΠ²Π΅ΡΠ°ΠΌΠΈ var loader:URLLoader = new URLLoader(); var request:URLRequest=new URLRequest("super.xml"); var textF:TfFormat = new TfFormat; var rnd:RND; //ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ Π³Π΅Π½Π΅ΡΠ°ΡΠΎΡΠ° Π½ΠΎΠΌΠ΅ΡΠ° Π²ΠΎΠΏΡΠΎΡΠ° public function Main() { loader.load(request); loader.addEventListener(Event.COMPLETE, preOnComplete); } /* Π€ΡΠ½ΠΊΡΠΈΡ Π·Π°Π³ΡΡΠ·ΠΊΠΈ XML-ΡΠ°ΠΉΠ»Π° */ function preOnComplete(event:Event):void { var tf:TextField = new TextField; tf.text = 'ΠΠΈΠΊΡΠΎΡΠΈΠ½Π°' tf.setTextFormat(textF); var goStart:AnswerBut = new AnswerBut('Π·Π°ΠΏΡΡΠΊ Π²ΠΈΠΊΡΠΎΡΠΈΠ½Ρ'); tf.x =30; tf.y =30; tf.width = 300; tf.height = 40; goStart.x = 50; goStart.y = 50; addChild(tf); addChild(goStart); goStart.addEventListener(MouseEvent.CLICK, onComplete); } function onComplete(event:Event):void { if (this.numChildren>1) { while (this.numChildren > 1) { this.removeChildAt(1); } } if (loader!=null) { externalXML=new XML(loader.data); numOfQuestion = externalXML.quest.length(); asking(externalXML.quest.length()+1); //Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ Π²ΠΎΠΏΡΠΎΡ } }
The function asking (numOff) is responsible for the task process of a specific question. The parameter of the function is passed the number of the previous question, which should not be set again. When you first start the function, this number is longer than the length of the array of questions. /
function asking(numOff):void { var currentRandom:Number = numOff; //ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅ΠΌΡ Π½ΠΎΠΌΠ΅ΡΡ //Π²ΠΎΠΏΡΠΎΡΠ° ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΠΉ Π½ΠΎΠΌΠ΅Ρ Π²ΠΎΠΏΡΠΎΡΠ° while(currentRandom == numOff) { currentRandom = Math.round(Math.random()*numOfQuestion); //ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ Π²ΠΎΠΏΡΠΎΡΠ°, Π½Π΅ ΡΠ°Π²Π½ΡΠΉ //ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅ΠΌΡ Π½ΠΎΠΌΠ΅ΡΡ } currentXML=externalXML.quest[currentRandom]; //ΡΠ΅ΠΊΡΡΠΈΠΉ Π²ΠΎΠΏΡΠΎΡ Π² Π²ΠΈΠ΄Π΅ XML var tf:TextField = new TextField; tf.text = currentXML.@text.toString(); tf.setTextFormat(textF); tf.width = 400; tf.height = 40; tf.x = 20; tf.y = 70; tf.wordWrap = true; addChild(tf); var numMassiv:Array = []; for (var i=0; i<currentXML.answer.length(); i++) { numMassiv[i] = i } var answerMassiv:Array = []; var answerNum = numMassiv.length; for (var k=0; k<answerNum; k++) { var kk = Math.round(Math.random()*(numMassiv.length-1)); answerMassiv[k]=new AnswerBut(currentXML.answer[numMassiv[kk]]); if (numMassiv[kk] == 0) answerMassiv[k].correct = true; answerMassiv[k].num = currentRandom; trace(currentRandom); numMassiv.splice(kk,1); answerMassiv[k].x=30; answerMassiv[k].y=k*20+120; addChild(answerMassiv[k]); answerMassiv[k].addEventListener(MouseEvent.CLICK, onClick); } } function onClick(e:MouseEvent):void { var prev = new Number(e.target.num); //ΡΠ΄Π°Π»ΡΠ΅ΠΌ Π²ΡΠ΅Ρ
ΠΏΠΎΡΠΎΠΌΠΊΠΎΠ² ΠΊΠ»Π°ΡΡΠ° if (this.numChildren>1) { while (this.numChildren > 1) { this.removeChildAt(1); } } asking(prev); } }
}