2011-07-11 14 views
0

私はこのコードを実行するときに苦労していますが、このエラーが発生します。右この上記VB.net NullReferenceExeptionが処理されていません

IF (CheckedItem Is Nothing) Then 
    Next 'Or the VB version of the C# continue keyword (my VB is rusty) 

:あなたはこれを挿入する必要があるよう

Dim CheckedItems(clbfiles.CheckedItems.Count) As Object 
    clbfiles.CheckedItems.CopyTo(CheckedItems, 0) 
    For Each CheckedItem As Object In CheckedItems 
     Dim FileToDelete As String 

     If Not CheckedItem.ToString = "" Then <---- error 
      'creates path to delete 
      FileToDelete = appdata + "\" + CheckedItem.ToString 

      If System.IO.File.Exists(FileToDelete) = True Then 
       'deletes 
       System.IO.File.Delete(FileToDelete) 
       MsgBox("File Deleted") 

      End If 
      'removes from list 
      clbfiles.Items.Remove(CheckedItem) 
     End If 
    Next 
End Sub 

エラーコード

System.NullReferenceException was unhandled 
    Message=Object reference not set to an instance of an object. 
    Source=ScreenShotCraft 
    StackTrace: 
     at ScreenShotCraft.frmindex.btndeleteselected_Click(Object sender, EventArgs e) 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
     at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
     at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
     at ScreenShotCraft.My.MyApplication.Main(String[] Args) 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

答えて

4

をあなたは、配列それ爆弾の最後の項目を取得するときには、そう、1であなたの配列が長すぎました。代わりにDim CheckedItems(clbfiles.CheckedItems.Count - 1) As Objectを実行してください。

2

奇妙は、そのエラーからいくつかの理由で、思わ

If Not CheckedItem.ToString = "" Then 

HTH。あなたは

Dim CheckedItems(clbfiles.CheckedItems.Count) As new Object 

ようCheckedItemsのインスタンスを作成する必要が

+0

これも重要な安全チェックです。私の答えは今日のバグがあった理由ですが、これは将来のバグを防ぐための答えです。 – mattmc3

0

はそれがお役に立てば幸いです。

2

は、アイテムの存在をチェックをしてみてください:

If Not CheckedItem is Nothing 

End If 

代わり

CheckedItem.ToString() = "" 

BecuaseのCheckedItemが何もないならば、ToStringメソッドは失敗します。それとも、使用できます。

If Not String.IsNullOrEmpty(CheckedItem.ToString()) 

End IF 
2

mattmc3とBigOrangeSUはどちらも正しいです。

直観に反し、それによっては、VB.NETの配列寸法は、ゼロベース、次のステートメントを実行した場合、あなたは2つのスロットで新しい配列を初期化します意味:この中

Dim myArray(1) as Integer 

ので、ケースは、mattmc3で説明しているように、CheckedItemsの次元を1つ減らすと、問題が解決される可能性があります。 (ここでNewキーワードを使用する必要はありません:Anurajによって与えられた答えはコンパイルされません)

しかし、あなたが見ている文字列が何らかの理由で初期化されていない場合はどうなりますか? BigOrangeSUが言っているように、文字列を調べる前にその文字列が値を持っているかどうかを確認したい場合は、例外がスローされる可能性があります。

私は、次の構文を好む:

If CheckedItem IsNot Nothing Then 

End If 
関連する問題