nullreferenceexception - Null Reference Exception with ListViewItem -


i have following code:

class album {     private string name;      private music[] musics;     private const int max_musics = 100;     private listviewitem[] back;      public listviewitem[] gettitles()     {         (int = 0; < max_musics; i++)             if (musics[i].title != null)                 back[i] = new listviewitem(musics[i].title);          return back;     } } 

at line back[i] = new listviewitem(musics[i].title), nullreferenceexception. dont know why because in debuger musics[i].title has value , checked.

you need tell how many elements 'back' array have before enter elements e.g.

    public listviewitem[] gettitles()     {         = new listviewitem[max_musics]; // initialize array          (int = 0; < max_musics; i++)             if (musics[i].title != null)                 back[i] = new listviewitem(musics[i].title);          return back;     } 

also make sure max_musics not bigger size of musics array, otherwise hit exception


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -