0

This is the code I have a problem on it I can't understund why when I debugg they send me that the System.NullReferenceException  and dt is nothing

        Dim dt As DataTable = PerformCRUD(Cmd)

        If dt.Rows.Count > 0 Then
            intRow = Convert.ToInt32(dt.Rows.Count.ToString())
        Else
            intRow = 0
        End If

        ToolStripStatusLabel1.Text = "Number of row(s):" & intRow.ToString()

    End Sub

End Class
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • `ToolStripStatusLabel1.Text = "Number of row(s):" & dt?.Rows.Count.ToString()`. Your `PerformCRUD(Cmd)` method returns a null DataTable. – Jimi Jul 03 '20 at 01:25
  • @Jimi, to be more precise, it returns `Nothing`, i.e. no `DataTable` at all. – jmcilhinney Jul 03 '20 at 01:31
  • @jmcilhinney Or an initialized DataTable object that is set to null, which is not exactly `Nothing`, since it may still contain non-public definitions that identify it as a DataTable (and not a DataSet, for example, or any other object type, for that matter), while the public object is null. – Jimi Jul 03 '20 at 01:53
  • @Jimi, *"an initialized DataTable object that is set to null"*. There is no such thing. A variable either refers to an object or it doesn't. If it refers to an object then it is not `Nothing` and thus cannot be the source of a `NullReferenceException`. If it doesn't refer to an object then it is `Nothing` and will be the source of a `NullReferenceException` if you attempt to access a member of an object via that reference. Unlike C#, VB does not differentiate between a variable explicitly set to `Nothing` and one not set, but that's just a compiler function in C#. Underneath, null is null. – jmcilhinney Jul 03 '20 at 02:00
  • @jmcilhinney It's a debugger *thing*, it applies to both C# and VB.Net. Try `Dim ds As DataSet = New DataSet() Dim dt As DataTable = New DataTable() ds = Nothing dt = Nothing`. Inspect the non-public properties of the two objects after setting both to `Nothing` or `null`, you'll see that are treated differently. Just details, though. – Jimi Jul 03 '20 at 02:12
  • The signature for `PerformCRUD()` scares me. It looks like it will force you to write code that's crazy-vulnerable to sql injection issues. You need a way to put parameters into an sql statement _without_ using string concatenation! – Joel Coehoorn Jul 03 '20 at 02:16
  • @Jimi, I'm seeing nothing but `Shared` members on those variables both before assigning objects to them and after setting them to `Nothing`, which is exactly what you'd expect, as they don't rely on an instance. I can't see any instance members in either case because the variables don't refer to an instance. Same goes for C#, whether I initialised the variables to `null` or not, i.e. I could see `static` members only on the variables. – jmcilhinney Jul 03 '20 at 02:44
  • @jmcilhinney No, of course there are no instance members. I'm referring to the `Shared Members -> Non-Public Members`: these are different and depend on the type. These become more interesting if you work on the debugger side instead of the debuggee's. These objects, in a way, maintain their *identity*. I was just explaining my *null DataTable*. – Jimi Jul 03 '20 at 02:59
  • @Jimi, `Shared` members and non-public members are not the same thing. `Shared` members can be public or non-public, while non-public members can be `Shared` or instance members. *"These objects, in a way, maintain their identity"*. There are no objects. A variable is not an object. A variable may or may not refer to an object. If it doesn't, trying to access a member of an object through it is invalid. Anyway, regardless of who's right, this argument doesn't relate directly to the question so I will end my input. – jmcilhinney Jul 03 '20 at 03:07
  • @jmcilhinney Well, I wasn't trying to determine who's *right* (about what?) -- I never said that a variable IS an object, I said that if you inspect the variable that refers to the object you instantiated and set to null, the Non-Public members are different and directly related to the object type. Anyway, I agree that the OP is probably not really interested in this kind of discussion. – Jimi Jul 03 '20 at 03:23

0 Answers0