首先有一點背景信息:此應用程序的目的是捕獲圖像並將它們自動保存到網絡目錄中,該網絡目錄將被創建或使用文本框。此代碼可以在我的電腦上工作(Windows 7 Home 64位)。我一直在使用它的Microsoft Visual Basic快遞2010年對象引用未設置爲對象的實例 - vb.net
但是.....試圖在Windows運行應用程序10平板電腦時創建的,我得到了如下錯誤:
在形式負載:
An error occurred while capturing the image. The video capture will now be terminated.
Object reference not set to an instance of an object.
在button2_Click事件:
Object reference not set to an instance of an object.
下面是代碼的全部。
Form2.vb
Public Class Form2
Public scanIsSet As Boolean
Private webcam As WebCam
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
webcam = New WebCam()
webcam.InitializeWebCam(imgVideo)
webcam.Start()
scanIsSet = False
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim CFGfile As String
Dim SaveDir As String
Dim imgIndex As Integer
Dim existingImages() As String
SaveDir = "C:\somepath\"
'save image to directory with index number
Try
imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg")
Catch ex As Exception
MsgBox("error while accessing object imgCapture" & ex.Message)
End Try
imgIndex = imgIndex + 1
Else
Beep()
MsgBox("Please scan or type in order number first")
End If
End Sub
End Class
WebCam.vb
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports WebCam_Capture
Imports System.Collections.Generic
Imports ZXing
Imports ZXing.OneD
'Design by Pongsakorn Poosankam
Class WebCam
Public scanz As Boolean
Public Sub setScan(ByVal x As Boolean)
scanz = x
End Sub
Private webcam As WebCamCapture
Private _FrameImage As System.Windows.Forms.PictureBox
Private FrameNumber As Integer = 30
Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Forms.PictureBox)
webcam = New WebCamCapture()
webcam.FrameNumber = CULng((0))
webcam.TimeToCapture_milliseconds = FrameNumber
AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured
_FrameImage = ImageControl
End Sub
Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal e As WebcamEventArgs)
_FrameImage.Image = e.WebCamImage
If scanz = True Then
Dim BCreader As New ZXing.BarcodeReader
'BCreader.Options.TryHarder = True
Try
Dim resu As Result = BCreader.Decode(e.WebCamImage)
Form2.OrderNumber.Text = resu.Text
setScan(False)
Form2.Label2.Text = ""
Beep()
Catch ex As Exception
'do nothing
End Try
End If
End Sub
Public Sub Start()
webcam.TimeToCapture_milliseconds = FrameNumber
webcam.Start(0)
End Sub
Public Sub [Stop]()
webcam.[Stop]()
End Sub
Public Sub [Continue]()
' change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber
' resume the video capture from the stop
webcam.Start(Me.webcam.FrameNumber)
End Sub
Public Sub ResolutionSetting()
webcam.Config()
End Sub
Public Sub AdvanceSetting()
webcam.Config2()
End Sub
End Class
正如你所朝的末見Form2.vb,我一直在嘗試包裹imgCapture.Image.Save(SaveDir & OrderNumber.Text & "\" & CStr(imgIndex) & ".jpg")
因爲我懷疑這是pictureBox對象的某種問題, try catch塊確實捕捉到異常,但我仍然不知道問題是什麼,爲什麼它發生在平板電腦上而不是PC上,或者如何解決它。
我發現了類似的問題,但都沒有提供我可以使用的解決方案。
的可能的複製(http://stackoverflow.com/questions/4660142 [什麼是一個NullReferenceException,如何解決呢?]/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –
@Andy Komeyev - 我讀了一篇,但我無法理解答案或它如何適用於我的代碼。我很新的BTW。 – user3479671
對不起,但這是太多的代碼。閱讀[this](http://stackoverflow.com/help/mcve),縮小問題範圍並調整您的問題,例如Minimal,Complete和Verifiable。另外,鏈接@AndyKorneyev提供,你至少應該知道你必須指定**精確的** LOC來產生空引用。 –