简体   繁体   中英

c# - “NullReferenceException was unhandled” but List isn't null

I'm trying to get a ListBox (AlbumsListBox) to list everything in a List (AlbumList).

AlbumList and AlbumsListBox are both created on Form FormMain. A new Album (with Album.Name defined in NameTextBox.Text on FormAlbumAC) is created to go into AlbumList on Form FormAlbumAC.

From what I've seen, making the AlbumsList the datasource of the AlbumsListBox seems to be the right way to go. But I get the error "NullReferenceException was unhandled, object reference not set to instance of an object" when I run the program.

Ln 16 of the FormAlbumAC excerpt is where it occurs.

formMain.AlbumsListBox.DataSource = MusicCollection.FormMain.PublicVars.AlbumList;

I don't understand why this is happening, since the message box just before that point shows that AlbumList.Count = 1, so AblumList isn't null?

What am I doing wrong? Is this the right way to achieve what I want? How can I fix this? Any advice is appreciated, thank you.

Form FormAlbumAC:

private FormMain formMain;

public FormAlbumAC(FormMain callerInstance)
{
    InitializeComponent();
    formMain = callerInstance;
}

private void buttonSave_Click(object sender, EventArgs e)
{
    if (MusicCollection.FormMain.PublicVars.AlbumList.Count != 100)
    {
        MusicCollection.FormMain.PublicVars.AlbumList.Add(new Album(NameTextBox.Text));

        MessageBox.Show("New Album added: " + NameTextBox.Text);
        MessageBox.Show(MusicCollection.FormMain.PublicVars.AlbumList.Count.ToString());
        formMain.AlbumsListBox.DataSource = MusicCollection.FormMain.PublicVars.AlbumList;
        this.Close();
    }
    else
    {
        MessageBox.Show("No room for new album.");
        this.Close();
    }
}

Form FormMain:

public const int MAX_ALBUMS = 100;

public FormMain()
{
    InitializeComponent();
}

private void buttonAddAlbum_Click(object sender, EventArgs e)
{
    FormAlbumAC addAlbumForm = new FormAlbumAC(this);
    addAlbumForm.ShowDialog();
}

public static class PublicVars
{
    public static List<Album> AlbumList { get; set; }

    static PublicVars()
    {
        AlbumList = new List<Album>(MAX_ALBUMS);
    }
}

public ListBox AlbumListBox
{
    get
    {
        return AlbumListBox;
    }
}

The local variable private FormMain formMain; has never been initialized.
And thus is NULL when you use it on the failing line.

You are trying to use the information stored statically in the class FormMain through an instance variable of type FormMain. But this variable is NULL and cannot access the data.

You could remove the error using

 formMain = new FormMain();
 formMain.AlbumsListBox.DataSource = MusicCollection.FormMain.PublicVars.AlbumList;
 ....

but at this point I think you will have other problems because this local instance of FormMain is not the same instance of the FormMain that, I suppose, has created the current instance of FormAlbumAC
If my assumption is correct, then you need to pass the instance of FormMain that creates FormAlbumAC inside the class.

private FormMain formMain;

public FormAlbumAC(FormMain callerInstance)
{
    InitializeComponent();
    formMain = callerInstance;
}

and then, somewhere in FormMain, when you construct the FormAlbumAC

....
FormAlbumAC album = new FormAlbumAC(this);
album.ShowDialog();
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM