对于Windows窗体应用程序没有太多经验(这是我第一次尝试构建),但是当我的应用程序不断在Form1_Load和7个Browsebutton_Click Subs中的2个上抛出空引用错误时,我无法理解。其他5个完美无缺。
其他信息:
AppConfig文件appsettings-part看起来像这样(验证所有路径并确实存在):
<appSettings>
<add key="LastZHRverzamelPDF" value="C:\x\pdf files\TestPDF.pdf" />
<add key="LastMapSplitted" value="C:\x\pdf files\ZHRFORMS" />
<add key="LastPersFile" value="C:\x\pdf files\Personeelslijst.xlsx" />
<add key="LastMap1" value="C:\x\pdf files\Salarisslips" />
<add key="LastMap2" value="C:\x\pdf files\Vakantie-uren" />
<add key="LastMap3" value="C:\x\pdf files\ZHRFORMS" />
<add key="LastMap4" value="C:\x\pdf files\Logitime" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
Form1_Load过程包含以下代码:
Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
Try
'Laatstgekozen waarden uit app.config halen en in lbls neerzetten
lblZHRFile.Text = ConfigurationManager.AppSettings("LastZHRverzamelPDF").ToString()
lblMapSplitted.Text = ConfigurationManager.AppSettings("LastMapSplitted").ToString() & "\"
lblPersFile.Text = ConfigurationManager.AppSettings("LastPersFile").ToString()
lblMap1.Text = ConfigurationManager.AppSettings("LastMap1").ToString() & "\"
lblMap2.Text = ConfigurationManager.AppSettings("LastMap2").ToString() & "\"
lblMap3.Text = ConfigurationManager.AppSettings("LastMap3").ToString() & "\"
lblMap4.Text = ConfigurationManager.AppSettings("LastMap4").ToString() & "\"
Catch ex As Exception
Dim exMsg As String = "Message: " & ex.Message & vbCrLf & "Source: " & ex.Source & vbCrLf & "StackTrace: " & ex.StackTrace
MsgBox(exMsg, MsgBoxStyle.Information, "Sub Form1_Load")
Dim errormsg As String = ex.Message
End Try
End Sub
我在这里将appconfig值加载到UserForm标签中以显示最后选择的值。在7个标签旁边,我有7个浏览按钮来修改路径。 不工作的其中两个包含以下代码:
Private Sub btnZHRPath_Click(sender As Object, e As EventArgs) Handles btnZHRPath.Click
Try
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.FileName = ConfigurationManager.AppSettings("LastZHRverzamelPDF").ToString()
Dim strFileName As String
With fd
.Title = "Selecteer verzamel-PDF file ZHRFORMS uit SAP"
.InitialDirectory = ""
.Filter = "All files (*.pdf)|*.pdf|All files (*.pdf)|*.pdf"
.FilterIndex = 2
.RestoreDirectory = True
End With
If fd.ShowDialog() = DialogResult.OK Then
strFileName = fd.FileName
lblZHRFile.Text = strFileName
Dim objConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
objConfig.AppSettings.Settings("LastZHRverzamelPDF").Value = strFileName
objConfig.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
Else
lblZHRFile.Text = "(overslaan)"
End If
Catch ex As Exception
Dim exMsg As String = "Message: " & ex.Message & vbCrLf & "Source: " & ex.Source & vbCrLf & "StackTrace: " & ex.StackTrace
MsgBox(exMsg, MsgBoxStyle.Information, "btnZHRPath_Click")
End Try
End Sub
同样的原因也不起作用:
Private Sub btnSplittedPath_Click(sender As Object, e As EventArgs) Handles btnSplittedPath.Click
Try
If sender = Nothing Then MsgBox("nullsender")
lblMapSplitted.Text = "C:\" 'ConfigurationManager.AppSettings("LastMapSplitted").ToString() & "\"
Dim Map As FolderBrowserDialog = New FolderBrowserDialog
Map.SelectedPath = ConfigurationManager.AppSettings("LastMapSplitted").ToString()
Map.Description = "Selecteer doelmap voor gesplitte PDF-files"
Map.ShowNewFolderButton = True
Dim DoelMap As String = ""
If Map.ShowDialog() = Windows.Forms.DialogResult.OK Then
DoelMap = Map.SelectedPath & "\"
Dim objConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
objConfig.AppSettings.Settings("LastMapSplitted").Value = Map.SelectedPath
objConfig.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
Else
DoelMap = "(overslaan)"
End If
lblMapSplitted.Text = DoelMap
Catch ex As Exception
Dim exMsg As String = "Message: " & ex.Message & vbCrLf & "Source: " & ex.Source & vbCrLf & "StackTrace: " & ex.StackTrace
MsgBox(exMsg, MsgBoxStyle.Information, "btnSplittedPath_Click")
End Try
End Sub
奇怪的是,这个浏览按钮 正常工作(就像剩下的4个代码相似):
Private Sub btnFilePath_Click(sender As Object, e As EventArgs) Handles btnFilePath.Click
Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
fd.FileName = ConfigurationManager.AppSettings("LastPersFile").ToString()
With fd
.Title = "Selecteer bestand met medewerkergegevens"
.InitialDirectory = ""
.Filter = "All files (*.xl*)|*.xl*|All files (*.xl*)|*.xl*"
.FilterIndex = 2
.RestoreDirectory = True
End With
If fd.ShowDialog() = DialogResult.OK Then
strFileName = fd.FileName
lblPersFile.Text = strFileName
Dim objConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
objConfig.AppSettings.Settings("LastPersFile").Value = strFileName
objConfig.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
End If
End Sub
尝试了一切来解决这个问题,但根本无法弄清楚为什么我的应用程序不断向我抛出这些错误!请帮帮我吧!