Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Code works but not when copied to another file

Guest
Oct 01, 2015 Oct 01, 2015

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.

Screenshot 1.jpg

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);

}

screenshot 2.jpg

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!!

TOPICS
ActionScript
288
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 02, 2015 Oct 02, 2015

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 02, 2015 Oct 02, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 02, 2015 Oct 02, 2015

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..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 02, 2015 Oct 02, 2015
LATEST

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 02, 2015 Oct 02, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines