views:

210

answers:

5

I am trying to save settings to an XML File and setting the relevant data if the check box is checked or not.

private static function createXMLData():void 
{
    prefsXML = <preferences/>;
    prefsXML.application.@windowsstart = Application.application.SettingsPage.settingWindowsStart.selected;
    prefsXML.application.@mintosystray = Application.application.SettingsPage.settingMinToSysTray.selected;
    //prefsXML.windowState.@x = stage.nativeWindow.x;
    //prefsXML.windowState.@y = stage.nativeWindow.y;
    prefsXML.saveDate = new Date().toString();
}

However when i run it, there is no values set on the check boxes due to the first time running and therefore i get an error.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

well i assume that is what the error means, it cannot get details of something that is not set yet.. so how would i get it to check and if nothing then it is obviously a "false".

Thank you.

A: 

You are trying to access the selected property of the checkbox, but the error is saying that the checkbox object itself is null - hence there is no selected property to get.

So, it doesn't look like the checkbox exists at all. You will need to create it first.

RodeoClown
Hi RodeoClown, the check box definitely does exist, FLEX picks it up using the tab completion feature.
medoix
It might be picking it up as it is defined in the file, so the IDE knows it exists, but it may not have been instantiated at the point in time you are trying to access it via the createXMLData() method.Again, that's just what it would appear the error message is saying - but I know I've hit plenty of misleading errors in the past too, so it could well be something different.
RodeoClown
A: 

One thing to realize is that the checkbox should always have a value once you create a data binding between the checkbox and some boolean field.

Justin Standard
A: 

I can't really be sure without looking at more code, but it seems like SettingsPage is the name of the class, not the instance of the class.

Raul Agrait
SettingsPage is an MXML page with the actual check boxes, and this code is in an ActionScript Class file.
medoix
A: 

You can easily work around the fact that the checkboxes may or may not exist by checking for them:

if(Application.application.SettingsPage["settingWindowsStart"])
    prefsXML.application.@windowsstart = Application.application.SettingsPage.settingWindowsStart.selected;
else
 prefsXML.application.@windowsstart = false;

This makes the assumption you mentioned of setting windowsstart to false if the component does not exist. Or alternatively you can use a try catch block:

try {
    prefsXML.application.@windowsstart = Application.application.SettingsPage.settingWindowsStart.selected;
}
catch(e:Error) {
    trace(e.getMessage());
    prefsXML.application.@windowsstart = false;
}

This will help get around the problem but I'd recommend at least making an attempt to understand what the problem is happening in the first place.

Fergal
that try may just work, i have already stated that the check boxes do in fact exist. The issue i am having is that the check boxes get the data from a binding in a class file. When the program is run for the first time, there is nothing set for it, hence i get the NULL error message.
medoix
I also believe there is an issue with data binding to a check box control where the data does not get sent back to the binding if the control is changed manually... do you know of a fix for this?
medoix
A: 

It looks like you are accessing the data before the application construction is complete (and therefore before the UI components exist). Try calling this code sometime after the applicationComplete event is dispatched.

<mx:Application ... applicationComplete="onApplicationComplete(event)">
  <mx:Script>
    private function onApplicationComplete(event:Event):void {
      createXMLData();
    }
  </mx:Script>
  ...
</mx:Application>
Michael Brewer-Davis