2014-01-17 60 views
1

我的任务是将Excel VBA脚本转换为VB.net独立Windows应用程序。我以前从未使用VB.net,并发现自己正在努力解决这个特定的崩溃问题。我提前道歉,如果我的代码是一场灾难,我正在学习VB.net如何工作。System.NullReferenceException:对象变量或未设置块变量。 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:Object variable或With block variable not set。”错误。调试程序突出显示Program.vb中的“Me.MainForm = My.Forms.MainForm”作为错误的来源,但我失去了如何修复它。任何人都可以在正确的方向给我一些提示吗?谢谢!

+0

http://stackoverflow.com/questions/4660142/what-isa-a- nullreferenceexception-how-do-i-fix-it – Plutonix

+0

我很早就仔细地阅读了这篇文章,发现很难像我尝试过那么多。我知道这是一个常见的问题,我保证我已经尝试过使用该帖子中的建议来解决问题,但我只需要一些专门为我的代码量身定制的帮助。 –

+0

FormLoad中是否有代码?某处某处正在引用尚未创建的对象。 *可能*您所假设的事件只能在用户在窗体加载时执行某些操作时调用。并且不使用'Val'会导致你比解决问题更麻烦 - 使用Convert或TryParse – Plutonix

回答

1

我不知道这是否是所有这些都是错误的,但是:

Dim System As Object 

这将导致各种各样的问题。 .NET已经拥有了System命名空间,这是一个比较重要的一个,因为你可以看到,只需几行后:

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 

它仍然可能无法正常工作,但某些情况下,它可以按照这种方式工作

+0

基本上VB无法创建** System ** .Windows.Forms.Form,因为您接管System作为Nothing对象 – Plutonix

+0

再次感谢您的帮助! –

相关问题