System.NullReferenceException: переменная объекта или переменная блока не установлены. VB.net
Мне было поручено преобразовать сценарий Excel VBA в отдельное приложение Windows для VB.net. Я никогда раньше не пользовался VB.net и не могу решить эту проблему. Я заранее прошу прощения, если мой код является катастрофой, так как я узнаю, как VB.net работает на ходу.
MainForm.vb
Public Partial Class MainForm
Dim Sessions As Object
Dim System As Object
Dim Sess0 As Object
Dim oldSystemTimeout&
Dim g_HostSettleTime As Double
Public Sub New()
Me.InitializeComponent()
End Sub
Public Sub costs_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles costs_box.TextChanged, other_box.TextChanged, poc_box.TextChanged
Dim total As Double
Dim costs As Double
Dim other As Double
Dim poc As Double
costs = Val(costs_box.Text)
other = Val(other_box.Text)
poc = Val(poc_box.Text)
total = costs + other + poc
totalCosts_box.Text = total.ToString("C2")
End Sub
Public Sub seller_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sellerLump_box.TextChanged, sellerSpecific_box.TextChanged
Dim total As Double
Dim lump As Double
Dim specific As Double
lump = Val(sellerLump_box.Text)
specific = Val(sellerSpecific_box.Text)
total = lump + specific
totalSeller_box.Text = total.ToString("C2")
End Sub
Public Sub credits_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sellerLump_box.TextChanged, sellerSpecific_box.TextChanged, dbCredit_box.TextChanged, lenderCredit_box.TextChanged
Dim total As Double
Dim lump As Double
Dim specific As Double
Dim db As Double
Dim lender As Double
lump = Val(sellerLump_box.Text)
specific = Val(sellerSpecific_box.Text)
db = Val(dbCredit_box.Text)
lender = Val(lenderCredit_box.Text)
total = lump + specific + db + lender
totalCredits_box.Text = total.ToString("C2")
End Sub
Public Sub Clear_buttonClick(sender As Object, e As EventArgs)
loanNumber_box.Clear
costs_box.Clear
other_box.Clear
poc_box.Clear
sellerLump_box.Clear
sellerSpecific_box.Clear
dbCredit_box.Clear
lenderCredit_box.Clear
loanType_box.Text = ""
loanFamily_box.Text = ""
propertyType_box.Text = ""
salesPrice_box.Text = ""
appValue_box.Text = ""
cltv_box.Text = ""
contribLimit_box.Text = ""
decisionUpper_box.Text = ""
decisionLower_box.Text = ""
salesPrice_box.ForeColor = SystemColors.ControlText
appValue_box.ForeColor = SystemColors.ControlText
decisionUpper_box.ForeColor = SystemColors.ControlText
decisionLower_box.ForeColor = SystemColors.ControlText
End Sub
Public Sub Run_buttonClick(sender As Object, e As EventArgs)
LPS()
End Sub
Public Sub LPS()
'--------------------------------------------------------------------------------
' Get the main system object
System = CreateObject("EXTRA.System") ' Gets the system object
If (System Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
Sessions = System.Sessions
If (Sessions Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
'--------------------------------------------------------------------------------
' Set the default wait timeout value
g_HostSettleTime = 50 ' Milliseconds Change As Needed. If retailChoice = 0 Then g_HostSettleTime = 150
oldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > oldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If
' Get the necessary Session Object
Sess0 = System.ActiveSession
If (Sess0 Is Nothing) Then
MsgBox("Please open an NTS session")
Exit Sub
End If
If Not Sess0.Visible Then Sess0.Visible = True
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
' Do not edit code above this line.
' Place your code here:
Dim loanNumber As String
Dim loanType As String
Dim loanFamily As String
Dim salesPrice As Double
Dim appraisedValue As Double
Dim propertyType As String
Dim cltv As String
Dim adjustedCLTV As Double
Dim limit As Double
Dim familyCounter As Integer
Dim propertyCounter As Integer
Dim priceCounter As Integer
Dim contribLimit As Double
Dim totalCredits As Double
Dim totalCosts As Double
loanNumber = loanNumber_box.Text
If loanNumber = "" Then
MsgBox("Please enter valid loan number", vbOKOnly)
loanNumber = ""
Exit Sub
Else
'LPS
If Mid(loanNumber, 1, 2) = "02" Or Mid(loanNumber, 1, 2) = "04" Then
loanType_box.Text = ""
loanFamily_box.Text = ""
propertyType_box.Text = ""
salesPrice_box.Text = ""
appValue_box.Text = ""
cltv_box.Text = ""
contribLimit_box.Text = ""
salesPrice_box.BackColor = SystemColors.Control
appValue_box.BackColor = SystemColors.Control
Sess0.screen.SendKeys ("<Reset><Clear>sign<Enter>61<Enter>")
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
Sess0.screen.PutString ("POINTS", 1, 3)
Sess0.screen.PutString (loanNumber, 1, 12)
Sess0.screen.SendKeys ("<ENTER>")
Sess0.screen.WaitHostQuiet (g_HostSettleTime)
loanType = Sess0.screen.getstring(6, 48, 1)
loanFamily = Sess0.screen.getstring(9, 23, 1)
salesPrice = Val(Sess0.screen.getstring(8, 11, 10))
appraisedValue = Val(Sess0.screen.getstring(8, 29, 10))
propertyType = Sess0.screen.getstring(7, 6, 4)
cltv = Sess0.screen.getstring(9, 73, 6)
adjustedCLTV = cltv / 100
totalCredits = totalCredits_box.Text
totalCosts = totalCosts_box.Text
If loanType = "C" Then
loanType_box.Text = "Conventional"
If loanFamily = "C" Then
loanFamily_box.Text = "Conforming"
familyCounter = 1
ElseIf loanFamily = "N" Then
loanFamily_box.Text = "Non-conforming"
familyCounter = 2
End If
If propertyType = "PRIM" Then
propertyType_box.Text = "Primary"
propertyCounter = 1
ElseIf propertyType = "SECN" Then
propertyType_box.Text = "Secondary"
propertyCounter = 2
ElseIf propertyType = "INVR" Then
propertyType_box.Text = "Investment"
propertyCounter = 3
End If
salesPrice_box.Text = salesPrice.ToString("C2")
appValue_box.Text = appraisedValue.ToString("C2")
If salesPrice < appraisedValue Then
priceCounter = 1
salesPrice_box.ForeColor = Color.ForestGreen
ElseIf salesPrice > appraisedValue Then
priceCounter = 2
appValue_box.ForeColor = Color.ForestGreen
ElseIf salesPrice = appraisedValue Then
priceCounter = 1
salesPrice_box.ForeColor = Color.ForestGreen
appValue_box.ForeColor = Color.ForestGreen
End If
cltv_box.Text = adjustedCLTV.ToString("P2")
ElseIf loanType <> "C" Then
MsgBox("Loan " & loanNumber & " is not a conventional loan", vbOKOnly)
Exit Sub
End If
If familyCounter = 1 Then
If propertyCounter = 1 Or propertyCounter = 2 Then
If cltv > 90# Then
limit = 0.03
ElseIf cltv > 75# And cltv <= 90# Then
limit = 0.06
ElseIf cltv <= 75# Then
limit = 0.09
End If
ElseIf propertyCounter = 3 Then
limit = 0.02
End If
ElseIf familyCounter = 2 Then
If propertyCounter = 1 Or propertyCounter = 2 Then
If cltv > 80# Then
limit = 0.03
ElseIf cltv <= 80# Then
limit = 0.06
End If
ElseIf propertyCounter = 3 Then
limit = 0
End If
End If
If priceCounter = 1 Then
contribLimit = salesPrice * limit
ElseIf priceCounter = 2 Then
contribLimit = appraisedValue * limit
End If
contribLimit_box.Text = contribLimit.ToString("C2")
If totalCredits > contribLimit And Not totalCredits > totalCosts Then
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Regular)
decisionUpper_box.Text = "Total credits exceed contribution limit"
ElseIf totalCredits > totalCosts And Not totalCredits > contribLimit Then
decisionUpper_box.Text = "TOTAL CREDITS EXCEED TOTAL COSTS"
decisionUpper_box.ForeColor = Color.Red
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
ElseIf totalCredits > totalCosts And totalCredits > contribLimit Then
decisionUpper_box.ForeColor = Color.Red
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
decisionUpper_box.Text = "TOTAL CREDITS EXCEED TOTAL COSTS"
decisionLower_box.Text = "Total credits exceed contribution limit"
decisionLower_box.Font = New Font(decisionUpper_box.Font, FontStyle.Regular)
ElseIf totalCredits < contribLimit Or totalCredits = contribLimit Then
decisionUpper_box.Text = "PASS"
decisionUpper_box.ForeColor = Color.Green
decisionUpper_box.Font = New Font(decisionUpper_box.Font, FontStyle.Bold)
End If
End If
End If
' Do not edit code below this line.
System.TimeoutValue = oldSystemTimeout
Sessions = Nothing
System = Nothing
Sess0 = Nothing
End Sub
End Class
Program.vb
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
' This file controls the behaviour of the application.
Partial Class MyApplication
Public Sub New()
MyBase.New(AuthenticationMode.Windows)
Me.IsSingleInstance = False
Me.EnableVisualStyles = True
Me.SaveMySettingsOnExit = False
Me.ShutDownStyle = ShutdownMode.AfterMainFormCloses
End Sub
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = My.Forms.MainForm
End Sub
End Class
End Namespace
То, что я пытаюсь сделать, это создать переменные на уровне класса, которые доступны для всех моих функций / подпрограмм. Когда я компилирую приведенный ниже код и запускаю его, я получаю "System.NullReferenceException: переменная объекта или переменная блока не установлена". ошибка. Отладчик выделяет "Me.MainForm = My.Forms.MainForm" в Program.vb в качестве источника ошибки, но я не уверен, как ее исправить. Кто-нибудь может дать мне несколько советов в правильном направлении? Спасибо!
1 ответ
Я не уверен, что это все, что не так, но:
Dim System As Object
Это вызовет все виды проблем. В.NET уже есть пространство имен системы, и оно довольно важно, как вы можете увидеть всего несколькими строками позже:
Public Sub costs_TextChanged(ByVal sender As System.Object, ...
Переименуйте это:
Dim XSys as Object
путь внизу:
' Get the main system object
XSys = CreateObject("EXTRA.System") ' Gets the system object
If (XSys Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
Sessions = XSys.Sessions
If (Sessions Is Nothing) Then
MsgBox ("Please open an NTS session")
Exit Sub
End If
' at the very end:
XSys.TimeoutValue = oldSystemTimeout
Sessions = Nothing
XSys = Nothing
Это все еще может не работать, но наверняка, это не может работать так, как было