Solved

Null reference exception warning

Posted on 2007-03-30
6
208 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

21 Experts available now in Live!

Get 1:1 Help Now