Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a argument null exception error in one of my subs:

VB
If MD5Text.Text.Contains(labelX12) Then
                       CheckedListBoxControl1.Items.Add(fs.FullName)
                       lblvirus.Text = CheckedListBoxControl1.Items.Count
                   End If



MD5Text.Text = MD5Reader.ReadToEnd and labelx12 is the string that I am comparing to. I have no idea why it is running into a null value when it is always fed information. I have tried to resolve this issue for some time now on my own and have reasearched other sites that tell you to check or uncheck the security settings box for one click security but that does not solve my issue.

Here is the call stack of my particular error:

at System.String.IndexOf(String value, Int32 startIndex, Int32 count, StringComparison comparisonType)
   at System.String.Contains(String value)
   at Final_test.Form1.ScanScanner(String dir) in C:\Users\Admin\Documents\Visual Studio 2010\Projects\Final test\Final test\Form1.vb:line 155
   at Final_test.Form1.starting_scan() in C:\Users\Admin\Documents\Visual Studio 2010\Projects\Final test\Final test\Form1.vb:line 120
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   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()


Any help would be great thank you!!
Posted
Updated 5-Oct-11 6:09am
v3

Certainly both MD5Text and MD5Text.Text are not null, otherwise you could not observe the call to MD5Text.Text.Contains and then IndexOf. The conclusion is: labelX12 is null. The error message confirms it, as the usual error message on null would suggest "uninitialized variable".

The questions are: why it is null and why it is called "label", as a string is expected? I'm not sure you post right fragment of code. The variable of the type Label would not let this code compile.

Next time always use debugger as Mehdi suggests.

—SA
 
Share this answer
 
Comments
Mehdi Gholam 5-Oct-11 12:40pm    
It pains me that people are not aware of (or unable to use) the excellent tools they have, sad really.
Sergey Alexandrovich Kryukov 5-Oct-11 12:52pm    
They will lean them, main thing is their attitude.
Thank you, Mehdi.
--SA
Dale 2012 5-Oct-11 17:07pm    
I am still unsure as to why its null and the name labelX12 was dimed as a string but overlooked renaming the variable

I have changed the labelx12 to dim computerhash as string but still i get a null exception? how can i check for a uninitialized variable?
Sergey Alexandrovich Kryukov 5-Oct-11 17:17pm    
How come you can be unsure? Unlike me, you have your source code before your eyes. Simply check for null. If the value of reference type is not initialized, it's null. I'm telling you, this is string, looking at exception text and the call.
--SA
Run with a debugger and see which part of your if statement is giving you problem.

You might like to change labelX12 to labelX12.Text if it is a label control.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Oct-11 12:29pm    
I'm not sure as is labelX12 is not string, the code would not compile. However, sometimes inquirers post code which does not compile, not the code they are asking about.
--SA
Sergey Alexandrovich Kryukov 5-Oct-11 12:38pm    
No need to debug as in this case everything is inferred from the exception stack provided -- please see my solution.
Your general advice is good though. I voted 4.
--SA
Dale 2012 5-Oct-11 17:11pm    
Thank you guys very much I appricate this allot I have changed the code as mehdi has said and even thoe I think I have tried this before it works now!!!!! SO HAPPY thanks again

Cheers!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900