Visual Basic - BindingNavigator is imploding my studio

Code:
Public Class frmStates

    Class State
        Public Property name As String
        Public Property abbr As String
        Public Property dateOfEntry As String
        Public Property landArea As Integer
        Public Property pop2015 As Integer

        Public Sub New(nameArg, abbrArg, dateOfEntryArg, landAreaArg, pop2015Arg)
            name = nameArg
            abbr = abbrArg
            dateOfEntry = dateOfEntryArg
            landArea = CInt(landAreaArg)
            pop2015 = CInt(pop2015Arg)
        End Sub

        Function getDensity()
            Return pop2015 / landArea
        End Function
    End Class

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim statesArray As String() = File.ReadAllLines("UnitedStates.txt")
        Dim stateObjectsArray(50) As State
        For i As Integer = 0 To statesArray.Length - 1
            Dim tempArray As String() = statesArray(i).Split(",c")
            stateObjectsArray(i) = New State(tempArray(0), tempArray(1), tempArray(2), tempArray(3), tempArray(4))
        Next

        Dim query = From elem In stateObjectsArray
                    Select elem.name

        dgvDisplay.DataSource = query.ToList()

    End Sub

End Class

Back at it again with another project. LINQ query isn't working because apparently this is wrong:
System.NullReferenceException: 'Object reference not set to an instance of an object.'

elem was Nothing.

What's hilarious is I tested the stateObjectsArray with MessageBox (like calling a single element of the array and calling its property/getDensity), and it returns everything it should. (Hardly "Nothing" as the error says...)
 
I am talking almost about 27 years ago...
Wrote a DTP type thing decades ago, it used a sql backend though but bridged through access so that folks could use the simple app interface and a LOT of custom reporting. Basically took odds and statistics for soccer matches, generated tables and commentary and then had to be dynamic enough that you could move stuff around so that the printing section could insert the ads.

That was oldschool , printing was still done on template type sheets that were created and then put though rollers. Mass production of publication.
 
Looking at the code, you should really add some error checking. For instance you read a line and just assume everything's fine.
What if the line's empty? What if the line doesn't have enough commas in it? What if you don't read 50 lines? What if one of the names are null? And so forth.

Even your linq query, select names where the names are not null.
 
Code:
Public Class frmStates

    Class State
        Public Property name As String
        Public Property abbr As String
        Public Property dateOfEntry As String
        Public Property landArea As Integer
        Public Property pop2015 As Integer

        Public Sub New(nameArg, abbrArg, dateOfEntryArg, landAreaArg, pop2015Arg)
            name = nameArg
            abbr = abbrArg
            dateOfEntry = dateOfEntryArg
            landArea = CInt(landAreaArg)
            pop2015 = CInt(pop2015Arg)
        End Sub

        Function getDensity()
            Return pop2015 / landArea
        End Function
    End Class

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim statesArray As String() = File.ReadAllLines("UnitedStates.txt")
        Dim stateObjectsArray(50) As State
        For i As Integer = 0 To statesArray.Length - 1
            Dim tempArray As String() = statesArray(i).Split(",c")
            stateObjectsArray(i) = New State(tempArray(0), tempArray(1), tempArray(2), tempArray(3), tempArray(4))
        Next

        Dim query = From elem In stateObjectsArray
                    Select elem.name

        dgvDisplay.DataSource = query.ToList()

    End Sub

End Class

Back at it again with another project. LINQ query isn't working because apparently this is wrong:


What's hilarious is I tested the stateObjectsArray with MessageBox (like calling a single element of the array and calling its property/getDensity), and it returns everything it should. (Hardly "Nothing" as the error says...)
I dont want to be the one to name the thing that shall not be named in a programming thread, but ***gpt will almost definitely solve your VBA questions - it did for me (but only experience I have is in excel - and I forgot most stuff I learned)
 
Looking at the code, you should really add some error checking. For instance you read a line and just assume everything's fine.
What if the line's empty? What if the line doesn't have enough commas in it? What if you don't read 50 lines? What if one of the names are null? And so forth.

Even your linq query, select names where the names are not null.
There's nothing wrong with the text file, I checked, 50 lines. I checked everything in stateObjectsArray with MessageBox. It all works as it should.

But for some reason, the LINQ query tells me the thing is Nothing when it really isn't...
 
There's nothing wrong with the text file, I checked, 50 lines. I checked everything in stateObjectsArray with MessageBox. It all works as it should.

But for some reason, the LINQ query tells me the thing is Nothing when it really isn't...

It won't tell you that there's a nothing value (I assume null value. I don't know VB, I'm a C#/C++ programmer) if there isn't one. That's not how programming works.

Good practice is to add error checking. I'm not sure what you mean that you checked stateObjectsArray with MessageBox. Did you check that all 50 lines have names?

Good practice is to not leave it to a human to check but that the code check for errors like that for you.
 
It won't tell you that there's a nothing value (I assume null value. I don't know VB, I'm a C#/C++ programmer) if there isn't one. That's not how programming works.
System.NullReferenceException: 'Object reference not set to an instance of an object.'

elem was Nothing.
The actual error message.
Good practice is to add error checking. I'm not sure what you mean that you checked stateObjectsArray with MessageBox.
For example, I would do this:
MessageBox.Show(stateObjectsArray(enter index here).name/abbr/getDensity()/etc)

And the requested value would display, no hitches.
Did you check that all 50 lines have names?
Yes.
 
Man, here I'm lying working through his code wanting to ask why he does not set stateObjectsArray length to statesArray length, and you gpt it in 5 seconds.

Also primesteak or whatever your moniker is now, why didn't you step though the code whilst running it?
 
Top
Sign up to the MyBroadband newsletter