Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:25:30+00:00 2026-06-09T16:25:30+00:00

I have three classes: a Main, a Player and a Car. The main creates

  • 0

I have three classes: a Main, a Player and a Car.

The main creates a Car and a Player.
The Car needs to trace the player’s x position.
But the code below is not working. I have researched and found that declaring it as static should fix the problem although it is not the best way to do it?

Main:

package { 

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends MovieClip
{       
    public static var _player:Player;
    public static var _car:Car;

    public function Main() 
    {
        createPlayer();
    }

    public function createPlayer():void
    {

        _player= new Player();
        _car = new Car();

        _player.x = stage.stageWidth / 2;
        _player.y = stage.stageHeight / 2;

        stage.addChild(_player);
        stage.addChild(_car);
    }

}

}

Car:

package { 

import flash.display.MovieClip;
import Main;

public class Player extends MovieClip
{
    public function Player()
    {
        trace(Main._car.x);

    }
}

}

But this causes an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Main/createPlayer()
at Main()

I want to know why this is giving me an error and what are the other good ways to do this?

//EDIT: Sorry _hero was my mistake, it is actually _car.
Thank You Very Much………..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:25:32+00:00Added an answer on June 9, 2026 at 4:25 pm

    First of all: THIS IS NOT A RIGHT WAY TO PROGRAM! You shouldn’t be using static properties everywhere.
    Secondly: Your _player instance is created before you assign new Car() to your _car instance, that’s why it’s still null. Try changing their lines, so that you have:

    _car = new Car();
    _player = new Player();
    

    Again, this will solve your problem, but your coding is not the right way. If you want to have a reference to your _car object, pass it in Player class’ constructor.

    P.S. Don’t forget to mark this post as an answer if it helped you.

    EDIT:

    Here is some code to get you started:

    In your Player class add a private property for storing a reference on your Car object. Then add your Player’s constructor a parameter, which you will pass when creating a new Player. Note that use this only if you want to store a reference to your Car object in your Main class too.

    package { 
    
    import flash.display.MovieClip;
    import Main;
    
    public class Player extends MovieClip
    {
        private var _car:Car; // add a property to store a reference to the Car object
    
        public function Player(car:Car) //add a parameter to your Player constructor to pass in a Car object
        {
            _car = car; //assign passed Car object to your property _car
            trace(_car.x) //now you can use _car as you wish
        }
    }
    }
    

    When defining a Player object, you’ll now need to pass in your Car object to it’s constructor. So do it like this:

    _player = new Player(_car);
    

    IF you don’t need a reference to your Car object in your Main class, or better to say if a Car is assigned to a Player, so that each Player would have it’s own Car, you’d better define a property in your Player Class, like this:

     package { 
    
    import flash.display.MovieClip;
    import Main;
    
    public class Player extends MovieClip
    {
        public var car:Car; // add a property to store a reference to the Car object, note that now we made the car a public property, so that it can be accessed from outside.
    
        public function Player() //add a parameter to your Player constructor to pass in a Car object
        {
            car = new Car(); // initialize your variable
            trace(car.x) //now you can use car as you wish from this class
        }
    }
    }
    

    Also note that this Car object we initialized in Player class can be accessed from you Main class like this:

    _player = new Player();
    trace(_player.car.x);
    

    I’d suggest you read basic concept of OOP (Object Oriented Programming), such as Encapsulation, Inheritance and Polymorphism. Search google for some good books which will help you get on your feet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code I have three classes as follows: Forum , Forum::Thread and Forum::Post
I have three classes, one main class, one GUI class which uses awt+swing to
My app contains three classes: main Activity, View_A, View_B. View_B needs to access a
I have 2 classes, a record and a player. In my main scene, I
Have three classes User, Group and Field. Many to many relationship on User /
I have three classes that I am using and have shortened for ease of
I have three classes: PurchaseOrder, PurchaseOrderLine, Item PurchaseOrder children are PurchaseOrderLine which is related
I have three classes, I am simply display my Facebook friends in tableView. And
I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.