System.NullReferenceException出错,请问是什么原因?如何解决?谢谢!
'申明一个类
Public Class Class_DBLink
Private m_name, m_dbtype, m_database As String
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
Public Property DatabaseType() As String
Get
Return m_dbtype
End Get
Set(ByVal value As String)
m_dbtype = value
End Set
End Property
Public Property Database() As String
Get
Return m_database
End Get
Set(ByVal value As String)
m_database = value
End Set
End Property
End Class
'读取ini文件代码
Module ModuleMain
'引用ini文件读写函数
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String) As Boolean
Public DBLinkCount As Integer '数据连接总数
Public DBLink() As Class_DBLink '申明数据连接类的实例
Public Sub GetAppSetting() '读取ini文件设置值
Dim Result As String
Dim i, LCount As Integer
On Error GoTo Err
'=======================================
Result = Space$(256)
GetPrivateProfileString("数据源", "总数", "1", Result, 255, Application.StartupPath & "\DBLink.ini")
Result = Trim(Result)
LCount = Result
For i = 1 To LCount '依次读取所有数据连接
If i = 1 Then '第1个数据源
ReDim DBLink(1)
Else
ReDim Preserve DBLink(i)
End If
Result = Space$(256)
GetPrivateProfileString(i, "Name", "数据源" & i.ToString, Result, 255, Application.StartupPath & "\DBLink.ini")
Result = Trim(Result.ToString)
DBLink(1).Name = Result.ToString '运行到这里时出错:System.NullReferenceException
Next
Exit Sub
Err:
MsgBox(Err.Description)
End Sub
End Module