Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. error message

error message

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsgame-devperformancehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyclone_S
    wrote on last edited by
    #1

    Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.

    #include "stdafx.h"
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;

    public ref class Ball
    {
    public:
    PictureBox^ gameBall;
    int speed;
    double xVel;
    double yVel;

    		Ball() // Class constructor.
    		{
    			
    		}
    
    		void Add\_to\_form( Form ^ form ) 
    		{
    			// Variables
    			speed = 5;
    			xVel = speed;
    			yVel = speed;
    
    			gameBall = gcnew PictureBox();
    			gameBall->BackColor = Color::LimeGreen;
    			gameBall->Size = Drawing::Size(20,20);
    
    			gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50);
    			form->Controls->Add(gameBall);
    		}
    

    };

    public ref class Form1 : public Form
    {
    public:
    PictureBox^ paddlePlayer;
    Timer^ gameTimer;
    Ball ball1; // Create ball1.

    		Form1() // Class constructor.
    		{
    			this->Size = Drawing::Size(640,480);
    			this->BackColor= Color::SkyBlue;
    			this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove);
    			gameTimer = gcnew Timer();
    			gameTimer->Interval = 20;
    			gameTimer->Start();
    			gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick);
    
    			paddlePlayer = gcnew PictureBox();
    			paddlePlayer->BackColor = Color::LimeGreen;
    			paddlePlayer->Size = Drawing::Size(128,16);
    			paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65);
    			this->Controls->Add(paddlePlayer);
    
    			
    			this->Controls->Add(ball1.gameBall);
    			
    		}
    
    		
    		System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e)
    		{
    			if(e->X < this->Width-paddlePlayer->Width - 18)
    			{
    				paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y);
    			}
    		}
    		System::Void gameTimer\_Tick(System::Object^  sender, System::EventArgs^  e)
    		{
    			//Move the game ball.
    			ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); 
    
    			// Check for t
    
    S N 2 Replies Last reply
    0
    • C Cyclone_S

      Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.

      #include "stdafx.h"
      using namespace System;
      using namespace System::Drawing;
      using namespace System::Windows::Forms;

      public ref class Ball
      {
      public:
      PictureBox^ gameBall;
      int speed;
      double xVel;
      double yVel;

      		Ball() // Class constructor.
      		{
      			
      		}
      
      		void Add\_to\_form( Form ^ form ) 
      		{
      			// Variables
      			speed = 5;
      			xVel = speed;
      			yVel = speed;
      
      			gameBall = gcnew PictureBox();
      			gameBall->BackColor = Color::LimeGreen;
      			gameBall->Size = Drawing::Size(20,20);
      
      			gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50);
      			form->Controls->Add(gameBall);
      		}
      

      };

      public ref class Form1 : public Form
      {
      public:
      PictureBox^ paddlePlayer;
      Timer^ gameTimer;
      Ball ball1; // Create ball1.

      		Form1() // Class constructor.
      		{
      			this->Size = Drawing::Size(640,480);
      			this->BackColor= Color::SkyBlue;
      			this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove);
      			gameTimer = gcnew Timer();
      			gameTimer->Interval = 20;
      			gameTimer->Start();
      			gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick);
      
      			paddlePlayer = gcnew PictureBox();
      			paddlePlayer->BackColor = Color::LimeGreen;
      			paddlePlayer->Size = Drawing::Size(128,16);
      			paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65);
      			this->Controls->Add(paddlePlayer);
      
      			
      			this->Controls->Add(ball1.gameBall);
      			
      		}
      
      		
      		System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e)
      		{
      			if(e->X < this->Width-paddlePlayer->Width - 18)
      			{
      				paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y);
      			}
      		}
      		System::Void gameTimer\_Tick(System::Object^  sender, System::EventArgs^  e)
      		{
      			//Move the game ball.
      			ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); 
      
      			// Check for t
      
      S Offline
      S Offline
      ShilpiP
      wrote on last edited by
      #2

      Please repost your message in Managed c++/CLI forum.

      "Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN)

      1 Reply Last reply
      0
      • C Cyclone_S

        Hi, I am trying to make a brick game. I want to have a class for the players ball. The ball will split in 3 when a certain item is collected. The code compiles fine but when it runs it gives me the error "Object reference not set to an instance of an object." Also what's the best way of writing the code for 3 ball objects without having to duplicate code 3 times. Thanks.

        #include "stdafx.h"
        using namespace System;
        using namespace System::Drawing;
        using namespace System::Windows::Forms;

        public ref class Ball
        {
        public:
        PictureBox^ gameBall;
        int speed;
        double xVel;
        double yVel;

        		Ball() // Class constructor.
        		{
        			
        		}
        
        		void Add\_to\_form( Form ^ form ) 
        		{
        			// Variables
        			speed = 5;
        			xVel = speed;
        			yVel = speed;
        
        			gameBall = gcnew PictureBox();
        			gameBall->BackColor = Color::LimeGreen;
        			gameBall->Size = Drawing::Size(20,20);
        
        			gameBall->Location = Drawing::Point(form->Width/2-35,form->Height/2-50);
        			form->Controls->Add(gameBall);
        		}
        

        };

        public ref class Form1 : public Form
        {
        public:
        PictureBox^ paddlePlayer;
        Timer^ gameTimer;
        Ball ball1; // Create ball1.

        		Form1() // Class constructor.
        		{
        			this->Size = Drawing::Size(640,480);
        			this->BackColor= Color::SkyBlue;
        			this->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::pongMain\_MouseMove);
        			gameTimer = gcnew Timer();
        			gameTimer->Interval = 20;
        			gameTimer->Start();
        			gameTimer->Tick += gcnew System::EventHandler(this, &Form1::gameTimer\_Tick);
        
        			paddlePlayer = gcnew PictureBox();
        			paddlePlayer->BackColor = Color::LimeGreen;
        			paddlePlayer->Size = Drawing::Size(128,16);
        			paddlePlayer->Location = Drawing::Point(this->Width/2,this->Height-65);
        			this->Controls->Add(paddlePlayer);
        
        			
        			this->Controls->Add(ball1.gameBall);
        			
        		}
        
        		
        		System::Void pongMain\_MouseMove( Object^, System::Windows::Forms::MouseEventArgs^ e)
        		{
        			if(e->X < this->Width-paddlePlayer->Width - 18)
        			{
        				paddlePlayer->Location = Drawing::Point(e->X,paddlePlayer->Location.Y);
        			}
        		}
        		System::Void gameTimer\_Tick(System::Object^  sender, System::EventArgs^  e)
        		{
        			//Move the game ball.
        			ball1.gameBall->Location = Point(ball1.gameBall->Location.X + Convert::ToUInt32(ball1.xVel), ball1.gameBall->Location.Y + Convert::ToUInt32(ball1.yVel)); 
        
        			// Check for t
        
        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        Just create three instances of your Ball class. Instead of managing a single Ball instance, use a list of Balls, even when you only have one entry. Iterate over the list of balls each frame.

        home

        C 1 Reply Last reply
        0
        • N Niklas L

          Just create three instances of your Ball class. Instead of managing a single Ball instance, use a list of Balls, even when you only have one entry. Iterate over the list of balls each frame.

          home

          C Offline
          C Offline
          Cyclone_S
          wrote on last edited by
          #4

          Thanks for the suggestion. It helped.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups