Copy link to clipboard
Copied
Hi everyone,
I am writing an educational game that will be used for our introductory chemistry courses. I'm having trouble pulling in xml data from my xml file. I always test what I want to do in a small file before adding it to the master game file. The strange this is that the code works in the test file but when I copy and paste it into the master file I get Error #1009. As I understand it i'm getting this because something is missing that flash cant find so it doesn't fill in the information that I want. I ran the debugger which traced the error to line 65 ( which changes a little bit depending on how many spaces I add between code but I will highlight it). When I disable the line of code the error just lists the next line because I'm trying to pull data into 6 text boxes one for each of 6 categories and I figure that all 6 are somehow broke and when I disable one it just lists the next broken one. Now again I am copying and pasting between the file the works and the file that doesn't, and I have double and triple checked my instance names and even changed them and every other variable. Onto the code!
<?xml version="1.0" encoding="utf-8"?>
<category>
<category1> Hi Mom</category1>
<category2> Monoatomic Ions </category2>
<category3> Polyatomic Ions </category3>
<category4> Acids and Bases </category4>
<category5> Elements </category5>
<category6> My fault </category6>
</category>
Here are my .xml categories. As you can see, nothing too out of the ordinary.
import flash.events.Event;
import flash.net.URLRequest;
var textXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("categories.xml"));
function xmlLoaded(evt:Event):void
{
var textXML = new XML(xmlLoader.data);
C1.text = textXML.category1;
C2.text = textXML.category2;
C3.text = textXML.category3;
C4.text = textXML.category4;
C5.text = textXML.category5;
C6.text = textXML.category6;
gotoAndStop(2);
}
I was following a tutorial by Tod Perkins on Lynda.com and this is how it is supposed to be written. Here is a screenshot of what it looks like when I run the test file.
This is good. This is exactly what I want. Now when I copy and paste the above code into my main file I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ChemScholar5_fla::MainTimeline/xmlLoaded()[ChemScholar5_fla.MainTimeline::frame1:65]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
I turned on the permit debugging option and the debugger is telling me the problem is on frame 1 line 65: that corresponds to the following
import flash.events.Event;
import flash.net.URLRequest;
var textXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("categories.xml"));
function xmlLoaded(evt:Event):void
{
var textXML = new XML(xmlLoader.data);
C1.text = textXML.category1;
C2.text = textXML.category2;
C3.text = textXML.category3;
C4.text = textXML.category4;
C5.text = textXML.category5;
C6.text = textXML.category6;
gotoAndStop(2);
}
If I disable the line of code, it just errors the next one as so:
import flash.events.Event;
import flash.net.URLRequest;
var textXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("categories.xml"));
function xmlLoaded(evt:Event):void
{
var textXML = new XML(xmlLoader.data);
/*C1.text = textXML.category1;*/
C2.text = textXML.category2;
C3.text = textXML.category3;
C4.text = textXML.category4;
C5.text = textXML.category5;
C6.text = textXML.category6;
gotoAndStop(2);
}
Here is another strange thing. If I remove C1-C6 and attempt to trace all of the data or part of the data it is being parsed and loaded correctly:
import flash.events.Event;
import flash.net.URLRequest;
var textXML:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(new URLRequest("categories.xml"));
function xmlLoaded(evt:Event):void
{
var textXML = new XML(xmlLoader.data);
trace(textXML.category1);
C1.text = textXML.category1;
C2.text = textXML.category2;
C3.text = textXML.category3;
C4.text = textXML.category4;
C5.text = textXML.category5;
C6.text = textXML.category6;
gotoAndStop(2);
}
Here on my computer I have highlighted the output of the trace (great) and I left the other code intact so that I would also get the TypeError. Correspondingly because I added a new line of code it says that the broken line is now 66 as I would expect. I am at a complete loss so hopefully I have described the problem well enough and someone is able to help. Thanks everyone!!
Copy link to clipboard
Copied
Try using the trace function on the objects of the line that errs to isolate which part of it is not being found.
function xmlLoaded(evt:Event):void
{
var textXML = new XML(xmlLoader.data);
trace(C1)
trace(textXML)
C1.text = textXML.category1;
C2.text = textXML.category2;
C3.text = textXML.category3;
C4.text = textXML.category4;
C5.text = textXML.category5;
C6.text = textXML.category6;
gotoAndStop(2);
}
Copy link to clipboard
Copied
The first frame only has actions in it the second frame has the text box. Tracing textXML displays my data but tracing C1 returns a null value. I checked to make sure the instance name is correct and it is
Copy link to clipboard
Copied
then there's no text fields on a frame 1 to fill them that's why you're getting the error 😕
it must be exist when you apply the code..
Copy link to clipboard
Copied
Well that makes sense but also raises another question. The test file that I was working in is not set up that way yet it works. There is nothing but actionscript in frame one and the text boxes are in frame two. Any idea why?
Copy link to clipboard
Copied
put the text fields on a new layer at frame 1, then select the second frame an press F5 to create a frame, NOT key frame.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now