Solved

Null reference exception warning

Posted on 2007-03-30
6
211 Views
Last Modified: 2013-11-26
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
Comment
Question by:vj_mi
  • 2
  • 2
  • 2
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 18824664
do you use the variable objCardDetails somewhere above that line? I guess so, because otherwise that warning should not be issued
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18824735
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
 

Author Comment

by:vj_mi
ID: 18824787
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 142

Expert Comment

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

0
 

Author Comment

by:vj_mi
ID: 18828924
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 18829153
It can't hurt.

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now