Sultan Mustafa
Honorary Master
- Joined
- Nov 7, 2020
- Messages
- 22,023
- Reaction score
- 14,995
Wut?Why do you hate go?
Wut?Why do you hate go?
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
System.NullReferenceException: 'Object reference not set to an instance of an object.'
elem was Nothing.
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.I am talking almost about 27 years ago...
Have you tried go?Wut?
No... Only heard of it. Why?Have you tried go?
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)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...)
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.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...
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.
The actual error message.System.NullReferenceException: 'Object reference not set to an instance of an object.'
elem was Nothing.
For example, I would do this:Good practice is to add error checking. I'm not sure what you mean that you checked stateObjectsArray with MessageBox.
MessageBox.Show(stateObjectsArray(enter index here).name/abbr/getDensity()/etc)
Yes.Did you check that all 50 lines have names?