?
Solved

A first chance exception of type 'System.NullReferenceException' occurred in sss.exe

Posted on 2006-03-20
30
Medium Priority
?
822 Views
Last Modified: 2008-02-26
For the code here, I'm getting  think a null value returned from the recorset or my code for the select is wrong...I'm not sure how to resolve:

  Public Sub MoveFiles(ByVal sFolder As String)
        Dim files() As String = System.IO.Directory.GetFiles(sFolder, "*.TXT")
        For Each sFile As String In files

            Dim sFileName As String = System.IO.Path.GetFileName(sFile)

            Dim sPart As String = sFileName.Substring(0, 4)

            Dim sNewFolder As String = GetFolder(sPart)


            System.IO.File.Move(sFile, System.IO.Path.Combine(sNewFolder, sFileName))

        Next

    End Sub

    Private Function GetFolder(ByVal PartnerID As String) As String
        Dim result As String = ""
        Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MYConn"))
        Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID")

        objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))

        Try
            objComm.Connection.Open()
            result = objComm.ExecuteScalar()

        Catch ex As Exception
            ex.Message.ToString()
        Finally
            If objConn.State = ConnectionState.Open Then objConn.Close()

        End Try

        Return result
    End Function


ERROR: A first chance exception of type 'System.NullReferenceException' occurred in sss.exe
0
Comment
Question by:dba123
  • 20
  • 10
30 Comments
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16239851
Wheres the error pointing to anyway??
0
 
LVL 1

Author Comment

by:dba123
ID: 16239913
Ok, I found out it's pointing here:

System.IO.File.Move(sFile, System.IO.Path.Combine(sNewFolder, sFileName))

True error really is: Object Reference not set to an instance of an object
0
 
LVL 1

Author Comment

by:dba123
ID: 16239937
I'm assuming it's a problem with my GetFolder but not sure what
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240001
Make these two changes and see if that makes any difference,

Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID",objConn)


and while opening:objConn.Open() instead of objComm.Connection.Open()
Might not be a mistake but lets do it the traditional way..Bcoz honestly i never used that
0
 
LVL 1

Author Comment

by:dba123
ID: 16240023
ok, sNewFolder is returning NULL
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240065
sNewFolder ???you might wanna post some more code here
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240068
ok got it ...sorry
0
 
LVL 1

Author Comment

by:dba123
ID: 16240087
Here's the whole thing:

Imports System.IO
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient

Module Module1

    Sub Main()

        Dim f As New Security.Permissions.FileIOPermission(Security.Permissions.PermissionState.None)
        f.AllLocalFiles = Security.Permissions.FileIOPermissionAccess.Read

        Dim file As New IO.FileInfo("\\sss\f$\inetpub\wwwroot\sss\ssis\maintenance\sss\phase2\msss_input\sss_input.mnt")

        Dim filefs As New IO.FileStream(file.FullName, IO.FileMode.Open)
        Dim reader As New IO.StreamReader(filefs)
        Dim counter As Integer = 0

        Dim CurrentFS As IO.FileStream
        Dim CurrentWriter As IO.StreamWriter

        While Not reader.Peek < 0
            Dim Line As String = reader.ReadLine
            If IsNumeric(Line.Substring(0, 1)) Then
                Dim Parts() As String = Line.Split(" "c)
                If Parts(0).Length = 8 Then
                    counter += 1
                    If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
                    CurrentFS = New IO.FileStream(IO.Path.Combine(IO.Path.GetDirectoryName("\\sss\f$\inetpub\wwwroot\sss\ssis\maintenance\sss\phase2\sss_output\"), Line.Substring(59, 4) & "[" & counter.ToString & "]" & Now.ToString("MM-dd-yyyy") & IO.Path.GetExtension(file.FullName)), IO.FileMode.Create)
                    CurrentWriter = New IO.StreamWriter(CurrentFS)
                End If

                If Not CurrentWriter Is Nothing Then
                    CurrentWriter.WriteLine(Line)
                End If

            End If
        End While

        If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
        'Console.Read()

        MoveFiles("\\sss\f$\inetpub\wwwroot\sss\ssis\maintenance\sss\phase2\sss_output\")

    End Sub


    Public Sub MoveFiles(ByVal sFolder As String)
        Dim files() As String = System.IO.Directory.GetFiles(sFolder, "*.mnt")
        For Each sFile As String In files

            Dim sFileName As String = System.IO.Path.GetFileName(sFile)

            Dim sPart As String = sFileName.Substring(0, 4)

            Dim sNewFolder As String = GetFolder(sPart)

            MsgBox("sFileName:" & sFileName & "sNewFolder:" & sNewFolder)

            System.IO.File.Move(sFile, System.IO.Path.Combine(sNewFolder, sFileName))

        Next

    End Sub

    Private Function GetFolder(ByVal PartnerID As String) As String
        'MsgBox(PartnerID)
        Dim result As String = ""
        Dim objConn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MYConn"))
        Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)

        objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))

        Try
            objComm.Connection.Open()
            result = objComm.ExecuteScalar()

        Catch ex As Exception
            MsgBox("Error:" & vbCrLf & ex.Message)
        Finally
            If objConn.State = ConnectionState.Open Then objConn.Close()

        End Try

        Return result
    End Function


End Module

0
 
LVL 1

Author Comment

by:dba123
ID: 16240101
so there's somethign not right in my GetFolder function but not sure what it is...preferably I'd like to reference a stored proc anyway rather than an inline select
0
 
LVL 1

Author Comment

by:dba123
ID: 16240110
The Lookup table definitely has valid lookup values...I'm sure something is malformed but can't pinpoint it in my code
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240117
I am sorry...did u check what objcomm.executescalar is returning...!!!!!
0
 
LVL 1

Author Comment

by:dba123
ID: 16240169
Ok, it's not getting there (to the result) because now the error is saying the ConnectionString property has not been initialized.
0
 
LVL 1

Author Comment

by:dba123
ID: 16240180
It's talking about ObjConn not being initialized
0
 
LVL 1

Author Comment

by:dba123
ID: 16240221
If I put in objConn.Open(), I still get the message saying that objConn hasn't been initialized.
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240362
change this

system.Configuration.ConfigurationSettings.AppSettings("MYConn") and see..
0
 
LVL 1

Author Comment

by:dba123
ID: 16240382
Warning      2      'Public Shared ReadOnly Property AppSettings() As System.Collections.Specialized.NameValueCollection' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

srivatsavaye , I'm coding in .NET 2.0, you can't use that anymore...
0
 
LVL 1

Author Comment

by:dba123
ID: 16240388
remember, this is a console application, not asp.net so you must use System.Configuration.ConfigurationManager.AppSettings("MYConn")
0
 
LVL 1

Author Comment

by:dba123
ID: 16240393
my connection string is in a app.config file, not web.config
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240424
The way you wrote is the way we write in web applications except for the appsettings...
Did you try that..anyway..bcoz I tried to type in configuration.configurationmanager..it did not have that configurationmanager..so just try it..
0
 
LVL 1

Author Comment

by:dba123
ID: 16240453
I did try it, it errors out, I just pasted the error did you see it:

Warning     2     'Public Shared ReadOnly Property AppSettings() As System.Collections.Specialized.NameValueCollection' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'
0
 
LVL 1

Author Comment

by:dba123
ID: 16240460
this guy had a similar problem...but of course no resolution here:

http://www.dotnetjunkies.com/Forums/ShowPost.aspx?PostID=2769
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16240488
Sorry...Lets debug carefully..

Do this..

dim conString as string=System.Configuration.ConfigurationManager.AppSettings("MyConn")

then
 Dim objConn As SqlConnection = New SqlConnection(constring)

just put a breakpoint and see if you are retrieving the connectionstring

Hope you did this right:appSettings is case sensitive in your app.config file..
0
 
LVL 1

Author Comment

by:dba123
ID: 16240660
Here's the app.config (I covered up our servername, etc. of course):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>

  <connectionStrings>
    <add name="HEDIConn"
         connectionString="Data Source=myservername;Initial Catalog=mydbname; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
 
</configuration>
0
 
LVL 1

Author Comment

by:dba123
ID: 16240680
If I set breakpoints, how can I see the value being returned without using like a msgbox?  I look at the compiler but it tells me nothing interesting
0
 
LVL 1

Author Comment

by:dba123
ID: 16240716
If I run your code, it's still saying the ConnectionString is not initialized

    Private Function GetFolder(ByVal PartnerID As String) As String
        'MsgBox(PartnerID)
        Dim result As String = ""
        Dim conString As String = System.Configuration.ConfigurationManager.AppSettings("MyConn")
        Dim objConn As SqlConnection = New SqlConnection(conString)
        Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)

        objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))

        Try
            objComm.Connection.Open()
            result = objComm.ExecuteScalar()
            MsgBox("Result:" & result)
        Catch ex As Exception
            MsgBox("Error:" & vbCrLf & ex.Message)
        Finally
            If objConn.State = ConnectionState.Open Then objConn.Close()
            objConn.Close()
        End Try

        Return result
    End Function
0
 
LVL 1

Author Comment

by:dba123
ID: 16240884
defining the connection string right into the module seems to work, however, I stick to best practices, which allows me to manage the connection string in one file (app.config) so I'd rather not have to do it this way:

        Dim conString As String = "Data Source=server;Initial Catalog=dbname; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
        Dim objConn As SqlConnection = New SqlConnection(conString)
0
 
LVL 1

Author Comment

by:dba123
ID: 16240892
I want to figure this out, why can't I do it the way we've been trying?  what's the misconnection with the app.config file?
0
 
LVL 9

Accepted Solution

by:
Raju Srivatsavaye earned 2000 total points
ID: 16240913
Look you havent initialized your connectiostring in the appsettings.You did in the ConnectionStrings property..Try doing this

<appSettings>

    <add name="MyConn" value="Your ConnectionString here"/>
  </appSettings>

Then try..You will have to get it this time.

Yes please put a message box there and seee if you are retrieving the connection string after making this change
0
 
LVL 1

Author Comment

by:dba123
ID: 16241005
Thanks, I'll assume that's it, if not I'll post a new thread but you're right, that is probably the problem, it's missing!
0
 
LVL 9

Expert Comment

by:Raju Srivatsavaye
ID: 16241014
If you see carefully the name of the connectionstring is HEDIConn not MyConn..

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

609 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