C# Objectverwendung ohne Instanz "Fehlermeldung"

Abcd12345

Lt. Junior Grade
Registriert
März 2006
Beiträge
483
Objectverwendung ohne Instanz "Fehlermeldung" Erledigt

Sorry, das ich erneut hier mit einer Frage auftauchen muss aber ich habe wieder mal ein kleines Problem.

Ich versuche laut Compiler ein Objekt zu verwenden ohne, dass ich eine Instanz von diesem gebildet habe.

Object reference not set to an instance of an object. -> Das ist die orginale Fehlermeldung

Dies kann ich irgendwie nicht ganz nachvollziehen :confused_alt:

Hier ist der Quellcode:
Code:
        private AnimatedSprite[] m_ExplosivesSprite = new AnimatedSprite[31];
        private int[] m_ExplosivesStatus = new int [31]; // <- erschaffe ich hier nicht die vermissten Instanzen??
        private double[] m_fExplosivesTimer = new double[31];
        private int m_Bombkind;

        public Explosives(int Bombkind)
        {
            m_Bombkind = Bombkind;
        }

        public void Load(Texture2D TextureExplosives)
        {
            for (int i = 0; i < 31; i++)
            m_ExplosivesSprite[i].LoadGraphic(TextureExplosives, 1, 2, 40, 40, 2);// <- Hier bleibt er stehen (beim ersten Schleifen durchlauf also bei i = 0)

            //Explosivstatus für alle Bombs auf 0 setzen
            for (int i = 0; i < 31; i++)
                m_ExplosivesStatus[i] = 0;

            //Explosivstimer auf 0 setzen
            for (int i = 0; i < 31; i++)
                m_fExplosivesTimer[i] = 0;
        }

Die Aussagekräftigen Zeilen habe ich glaub ich makiert. Würde mich über Hilfe freuen :)
 
Zuletzt bearbeitet:
bin zwar kein C# Freund, aber mach doch für jedes m_ExplosivesSprite ein:

m_ExplosivesSprite = new AnimatedSprite();




---------------------------
lustig, irgendwie scheint Microsoft doch bei Java geklaut zu haben? ;)
 
Das ist eine Klasse die aus einem XNA Tut stammt. Ich hab die Klasse aber selber etwas angepasst für mein Bomberman :).

Ich hab leider immer noch keine Lösung für mein Problem gefunden. Wäre schön wenn jemand helfen könnte ;)

Code:
    class Explosives
    {
        private AnimatedSprite[] m_ExplosivesSprite;
        private int[] m_ExplosivesStatus = new int [31];
        private double[] m_fExplosivesTimer = new double[31];
        private int m_Bombkind;

        public Explosives(int Bombkind)
        {
            m_Bombkind = Bombkind;

            for(int i = 0; i < 31; i++)
            {
                m_ExplosivesSprite[i] = new AnimatedSprite(); //<---- Das geht leider auch nicht :(
            }
        }

        public void Load(Texture2D TextureExplosives)
        {
            for (int i = 0; i < 31; i++)
            {
                m_ExplosivesSprite[i].LoadGraphic(TextureExplosives, 1, 2, 40, 40, 2);
            }

            //Explosivstatus für alle Bombs auf 0 setzen
            for (int i = 0; i < 31; i++)
                m_ExplosivesStatus[i] = 0;

            //Explosivstimer auf 0 setzen
            for (int i = 0; i < 31; i++)
                m_fExplosivesTimer[i] = 0;
        }
 
Zuletzt bearbeitet:
ja, guck doch mal in die Dokumentation (api doc?) zu AnimatedSprite. Vielleicht verlangt der Konstruktor ja noch ein paar Parameter ;)
Oder welche Fehlermeldung erscheint jetzt an welcher Stelle?
 
AW: Objectverwendung ohne Instanz "Fehlermeldung" Hat sic herledigt

Ich hab jetzt noch ne Alternative getestet, die mir von der Syntax am besten gefällt:

Ich kriege aber weiterhin die Fehlermeldung, was ich auch mache

Code:
    class Explosives
    {
        private List<AnimatedSprite> m_ExplosivesSpriteList;
        private int[] m_ExplosivesStatus = new int [31];
        private double[] m_fExplosivesTimer = new double[31];
        private int m_Bombkind;
       
        

        public Explosives(int Bombkind)
        {
            m_Bombkind = Bombkind;

            AnimatedSprite Temp = new AnimatedSprite();

            for (int i = 0; i < 31; i++)
            {
               
                m_ExplosivesSpriteList.Add(Temp); <--- "Hier kommt die Fehlermeldung Object reference not set to an instance of an object. 
            }
           
        }


Ne also, Animated Sprite erwarted keine weiteren Parameter mehr.

Hier ist auch nochmal die AnimatedSprite Klasse falls das was hilft..

Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame6
{

    public class AnimatedSprite
    {
        public Texture2D Texture;       // Textur
        Logfile Log = new Logfile();

        

        private float m_TotalElapsed;   // Abgelaufene Zeit

        private int m_Rows;             // Anzahl der Zeilen
        private int m_Columns;          // Anzahl der Spalten
        private int m_Width;            // Breite eines Einzelbilds
        private int m_Height;           // Höhe eines Einzelbilds
        private float m_AnimationSpeed; // Bilder pro Sekunde

        //Property um AninamtionsSpeed zu changen
        public float AnimationSpeed
        {
            set
            {
             
                m_AnimationSpeed = value;
            }
        }

        private int m_CurrentRow;       // Aktuelle Zeile
        private int m_CurrentColumn;    // Aktuelle Spalte


        //Konstruktor -> nur fürs log
        public AnimatedSprite()
        {
            
        }


        public void LoadGraphic(
          Texture2D Texture,
          int Rows,
          int Columns,
          int Width,
          int Height,
          int AnimationSpeed

          
          )
        {
            this.Texture = Texture;
            m_Rows = Rows;
            m_Columns = Columns;
            m_Width = Width;
            m_Height = Height;
            m_AnimationSpeed = (float)1 / AnimationSpeed;


            m_TotalElapsed = 0;
            m_CurrentRow = 0;
            m_CurrentColumn = 0;
        }

        public void Update(float Elapsed)
        {

            m_TotalElapsed += Elapsed;
            if (m_TotalElapsed > m_AnimationSpeed)
            {
                m_TotalElapsed -= m_AnimationSpeed;

                m_CurrentColumn += 1;
                if (m_CurrentColumn >= m_Columns)
                {
                    m_CurrentRow += 1;
                    m_CurrentColumn = 0;

                    if (m_CurrentRow >= m_Rows)
                    {
                        m_CurrentRow = 0;
                    }
                }

            }

           

        }

        public void Draw(SpriteBatch Sprite, Vector2 Position, Color Color)
        {


            Sprite.Draw(
                Texture,
                new Rectangle((int)Position.X, (int)Position.Y, m_Width, m_Height),
                new Rectangle(
                  m_CurrentColumn * m_Width,
                  m_CurrentRow * m_Height,
                  m_Width, m_Height),
                Color
                );
        }
    }

}

Die Klasse funktioniert auch hab mit der schon an anderer Stelle einige Mänchen über den Bildschirm laufen lassen usw :)


Okey das war falsch:

Code:
private List<AnimatedSprite> m_ExplosivesSpriteList = new List<AnimatedSprite>(); //Liste für die Sprites
 
Zuletzt bearbeitet: (hat sich erledigt)
Hallo,

das, was CoolHandLuke schrieb, war schon richtig. Allerdings hast du dabei wieder einen Fehler eingebaut. Ich glaube, es müsste so aussehen:
Code:
class Explosives
{
 private AnimatedSprite[] m_ExplosivesSprite = new AnimatedSprite[31]; // (1)
 ...

 public Explosives(...)
 {
  ...
  
  for(int i = 0; i < 31; i++)
   m_ExplosivesSprite[i] = new AnimatedSprite(); // (2)
 }

 ...

}
In Schritt (1) erstellst du das Array, das Objekte vom Typ AnimatedSprite enthält. Die Länge des Arrays beträgt 31. Die enthaltenen Objekte wurden aber noch nicht instanziiert und sind daher null. Willst du die Objekte des Arrays verwenden, kracht es.
Deswegen musst du alle 31 Objekte vor Gebrauch erst wie in Schritt (2) instanziieren.

MfG
 
Zurück
Oben