Object reference not set to an instance of an object.

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
Hoi allen,

heb een class:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      private Form1 form1;

      private int score;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;

      private Rectangle boundaries;
      private Random random;

      //private List<Stars> stars = new List<Stars>();

      private Bitmap[] StarAnimation;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
      private Stars stars;

      public Game(Form1 form1)
      {
          this.form1 = form1;
          form1.game = this;
          InitializeImages();
      }

      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Method FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method

       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages

      public void Draw(Graphics g)
      {
          g.FillRectangle(Brushes.SkyBlue, form1.ClientRectangle);      
      }     

      public void Twinkle()
      {         
      }

      private int animationCell = 0;
      private int Frame = 0;
      public void AnimateObject()
      {
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          form1.Invalidate();
         

      }//End method 
    }
}
en een Form:

PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StarInvader2
{
    public partial class Form1 : Form
    {
        //Game game = new Game(Game);
        public Game game;
        public Form1()
        {
            InitializeComponent();
            //public Game game;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //Color color1 = Color.Black;
           // this.BackColor = Color.Black;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(e.Location.ToString());
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            game.Draw(e.Graphics);
        }
    }
}

Maar nu krijg ik bij deze line: game.Draw(e.Graphics); de volgende error:

Object reference not set to an instance of an object.

Alsvast enorm bedankt voor jullie hulp!!

Niels
 
Probeer eens;

[cpp]public Game game = new Game();[/cpp]

Dat zou moeten werken, omdat Game nog niet aangemaakt was (d.m.v. de ''new'' methode)

En eigenlijk zou het ook moeten werken zonder ''public'' ervoor, maar ik weet niet precies wat je er allemaal mee wilt doen :P

Hiero staat een stukje dat erg interessant is, als je meer wilt weten over dit soort errors:
http://codebetter.com/raymondlewall...tance-of-an-object-3-common-causes-in-vb-net/
 
Laatst bewerkt:
HOi,

dit had ik al geprobeerd, maar dan krijg ik deze error:


Error 1 'StarInvader2.Game' does not contain a constructor that takes 0 arguments.

Omdat in de Game class in de constructor een parameter wordt doorgegeven. Maar ik weet niet wat ik tussen de haakjes moet invullen: Game game = new Game(..)
 
Ik heb het nu zo:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      private Form1 form1;

      private int score;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;

      private Rectangle boundaries;
      private Random random;

      //private List<Stars> stars = new List<Stars>();

      private Bitmap[] StarAnimation;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
      private Stars stars;

      public Game(Form1 form1)
      {
          this.form1 = form1;
          form1.game = this;
          InitializeImages();
      }

      public Game()
      { }

      

      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Method FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method

       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages

      public void Draw(Graphics g)
      {
          g.FillRectangle(Brushes.SkyBlue, form1.ClientRectangle);      
      }

     

      public void Twinkle()
      { 
        
      }

      private int animationCell = 0;
      private int Frame = 0;
      public void AnimateObject()
      {
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          form1.Invalidate();
         

      }//End method 
    }
}

PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StarInvader2
{
    public partial class Form1 : Form
    {
       public Game game = new Game();
       //public Game game{get; set;}
        public Form1()
        {
            InitializeComponent();
            //public Game game;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //Color color1 = Color.Black;
           // this.BackColor = Color.Black;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(e.Location.ToString());
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            game.Draw(e.Graphics);
        }
    }
}

Maar nu krijg ik bij deze regel:
public void Draw(Graphics g)
{
g.FillRectangle(Brushes.SkyBlue, form1.ClientRectangle);
}
een error:
Object reference not set to an instance of an object.
 
Ik zou het helaas niet weten, ik ben niet zo van de graphics, drawing en dergelijken.

Alhoewel, een gokje; van Form1 moet je ook nog een ''new Form1();'' gebruiken.
 
Je bedoelt in ge Game class?

ja, dat heb ik gedaan:

PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace StarInvader2
{
  public  class Game
    {
      //private Form1 form1;
      public Form1 form1 = new Form1();

      private int score;
      private int livesLeft = 2;
      private int wave = 0;
      private int framesSkipped = 0;

      private Rectangle boundaries;
      private Random random;

      //private List<Stars> stars = new List<Stars>();

      private Bitmap[] StarAnimation;
      //private Direction invaderDirection;
      //private List<Invader>invaders;
      //private PlayerShip playership;
      //private List<Shot> playerShots;
     // private List<Shot> invaderShots;
      private Stars stars;

      public Game(Form1 form1)
      {
          this.form1 = form1;
          form1.game = this;
          InitializeImages();
      }

      public Game()
      { }

      

      public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
      {
          Bitmap bitmap = new Bitmap(Width, Height);
          using (Graphics graphics = Graphics.FromImage(bitmap))//Method FromImage
          {
              graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
          }
          return bitmap;
      }//End method

       public void InitializeImages()
       {         
          StarAnimation = new Bitmap[4];
          StarAnimation[0] = ResizeImage(Properties.Resources.star1, 10, 10);
          StarAnimation[1] = ResizeImage(Properties.Resources.star2, 10, 10);
          StarAnimation[2] = ResizeImage(Properties.Resources.star3, 10, 10);
          StarAnimation[3] = ResizeImage(Properties.Resources.star4, 10, 10);         
          PictureBox starPicture = new PictureBox();
          starPicture.Size = new Size(10, 10);
      }//End method InitializeImages

      public void Draw(Graphics g)
      {
          g.FillRectangle(Brushes.SkyBlue, form1.ClientRectangle);      
      }

     

      public void Twinkle()
      { 
        
      }

      private int animationCell = 0;
      private int Frame = 0;
      public void AnimateObject()
      {
          Frame++;
          if (Frame >= 6)
          {
              Frame = 0;
          }
          switch (Frame)
          {
              case 0: animationCell = 0; break;
              case 1: animationCell = 1; break;
              case 2: animationCell = 2; break;
              case 3: animationCell = 3; break;
              case 4: animationCell = 2; break;
              case 5: animationCell = 1; break;
              default: animationCell = 0; break;
          }
          form1.Invalidate();
         

      }//End method 
    }
}

Maar dan krijg ik deze error:
An unhandled exception of type 'System.StackOverflowException' occurred in StarInvader2.exe

bij deze regel:
public Form1 form1 = new Form1();

public Form1 form1 = new Form1();
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan