I am struggling with the code when I run it I get this error.
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 Ch开发者_C百科eckedItem.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
ERROR CODE
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:
You made your array too long by 1, so when you get to the last item in the array it bombs. Do this instead: Dim CheckedItems(clbfiles.CheckedItems.Count - 1) As Object
.
Weird, for some reason from that error, seems like you need to insert this:
IF (CheckedItem Is Nothing) Then
Next 'Or the VB version of the C# continue keyword (my VB is rusty)
Right above this:
If Not CheckedItem.ToString = "" Then
HTH.
try checking for the item existance:
If Not CheckedItem is Nothing
End If
instead of
CheckedItem.ToString() = ""
Becuase if CheckedItem is nothing then the ToString method will fail. Or you could use:
If Not String.IsNullOrEmpty(CheckedItem.ToString())
End IF
Both mattmc3 and BigOrangeSU are correct.
Counterintuitive as it may be, array dimensions in VB.NET are zero-based, meaning if you run the following statement, you'll initialize a new array with 2 slots:
Dim myArray(1) as Integer
So in this case, as mattmc3 describes, reducing the dimension of CheckedItems
by one will likely solve your problem. (There is no need to use the New
keyword here: the answer given by Anuraj won't even compile!)
But what if the string you're looking at isn't initialized for some reason? As BigOrangeSU has said, you want to verify the string has a value before you examine it, otherwise you could end up throwing an exception.
I prefer the following syntax:
If CheckedItem IsNot Nothing Then
End If
You need to create the instance of CheckedItems like
Dim CheckedItems(clbfiles.CheckedItems.Count) As new Object
Hope it helps.
精彩评论