Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I have converted one of my projects from vb6 to VB.NET.

When I build the project I get an warning of:

a null reference exception could result at runtime

Does anyone know how to solve this?

Thank You
Posted
Updated 31-May-11 2:59am
v2
Comments
Sergey Alexandrovich Kryukov 31-May-11 16:21pm    
"Does anyone know" sounds rude! I think, all experts know how to solve it. Please don't measure other people by your own measure.
--SA

Probably can try to turn off the Option Strict.

Goto Properties on Project > Compile Tab > Option Strict [Drop down List Box] > set it to off

or

type this on the top of your code.

Option Strict Off


Good Luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-May-11 16:26pm    
Are you talking about C or C++? Why? I voted 1. If I missed something, please let me know.
--SA
Manfred Rudolf Bihy 31-May-11 17:40pm    
Hmmmm, what are you talking about SA? "Option Strict" is VB.NET and such is the question tagged which OP posted.
For your reference: http://support.microsoft.com/kb/311329/en-us
Sergey Alexandrovich Kryukov 31-May-11 19:59pm    
Opps! My bad. Thank you for correcting me, Manfred.
--SA
Manfred Rudolf Bihy 31-May-11 20:27pm    
We all make mistakes! :)
Sergey Alexandrovich Kryukov 31-May-11 21:43pm    
Oh, we do! Please see my last 2 or 3 comments to your comments as well.
Cheers,
--SA
The result of that specific piece of code could return a null value or a variable is not initialized, but it could also never happen or result in any trouble. The compiler just noticed that there is a condition where it could happen and it isn't surrounded with a try..catch to fix that or a variable isn't assigned a value, like String s = "". You could simply check it, try and see what happens.

Good luck!
 
Share this answer
 
Comments
S Houghtelin 31-May-11 10:49am    
+5 Good answer!
Sergey Alexandrovich Kryukov 31-May-11 16:23pm    
How can you write s = ""? Use string.Empty. Also, this is certainly not compiler, but exception. Try-catch normally should be use to fix anything. Sorry, my 3 this time.
--SA
I think you did not get a clear answer so far.

Simply run you code under debugger; it should point you to the line of code where it happens. The reason is only one: the code tries to access an instance of an object which is null, no matter by what reason. When you find it, check the offending object for null and act appropriately under the if block. What to do in the object is null totally depends on your semantic; only you can decide what to do.

Alternatively, catch all exceptions on top of each thread and dump exception with its stack using System.Exception.Stack. In Debug configuration, it will show you all the code line numbers where bad thing happens.

[EDIT]
If you have warning, it does not mean you can catch exception in all cases. Make sure there is no warnings. A warning shows you exact location of the line where an initialized variable is used. Never allow such situation. Initialize all the variables before use; no exceptions.

—SA
 
Share this answer
 
v2
Comments
[no name] 1-Jun-11 1:47am    
He is getting a warning that an unititialized variable may cause trouble, no more. Debugging may or may not work. He should just look at the specified location and see to it that the variqable in question is properly initialized.

Catching exceptions is of course a good idea, but please don't teach him to do this to sweep everything under the rug and pretend the exceptions never happened.
Sergey Alexandrovich Kryukov 1-Jun-11 19:44pm    
I see. I did not understand the question properly. The answer is updated. Thank you very much for your help.
--SA

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