?
Solved

Null reference exception warning

Posted on 2007-03-30
6
Medium Priority
?
221 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 1000 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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 
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 1000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

800 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