How to Handlen NullReferenceException in my code

karthicmurugan

New member
Joined
Apr 17, 2020
Messages
3
Programming Experience
3-5
Public Structure HDDAll
Dim sHDD_Drive As String
Dim sHDD_FileSystem As String
Dim sHDD_VolumName As String
Dim sHDD_DriveType As String
Dim sHDD_TotalSize As Long
Dim sHDD_AvailableSize As Long
End Structure

Public Function GetAllDrive(sHAll() As HDDAll)

Dim a As String = ""
'structure HDDAll contain
Dim sHA() As HDDAll

For Each drive_info As DriveInfo In DriveInfo.GetDrives()
Select Case drive_info.DriveType
Case drive_info.DriveType.Fixed
For i As Integer = 0 To drive_info.DriveType.Fixed
sHA(i).sHDD_Drive = drive_info.Name.ToString
sHA(i).sHDD_DriveType = drive_info.DriveType.ToString
sHA(i).sHDD_VolumName = drive_info.VolumeLabel.ToString
sHA(i).sHDD_TotalSize = Val(drive_info.TotalSize)
sHA(i).sHDD_AvailableSize = Val(drive_info.AvailableFreeSpace)
Next
End Select
Next drive_info
sHAll = sHA

End Function
please any one rectify nullreference exception
 
Whenever an exception is thrown, the debugger tells you where. When the exception is thrown, check each reference on that line to determine which is null (Nothing). You can then follow the code backwards to where you thought that reference should have been set to see why it wasn't if it's not clear why it wasn't, set a breakpoint on that line and run your code again. Firstly, make sure the line is hit. Secondly, check whether the reference is set. Finally, if it is, step forward through the code to find where it is cleared. Once you have all that information, you will likely be able to fix the issue yourself but, if you can't, you can tell us a lot more about it.
 
On a different note, is there a specific reason that you're using that dodgy structure? If it's for a specific API then you have to do what you have to do. If it's because you think it's a good idea, I suggest that you think again. Ideally, that type would be a class and the names would be more sensible.

Also, why on Earth are you using Val there? Val converts a String to a Double. Why would you use it to convert a Long to a Double and then assign the result a field of type Long. That is nonsensical and pointless at best. You're also calling ToString on Strings. Again, nonsensical and pointless at best.
 
If someone posts to your thread and you don't understand what they posted, you don't report their post. Reporting posts is to notify moderators that there is something wrong with that post, e.g. abuse or spam. If you want to ask for further information then you just reply. If I was a moderator then I would never have been aware that you had incorrectly reported my post.

That said, you asked my to "show programmatically", which just demonstrates that you didn't bother reading what I wrote. There is no "programmatically" to show. I told you how to use the tools at your disposal to diagnose issues for yourself. The idea is that you read, research and then make an effort on your own behalf, not just sit there waiting for someone else to solve your problems for you. We all need help along the way and that's fine, but we can all do plenty to help ourselves solve our own issues and you have done nothing. Make an effort to follow the instructions I have provided. Even if you can't do it all, you can at least try to do some of it, which you haven't. If you have problems with English then do some research on how to debug code in your own language. In short, if you expect others to volunteer their time to help you, make some sort of effort to help yourself first. Even if you can't solve the problem, you can still provide much more relevant information about it.
 
Back
Top