Solved

Get Visual Studio .NET 2010 to stop reporting a Error or show me how to fix the error

Posted on 2011-09-22
3
237 Views
Last Modified: 2013-11-27
I'm learning just started learning Visual Studio .NET 2010 a big step from VB6 how do I get VS 2010 to stop reporting a Error or show me how to fix the error so the IDE will stop bugging me about it.

Here is the code:

Function GetMSAppShortPath(ByVal exeCode As Integer) As String

        Dim exeName As String = ""
        If exeCode = 1 Then exeName = "Msaccess.exe"
        If exeCode = 2 Then exeName = "Winword.exe"
        If exeCode = 3 Then exeName = "Excel.exe"
        If exeCode = 4 Then exeName = "Outlook.exe"
        If exeCode = 5 Then exeName = "IExplore.exe"
        If exeCode = 6 Then exeName = "PowerPnt.exe"

        Dim regKey As Microsoft.Win32.RegistryKey
        Dim strPath As String
        Try

            regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
                "Software\Microsoft\Windows\CurrentVersion\App Paths\" & exeName)
            strPath = regKey.GetValue("")

        Catch e As Exception
            strPath = ""
        Finally

            If Not regKey Is Nothing Then regKey.Close()
        End Try
        Return strPath

    End Function


The error popping up:
Is the first regkey value in the below code line. Sure the answer is simple but still have to learn how to fix it. Thank You
If Not regKeyIs Nothing Then regKey.Close()

Error Window Box Display for this error:      
Variable 'regKey' is used before it has been assigned a value. A null reference exception could result at runtime.
0
Comment
Question by:Cool-Cell
3 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 36584063
Just change your definition of the regKey variable in the following line so it is set to nothing should get rid of the error :

Dim regKey As Microsoft.Win32.RegistryKey = Nothing

0
 
LVL 3

Expert Comment

by:libby9284
ID: 36584066
I'm pretty sure the only way to get rid of the error message is to resolve it.  Why do you have it in a Try/Catch block?  Try using an if/else block instead, testing regKey for value Nothing after its assigned.
0
 
LVL 1

Author Closing Comment

by:Cool-Cell
ID: 36584122
Thank you! The solution was simple like I thought and helped in my learning process.

Answer the Question to other response:
Why in Try - Catch statement because just learning from source code I can find on the internet and reading EE answered question and asking questions on EE if I get stumped.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

706 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

12 Experts available now in Live!

Get 1:1 Help Now