0

When running my program I am receiving a weird error that I've been trying to fix for a while now. Edited:

        DataTable peers = new DataTable();

        ObjectSerialisation.FileInit();

        if (ObjectSerialisation.PeerExists() && !(ObjectSerialisation.PeersEmpty()))
        {
            peers = ObjectSerialisation.DeserialisePeers();
        }
        else
        {
            peers.Columns.Add("nick");
            peers.Columns.Add("address");
            peers.Rows.Add("xnbya", "4lxi4nv5kkhyks2x.onion");
            peers.Rows.Add("curly", "h3m36rpq35rv7n36.onion");
            peers.PrimaryKey = new DataColumn[] { peers.Columns["address"] };
        }

        lstRecentChats.DataSource = peers;
        lstRecentChats.DisplayMember = "nick";

The lstRecentChats.DataSource = peers; always throws:

An unhandled exception of type 'System.NullReferenceException' occurred in ************.exe

Additional information: Object reference not set to an instance of an object.

I have no idea why this is occurring as I have ensured the DataTable always contains data I have even tried deleting the currently stored data to force it to use that defined within the else code block I have no idea how to fix it I have tried using a static DataTable in another file but all to no avail. Any help would be greatly appreciated.

Edit 2:

The code can be distilled down to this: public partial class frmMain : Form { public frmMain() { DataTable peers = new DataTable();

        peers.Columns.Add("nick");
        peers.Columns.Add("address");
        peers.Rows.Add("xnbya", "4lxi4nv5kkhyks2x.onion");
        peers.Rows.Add("curly", "h3m36rpq35rv7n36.onion");
        peers.PrimaryKey = new DataColumn[] { peers.Columns["address"] };

        lstRecentChats.DataSource = peers;
        lstRecentChats.DisplayMember = "nick";
    }
}

The error still occurs within just this code.

4
  • And what about lstRecentChats ?
    – Steve
    Apr 6, 2014 at 22:26
  • If you get constantly a null reference exception on that line, then lstRecentChats is not initialized, but do you realize that DataTable peers and GlobalVars.Peers.peers are two totally different objects? If the if succeeds you set the DataSource to a null datatable.
    – Steve
    Apr 6, 2014 at 22:31
  • please provide context of the code where is this code being executed? witch method of the view Apr 6, 2014 at 22:31
  • @Steve That is an error due to the endless tinkering with this section of the code I should have really picked that up before commenting. Ill edit now. Apr 6, 2014 at 22:38

1 Answer 1

1

It seems lstRecentChats is null. If you put this code into your constructor before InitializeComponent method then you are trying to access lstRecentChats before it's initialized.So first call the InitializeComponent then set the DataSource.

5
  • This code is run during the constructor of a window how can I initialise it first? Apr 6, 2014 at 22:41
  • call InitializeComponent(); before lstRecentChats.DataSource = peers; and make sure you are calling it just once. Apr 6, 2014 at 22:44
  • Fantastic this seems to have solved the problem. For my own learning please may I ask why it is I must call InitializeComponent() rather than just using it? Apr 6, 2014 at 22:49
  • @ScottGriffiths From your solution explorer, open Form.Designer.cs and you will see that method initializes your components (all of your controls, for ex. buttons, labels, datagridviews) and setting the properties (font color, name, location etc.). that's why you should call InitializeComponent before trying to access any of your controls that you create with the Designer. Apr 6, 2014 at 22:55
  • Thank you very much that makes a lot more sense now. Apr 6, 2014 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.