nullreferenceexception未处理c#错误(类和对象)

nullreferenceexception未处理c#错误(类和对象),c#,class,object,nullreferenceexception,C#,Class,Object,Nullreferenceexception,这里是错误 对象引用未设置为对象的实例 private void addSongToolStripMenuItem_Click(object sender, EventArgs e) { string tempTitle; int tempLength = 0; CD myCD = new CD(); frmAddSong newSong = new frmAddSong(); newSong.Sho

这里是错误 对象引用未设置为对象的实例

private void addSongToolStripMenuItem_Click(object sender, EventArgs e)
    {

        string tempTitle;
        int tempLength = 0;
        CD myCD = new CD();

        frmAddSong newSong = new frmAddSong();


        newSong.ShowDialog();

        tempLength = Convert.ToInt32(newSong.txtSongLength.Text);
        tempTitle = newSong.txtSongTitle.Text;

        //index is a form level variable
        myCD.songList[index].SongLength = tempLength;// I get the error here
        myCD.songList[index].SongTitle = tempTitle;// or here

        MessageBox.Show(tempTitle + " " + tempLength.ToString());//Confirmation
        index++;
    }
这是我的问题:我有两个类,Song和CD在CD中是一个Song数组,每当我试图访问Song数组来设置或获取属性时,我都会得到一个错误

这是我的CD课:

public class CD
   {
    private string cdtitle;
    private string artist;
    public Song[] songList = new Song[10];
    private int releasedate;

    //constructors
    public CD()
    {
        cdtitle = artist = "";
        releasedate = 2014;
        numCDs = 0;
    }

    public CD(string t, string a)
    {
        cdtitle = t;
        artist = a;
        numCDs = 0;
    }

    //Properties
    public string CDTitle
    {
    get
    {
        return cdtitle;
    }
    set
    {
        cdtitle = value;
    }

    }

    public string CDArtist
    {
        get
        {
            return artist;
        }
        set
        {
            artist = value;
        }

    }

    public int ReleaseDate
    {
        get
        {
            return releasedate;
        }
        set
            {
                releasedate = value;
            }

        }
    }
这是歌曲课

public class Song
{   //attributes
    private string songtitle;
    private int songlength; //seconds


    //constructors
    //default constructor
    public Song()
    {
        songtitle = "";
        songlength = 0;
    }


    //initialize the song title only
    public Song(string st)
    {
        songtitle = st;
        songlength = 0;
    }
    //initialize the length only
    public Song(int len)
    {
        songtitle = "";
        songlength = len;
    }

    //Properties
    public string SongTitle
    {
        get
        {
            return songtitle;
        }

        set
        {
            songtitle = value;
        }
    }

    public int SongLength
    {

        get
        {
            return songlength;
        }

        set
        {
           songlength = value;
        }

    }
}
每当我试图从cd对象访问歌曲列表时,都会出现nullreferenceexception错误 对象引用未设置为对象的实例

private void addSongToolStripMenuItem_Click(object sender, EventArgs e)
    {

        string tempTitle;
        int tempLength = 0;
        CD myCD = new CD();

        frmAddSong newSong = new frmAddSong();


        newSong.ShowDialog();

        tempLength = Convert.ToInt32(newSong.txtSongLength.Text);
        tempTitle = newSong.txtSongTitle.Text;

        //index is a form level variable
        myCD.songList[index].SongLength = tempLength;// I get the error here
        myCD.songList[index].SongTitle = tempTitle;// or here

        MessageBox.Show(tempTitle + " " + tempLength.ToString());//Confirmation
        index++;
    }

在CD类中,创建sond数组,但不要在此数组中添加任何Song对象。因为这一行中的数组是空的myCD.songList[index].SongLength=tempLength;您可以尝试访问不存在的对象并调用此对象SongLength属性。这就是为什么你会犯错误。 要修复它,您需要先创建一个歌曲对象吗?并将其添加到CD对象的阵列中

Song s = new Song();
myCD.songList[index] = s;
之后,你可以使用

myCD.songList[index].SongLength = tempLength;

几乎所有NullReferenceException都是相同的。您正试图访问一个空对象的属性(这里是因为您的歌曲列表中在某个索引处没有歌曲)。最好的做法是设置一个断点并调试程序,以查看输入为空的原因并更正它。在访问它之前,您需要在
myCD.songList[index]
上创建一个新的
Song
对象。感谢您的odpro,它非常简单,但给了我几个小时的麻烦。