VB 代码中奇怪的空指针异常

weird null pointer exception from VB code

提问人:George2 提问时间:5/4/2009 最后编辑:Siddharth RoutGeorge2 更新时间:6/11/2012 访问量:1940

问:

我正在使用 Windows Vista x64 + VSTS 2008。我正在从以下 URL(本文的相关示例代码)调试示例程序,

http://www.codeproject.com/KB/audio-video/CaptureScreenAsVideo.aspx?display=PrintAll&fid=129831&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=101&select=1160633

但是在按下写入文件按钮时,我遇到了空指针异常,这是我的屏幕快照。

http://tinypic.com/view.php?pic=14cbfop&s=5

任何想法,怎么了?

EDIT1:这是完整的源代码。

Imports WMEncoderLib
Imports WMPREVIEWLib

Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(48, 80)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(216, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Write To file and Close application"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim Encoder As WMEncoder
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a WMEncoder object.

        Encoder = New WMEncoder
        ' Retrieve the source group collection and add a source group. 

        Dim SrcGrp As IWMEncSourceGroup2
        Dim SrcGrpColl As IWMEncSourceGroupCollection
        SrcGrpColl = Encoder.SourceGroupCollection
        SrcGrp = SrcGrpColl.Add("SG_1")

        ' Add a video and audio source to the source group.
        Dim SrcVid As IWMEncVideoSource2
        Dim SrcAud As IWMEncAudioSource
        SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO)
        SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO)

        ' Identify the source files to encode.
        SrcVid.SetInput("ScreenCap://ScreenCapture1")
        SrcAud.SetInput("Device://Default_Audio_Device")

        ' Choose a profile from the collection.
        Dim ProColl As IWMEncProfileCollection
        Dim Pro As IWMEncProfile
        Dim i As Integer
        Dim lLength As Long

        ProColl = Encoder.ProfileCollection
        lLength = ProColl.Count
        'For i = 0 To lLength - 1
        '    Console.WriteLine(ProColl.Item(i).Name)
        'Next
        For i = 0 To lLength - 1
            Pro = ProColl.Item(i)
            If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then
                SrcGrp.Profile = Pro
                Exit For
            End If
        Next

        ' Fill in the description object members.
        Dim Descr As IWMEncDisplayInfo
        Descr = Encoder.DisplayInfo
        Descr.Author = "Armoghan Asif"
        Descr.Copyright = "Copyright information"
        Descr.Description = "Text description of encoded content"
        Descr.Rating = "Rating information"
        Descr.Title = "Title of encoded content"

        ' Add an attribute to the collection.
        Dim Attr As IWMEncAttributes
        Attr = Encoder.Attributes
        Attr.Add("URL", "www.adnare.com")

        ' Specify a file object in which to save encoded content.
        Dim File As IWMEncFile
        File = Encoder.File
        File.LocalFileName = "C:\OutputFile.avi"

        ' Crop 2 pixels from each edge of the video image.
        SrcVid.CroppingBottomMargin = 2
        SrcVid.CroppingTopMargin = 2
        SrcVid.CroppingLeftMargin = 2
        SrcVid.CroppingRightMargin = 2

        ' Start the encoding process.
        Encoder.Start()

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Encoder.RunState Then
            Encoder.Stop()
            Application.Exit()
        End If
    End Sub
End Class
vb.net nullPointerException

评论


答:

1赞 Stefan Steinegger 5/4/2009 #1

您可能没有编码器。检查先决条件(请参阅此行:“你将需要 Windows Media Encoder 9 系列和 SDK。

评论

0赞 George2 5/4/2009
我已经安装了编码器,但仍然遇到这个问题。您可以尝试安装它并重现我的问题吗?
2赞 Ayman Hourieh 5/4/2009 #2

是否按照本文的说明安装了 Windows Media Encoder 9 系列SDK?我的猜测是,这是罪魁祸首。

评论

0赞 George2 5/4/2009
我已经安装了它们,但仍然遇到了这个问题。顺便说一句:您可以尝试安装它并重现我的问题。
0赞 Ayman Hourieh 5/4/2009
对不起;我不使用 Windows 也不了解 VB,所以我无法测试。
0赞 George2 5/4/2009
代码可以很容易地转换为C#,只需几十行代码。但同样的问题。:-(
1赞 RichieHindle 5/4/2009 #3

这段代码还完好无损吗?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Create a WMEncoder object.
    Encoder = New WMEncoder

这意味着该代码丢失或悄悄失败。

如果在后面的行上放一个断点,值是多少?Encoder = New WMEncoderEncoder

评论

0赞 George2 5/4/2009
@RichieHindle,我对你的意思感到困惑。你能在脑海中写下正确的代码吗?代码使一切变得清晰。:-)
0赞 RichieHindle 5/4/2009
该代码直接来自您指向的 CodeProject 下载中的示例代码。我想知道您是否更改了示例代码,以便不再初始化 Encoder 变量。
0赞 George2 5/4/2009
@RichieHindle,我使用的是原始代码,没有更改任何内容。您可以尝试运行此程序(代码项目文章中的原始代码)并重现我的问题。
0赞 George2 5/4/2009
我刚刚发布了我正在使用的所有代码。如果您发现任何处理初始化的问题,请告诉我。:-)