vj_mi
asked on
Null reference exception warning
I have following code in VB.NEt 2005:
Dim objCardDetails As frmtCardDetails
Try
'I have some 20 lines of code here and then the below code
intAccountNr = GetAccountNr()
objCardDetails = New frmtCardDetails(intAccount Nr )
Catch
Finally
End Try
Now I get following warning:
Warning 9 Variable 'objCardDetails' is used before it has been assigned a value. A null reference exception could result at runtime.
How should I initialize this variable?
Regards
MI
Dim objCardDetails As frmtCardDetails
Try
'I have some 20 lines of code here and then the below code
intAccountNr = GetAccountNr()
objCardDetails = New frmtCardDetails(intAccount
Catch
Finally
End Try
Now I get following warning:
Warning 9 Variable 'objCardDetails' is used before it has been assigned a value. A null reference exception could result at runtime.
How should I initialize this variable?
Regards
MI
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No I have not used that variable. I have similar issue at most of the places. I declare a variable of type form and I use it somewhere down belowI get this error.
The earlier code is somthing like this:
If c.Style Is Nothing Then
If mViewStyle Is Nothing Then
mViewStyle = grdTemp.Styles.Add(c.Field Name, "")
mViewStyle.Options = StyleOptions.UseHorzAlignm ent
End If
c.StyleName = mViewStyle.StyleName
End If
c.Style.HAlignment = HorzAlignment.Near
Regards,
MI
The earlier code is somthing like this:
If c.Style Is Nothing Then
If mViewStyle Is Nothing Then
mViewStyle = grdTemp.Styles.Add(c.Field
mViewStyle.Options = StyleOptions.UseHorzAlignm
End If
c.StyleName = mViewStyle.StyleName
End If
c.Style.HAlignment = HorzAlignment.Near
Regards,
MI
do you have that line:
objCardDetails = New frmtCardDetails(intAccount Nr )
inside an if / else statement?
objCardDetails = New frmtCardDetails(intAccount
inside an if / else statement?
ASKER
No... it is not inside the If statement.
What will happen if I initialize to Nothing, like:
Dim objCardDetails As frmtCardDetails = nothing
Regards,
MI
What will happen if I initialize to Nothing, like:
Dim objCardDetails As frmtCardDetails = nothing
Regards,
MI
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Show us the 20 lines:
"I have some 20 lines of code here and then the below code"
or more of your code...