Solved

Null reference exception warning

Posted on 2007-03-30
6
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 143

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 86

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 143

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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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