Say I have a basic class "Code.as"
And I also have class "Code2.as" that extends "Code.as"
In "Code.as" I have the line "public static var code2:Code = new Code2();"
I keep getting this error:
"TypeError: Error #1009: Cannot access a property or method of a null object reference"
at "Code2.as"s class declaration.
Why? I can't figure it out.
If "Code2.as" extends "Code.as" I thought it would be alright to address it as a "Code" as well...
The correct line should be:
public static var code2:Code2 = new Code2();
Also make sure you import "Code2":
import Code2;
Related
So I have this superclass grid class, and a subclass of the grid class named GrassTile1, GrassTile2, etc... all of the instance of the subclasses are stored in an array. How am I suppose to convert the instance of subclass to its superclass referencing to the array?
private var backgroundGrid = []; //the array which the grids are stored in, in the main class.
public class Grid extends MovieClip
{
protected var node :PathfindNode; //the variable I wish to access, from an instance of subclass.
public function Grid(){
node = new PathfindNode();
}
}
public class GrassTile1 extends Grid { //every subclass of Grid will extends Grid
public function GrassTile1() {
// constructor code
}
}
function getBackgroundGrid(i:int,j:int):Grid{ //in the main class
return Grid(backgroundGrid[i][j]); // this line gives me an error
}
TypeError: Error #1034: Type Coercion failed: cannot convert GrassTile1#2905d5f1 to Grid.
I've tried accessing backgroundGrid[i][j].node and other ways to work around that I could think of and failed. Any Idea?
Try :
return backgroundGrid[i][j] as Grid;
Personally, Grid seems like a bad class name to use. I think Tile makes more sense, as that GrassTile1 is not a grid as I logically understand a grid. A grid might contain a collection of tiles, so doesn't sound logical to use that as a class name for tiles.
Also, where is the line where you actually call the getBackgroundGrid method ? You should try casting there, as opposed to in that method. I believe that will solve the problem.
I can't verify the line throwing the error, so we are assuming that it's the return statement. But, it could be on the other side where you are calling getBackgroundGrid.
UPDATE : I have tried a .fla using what you are describing and it works just fine, I get no error. Which is why I'm thinking we are missing something here and maybe the definition of the class is not being used. Can you put a trace in your constructors to verify what you expect is actually happening ?
I am trying to target a variable in the main .as file (The one that acts as the stage) from another .as file.
public var stageRef:MovieClip = root as MovieClip;
or
MovieClip(root)variable = 10;
don't seem to want to work for me. Neither of them produce any compile errors but when I try to use them they give me a 1009 error, cannot access a property or a null object reference. Any ideas of how i would go about doing this? Thanks in advance.
Im your Main.as class make the variable public. Here's an example:
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
public function Main()
{
}
}
}
DEFAULT_VALUE is optional. VARIABLE_TYPE is recommended, if not specified the type will be Object by default.
There are many ways to pass a variable to another class. If the class is created inside the Main class, just pass the variable to that class like this:
var myOtherClass:OtherClass = new OtherClass(YOUR_VAR_HERE);
or
var myOtherClass:OtherClass = new OtherClass();
myOtherClass.varReference = YOUR_VAR_HERE;
In first case make sure the constructor is expecting a variable. In the second, make sure the OtherClass has a public variable varReference that you can access and modify.
Another way loved by newbie programmers are static (singleton) variables: in the Main class specify your variable as such:
public static var YOUR_VAR_HERE:VARIABLE_TYPE = DEFAULT_VALUE;
Then you can access YOUR_VAR_HERE simply by referring to the class Main. Like this:
trace(Main.YOUR_VAR_HERE);
NOTE: it's considered to use all uppercase letters for constants, not variables, in this case I used all caps for readability.
for example is it possible to do somehing like public var socket:flash.net.Socket = new flash.net.Socket();?
No you will still get a class undefined compiler error if you don't import flash.net.Socket
Here's my class signature:
public class YouTubeControls extends Controls implements IControls
YouTubeControls has a public var foo. This code:
var controls:IControls = new YouTubeControls();
trace(controls.foo);
results in this error:
Access of possibly undefined property foo through a reference with static type IControls.
My application is going to have other "Control" classes, so casting controls (YouTubeControls(controls)) won't work. How can I access controls.foo?
Edit
If I can't do this without casting, how do I handle the problem of needing to know which class to cast it as?
trace(controls.foo); is the same as calling IControl(controls).foo since you controls variable is declared to be of type IControl. The problem is that you did not give the IControl interface a getter function foo. Note, properties are not allowed in interfaces, only methods. See the other answers here.
If foo is defined in YouTubeControls, you will not be able to access it through a reference to IControls. If you change your code to this, it will work:
var ytControls:YouTubeControls = new YouTubeControls();
trace(ytControls.foo);
var controls:IControls = ytControls;
However, you mentioned that other controls may also have a foo property; if that's the case, then you should define that property in IControls, not YouTubeControls.
I don't have access to Flash Builder at the moment, but I believe that you should be able to do use the 'as' operator to test if the object is one class or another.
if ((controls as YouTubeControls) != null) //controls IS a YouTubeControls
//because it didn't return null
trace((controls as YouTubeControls).foo);
else
...
The advantage to the 'as' operator is that it attempts to cast, but if it fails it returns null, while the other form of casting...
YouTubeControls(controls)
Will throw a runtime exception if controls cannot be cast as a YouTubeControls.
If you have several IControls you may want to extend this interface.
public interface IMyControl extends IControl
{
public function get foo():SomeType;
}
And then
public class YouTubeControls extends Controls implements IMyControl
in each of your controls class.
I'm trying to place a class into a package where another public class is placed. The documentation says that only one external visible declaration can be put in a package.
So i declare the second class internal. But then it gives the following error:
5006: An ActionScript file can not have more than one externally visible definition: character.AnimatedCharacterClass, character.CharacterPositions
The code I use is:
internal class CharacterPositions
{
public static const BEGIN_WALK:String = 'begin_walk';
public static const END_WALK:String = 'end_walk';
public static const STAND:String = 'stand';
}
Does anyone have a clue what is happening here?
I found that i have to put the second class outside the package. It still confuses me though.