[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

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(intAccountNr )
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
0
vj_mi
Asked:
vj_mi
  • 2
  • 2
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
do you use the variable objCardDetails somewhere above that line? I guess so, because otherwise that warning should not be issued
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree.

Show us the 20 lines:

    "I have some 20 lines of code here and then the below code"

or more of your code...
0
 
vj_miAuthor Commented:
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.FieldName, "")
                mViewStyle.Options = StyleOptions.UseHorzAlignment
            End If
            c.StyleName = mViewStyle.StyleName
        End If
        c.Style.HAlignment = HorzAlignment.Near

Regards,
MI
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
do you have that line:
objCardDetails = New frmtCardDetails(intAccountNr )
inside an if / else statement?

0
 
vj_miAuthor Commented:
No... it is not inside the If statement.

What will happen if I initialize to Nothing, like:

Dim objCardDetails As frmtCardDetails = nothing

Regards,
MI
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It can't hurt.

...but still shouldn't make a difference given the code you've posted thus far.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now