如何摆脱“对象引用未设置为对象的实例”。 [英] How do get rid of "Object reference not set to an instance of an object."
问题描述
我收到消息对象引用未设置为对象的实例。
我知道我没有正确初始化或定义变量chgbtch但是我找不到一个可以告诉我如何的例子。
我正在使用带ChargeItem的ChargeBatch类构建要通过JSON字符串传递的数据。我可以在类中反序列化,但是当我尝试填充类ChargeItem以将其序列化为JSON字符串时,我得到上述错误。
提前谢谢任何和所有的帮助。
以下是我的代码。
公开Class ChargeBatch
Public Property ChargeItems As List(Of ChargeItem)
Public Property CommunityKey As Long
Public Property EffectiveDate As String
End Class
'Charge Items
公共类ChargeItem
公共财产OwnerKey()As Long
公共财产ChargeCodeKey()As Long
公共财产金额()As Double
End Class
模块APIARcharge
Sub Main(ByVal ParamArray args()As String)
Dim iMyChrgItem As Integer = 0
Dim CommID As String = Nothing
Dim ARFile As String = Nothing
如果是args.Len gth< 2然后
抛出新的ApplicationException(传递给此模块的参数无效或没有。)
结束
否则
CommID = args(0)
ARFile = args( 1)
结束如果
如果不是File.Exists(ARFile)则
抛出新的ApplicationException(String.Format(文件[{0}]不存在。, ARFile))
结束
结束如果
'打开输入文件并发布
Dim chgbtch As New ChargeBatch
使用mystream As StreamReader = New StreamReader(ARFile) )
Dim recs As String
而不是mystream.EndOfStream
recs = mystream.ReadLine()。TrimEnd
chgbtch.ChargeItems(iMyChrgItem).OwnerKey =(CInt(Mid) ,11,10)))
chgbtch.ChargeItems(iMyChrgItem).ChargeCodeKey = CInt(Mid(recs,21,10))
chgbtch.ChargeItems(iMyChrgItem).Amount = CDbl(Mid(recs,47,9))chgbtch.CommunityKey = CInt(Mid(recs,1,10))
chgbtch.EffectiveDate = Format(Mids(recs,41) ,6),mm / dd / YYYYT00:00:00)
iMyChrgItem = iMyChrgItem + 1
结束时
结束使用
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(chgbtch)
End Sub
你做的时候初始化chgbtch执行:
Dim chgbtch 作为 < span class =code-keyword>新 ChargeBatch
错误很可能会出现
chgbtch.ChargeItems(iMyChrgItem).OwnerKey
因为ChargeItems尚未初始化。
有几种方法可以解决这个问题哪个更适合你。
初始化chgbtch之后你也可以初始化ChargeItems
chgbtch.ChargeItems = 新列表...
或者您可以创建一个构造函数您的类和构造函数初始化所有需要的变量。
您需要添加构造函数来初始化类中的任何引用属性。类似于:
公共 类 ChargeBatch
公开 属性 ChargeItems 作为列表( ChargeItem)
公共 属性 CommunityKey 作为 长
公共 属性 EffectiveDate 作为 字符串
公开 Sub 新()
ChargeItems = new 列表( ChargeItem)
结束 Sub
结束 类
然后在您的代码中,您需要添加一个新的ChargeItem
在尝试设置其属性之前,每个记录的列表。
NB我不编程VB.NET所以你需要验证上面的语法。
I am getting the message "Object reference not set to an instance of an object."
I know I am not initializing or defining the variable chgbtch correctly but I cannot find an example that can show me how.
I am using the class ChargeBatch with ChargeItem to build data to be passed via a JSON string. I can deserialize in the class fine but when I try to populate the class ChargeItem to serialize it into a JSON string, I get the above error.
Thank you in advance for any and all help.
Below is my code.
Public Class ChargeBatch Public Property ChargeItems As List(Of ChargeItem) Public Property CommunityKey As Long Public Property EffectiveDate As String End Class 'Charge Items Public Class ChargeItem Public Property OwnerKey() As Long Public Property ChargeCodeKey() As Long Public Property Amount() As Double End Class Module APIARcharge Sub Main(ByVal ParamArray args() As String) Dim iMyChrgItem As Integer = 0 Dim CommID As String = Nothing Dim ARFile As String = Nothing If args.Length < 2 Then Throw New ApplicationException("Invalid or no parameters passed to this module.") End Else CommID = args(0) ARFile = args(1) End If If Not File.Exists(ARFile) Then Throw New ApplicationException(String.Format("File [{0}] does not exist.", ARFile)) End End If ' Open input file and post Dim chgbtch As New ChargeBatch Using mystream As StreamReader = New StreamReader(ARFile) Dim recs As String While Not mystream.EndOfStream recs = mystream.ReadLine().TrimEnd chgbtch.ChargeItems(iMyChrgItem).OwnerKey = (CInt(Mid(recs, 11, 10))) chgbtch.ChargeItems(iMyChrgItem).ChargeCodeKey = CInt(Mid(recs, 21, 10)) chgbtch.ChargeItems(iMyChrgItem).Amount = CDbl(Mid(recs, 47, 9)) chgbtch.CommunityKey = CInt(Mid(recs, 1, 10)) chgbtch.EffectiveDate = Format(Mid(recs, 41, 6), "mm/dd/YYYYT00:00:00") iMyChrgItem = iMyChrgItem + 1 End While End Using Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(chgbtch) End Sub
You do initialize chgbtch when you do:
Dim chgbtch As New ChargeBatch
The error is likely coming on
chgbtch.ChargeItems(iMyChrgItem).OwnerKey
because ChargeItems has not been intialized.
There are a couple of ways of handling this and you decide which works better for you.
After you initialize chgbtch you could also initialize ChargeItems
chgbtch.ChargeItems = New List...
or you can create a Constructor for your Class and in the constructor initialize all of the needed variables.
You need to add a constructor to initialise any reference properties within your class. Something like:
Public Class ChargeBatch Public Property ChargeItems As List(Of ChargeItem) Public Property CommunityKey As Long Public Property EffectiveDate As String Public Sub New() ChargeItems = new List(Of ChargeItem) End Sub End Class
Then in your code, you will need to add a newChargeItem
to the list for each record before trying to set its properties.
NB I don't program VB.NET so you need to verify the syntax above.
这篇关于如何摆脱“对象引用未设置为对象的实例”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!