Object reference not set to an instance of an object (which is not null)

2

I'm trying to show the amount of free space on a drive (the C :) but I get the error "Additional information: Object reference not set to an instance of an object".

When I use MessageBox to show the free space it returns in bytes the value without problems, but when I try to assign the value to a Label I get the error. The code is as follows:

private void SetMainPage()
{
    DriveInfo C = new DriveInfo("C");
    espacoHD.Text = C.TotalFreeSpace.ToString();
}
    
asked by anonymous 29.08.2017 / 23:10

1 answer

0

Possibly the HD space is that it is null. Here is the code I did and it worked.

DriveInfo C = new DriveInfo("C");

Label espacoHD = new Label();
espacoHD.Text = C.TotalFreeSpace.ToString();
    
30.08.2017 / 00:02