-1

I'm trying to realize the listBox with List where i getting some variable. And when i wonna to add a new "Object" to static List i getting a NullReferenceException

This is my code where i adding a new List

       if (EventArgsIn.Message.Chat.Id == MainChatId)
        {
            if (EventArgsIn.Message.ReplyToMessage != null)
            {
                var _tempMessage = new listBoxMessage()
                {
                    From = EventArgsIn.Message.From.Username,
                    FromId = EventArgsIn.Message.From.Id,
                    MessageId = EventArgsIn.Message.MessageId,
                    MessageText = EventArgsIn.Message.Text,
                    MessageIdReply = 0
                };
                tempMessageMain.Add(_tempMessage);
            } else
            {

                var _tempMessage = new listBoxMessage() {
                
                    From = EventArgsIn.Message.From.Username,
                    FromId = EventArgsIn.Message.From.Id,
                    MessageId = EventArgsIn.Message.MessageId,
                    MessageText = EventArgsIn.Message.Text,
                    MessageIdReply = 0
                };
                tempMessageMain.Add(_tempMessage);
                
            }
        }

And here is my static List public static List<listBoxMessage> tempMessageMain;

A-a-and my class where i doing Template

public class listBoxMessage
    {
        public listBoxMessage()
        {
        }

        public string From { get; set; }
        public int FromId { get; set; }
        public int MessageId { get; set; }
        public string MessageText { get; set; }
        public int MessageIdReply { get; set; }
    }
}

This is test code*

  • 2
    you are not initializing `tempMessageMain`. You need `tempMessageMain = new List();` at some place in your code. At least I don't see it here. – Marshal Dec 17 '20 at 19:09
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jawad Dec 17 '20 at 19:14
  • @Marshal, then what's this `public static List tempMessageMain;` – Timur Jesur Dec 17 '20 at 19:16
  • @TimurJesur thats the place where you define an object.. you have to initialize that as well. When you declare a variable, its null... Perhaps a read [through here](https://www.pluralsight.com/guides/declaring-and-initializing-variables-in-c) would help in understanding the difference – Jawad Dec 17 '20 at 19:17
  • `tempMessageMain.Add(_tempMessage);` is adding a list as an item to the `tempMessageMain`. But, as I understand your description, you want to add the list to the list `tempMessageMain`. (see: https://stackoverflow.com/questions/1825568/append-a-lists-contents-to-another-list-c-sharp/1825577) – Luuk Dec 17 '20 at 19:17
  • This is correct! Very thankfull for all comments, and small question, what a event (for listBox) when list is updating ? – Timur Jesur Dec 17 '20 at 19:25

1 Answers1

1

You declared a listbox with next line:

public static List<listBoxMessage> tempMessageMain;

The value of tempMessageMain is still null.

Now you have to create a new instance of tempMessageMain:

public static List<listBoxMessage> tempMessageMain = new List<listBoxMessage>();
Marshal
  • 6,233
  • 12
  • 52
  • 86
Rudy Meijer
  • 171
  • 1
  • 7