Home All Groups Group Topic Archive Search About

Writing to a file opened in another class

Author
9 Aug 2006 5:27 AM
AA
Hi,
I am trying to write to a file. I have 3 classes defined. In 1 class
(Test), I have the File open, close utilities. The application is a
console application. I am opening the file in class Test1. I write
something in it. Then I call a sub in class Test2. Here when I try to
write in the file, I am getting error "Object reference not set to an
instance of an object." The error occurs in the WriteToFile sub of
class Test. For some reason Class Test2 does not know that the log file
is open and thit is why the error is coming, I think!
Any other reasons, solutions or workaround?

Regards,
p

<code>
Module Module1

    Sub Main()
        Dim start_test As New Test1
        start_test.run_test()
    End Sub

End Module
</code>

<code>
Imports System
Imports System.IO

Public Class Test1
    Inherits Test
    Dim log_file_name As String = "test1.log"
    Sub run_test()
        Try
            OpenLogFile(log_file_name, False)
            log("ii", "pp")
            Dim pt As New Test2
            pt.run_test1()
        Catch ex As Exception
        Finally
            CloseLogFile()
        End Try
    End Sub
End Class

Public Class Test2
    Inherits Test
    Sub run_test1()
        Try
            log("ii", "phhp")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
        End Try
    End Sub
End Class
</code>

<code>
Imports System
Imports System.IO
Imports System.String
Public Class Test
    Dim objStreamWriter As StreamWriter
    Public Sub OpenLogFile(ByVal filename As String, ByVal bool As
Boolean)
        objStreamWriter = New StreamWriter(filename, bool)
    End Sub
    Public Sub WriteToFile(ByVal str As String)
        Console.WriteLine("in WriteToFile")
        objStreamWriter.WriteLine(str)
    End Sub

    Public Sub CloseLogFile()
        objStreamWriter.Close()
    End Sub
    Public Sub OpenFile(ByVal filename As String, ByVal bool As
Boolean)
        objStreamWriter = New StreamWriter(filename, bool)
    End Sub

    Public Sub CloseFile()
        objStreamWriter.Close()
    End Sub
    Public Sub log(ByVal invoker As String, ByVal methodname As String)
        toLog(invoker, methodname)
    End Sub
    Sub toLog(ByVal MyCallerFile As String, ByVal methodname As String)
        Dim str As String
        str = MyCallerFile + "     " + methodname
        WriteToFile(str)
    End Sub
End Class
</code>

Author
9 Aug 2006 7:03 AM
GhostInAK
Hello AA,

You forgot to initialize a variable.. or you passed a null reference (Nothing)
to the sub.  This error has nothing to do with the state of the file (being
open or not).

-Boo

Show quoteHide quote
> Hi,
> I am trying to write to a file. I have 3 classes defined. In 1 class
> (Test), I have the File open, close utilities. The application is a
> console application. I am opening the file in class Test1. I write
> something in it. Then I call a sub in class Test2. Here when I try to
> write in the file, I am getting error "Object reference not set to an
> instance of an object." The error occurs in the WriteToFile sub of
> class Test. For some reason Class Test2 does not know that the log
> file
> is open and thit is why the error is coming, I think!
> Any other reasons, solutions or workaround?
> Regards,
> p
> <code>
> Module Module1
> Sub Main()
> Dim start_test As New Test1
> start_test.run_test()
> End Sub
> End Module
> </code>
> <code>
> Imports System
> Imports System.IO
> Public Class Test1
> Inherits Test
> Dim log_file_name As String = "test1.log"
> Sub run_test()
> Try
> OpenLogFile(log_file_name, False)
> log("ii", "pp")
> Dim pt As New Test2
> pt.run_test1()
> Catch ex As Exception
> Finally
> CloseLogFile()
> End Try
> End Sub
> End Class
> Public Class Test2
> Inherits Test
> Sub run_test1()
> Try
> log("ii", "phhp")
> Catch ex As Exception
> Console.WriteLine(ex.Message)
> Finally
> End Try
> End Sub
> End Class
> </code>
>
> <code>
> Imports System
> Imports System.IO
> Imports System.String
> Public Class Test
> Dim objStreamWriter As StreamWriter
> Public Sub OpenLogFile(ByVal filename As String, ByVal bool As
> Boolean)
> objStreamWriter = New StreamWriter(filename, bool)
> End Sub
> Public Sub WriteToFile(ByVal str As String)
> Console.WriteLine("in WriteToFile")
> objStreamWriter.WriteLine(str)
> End Sub
> Public Sub CloseLogFile()
> objStreamWriter.Close()
> End Sub
> Public Sub OpenFile(ByVal filename As String, ByVal bool As
> Boolean)
> objStreamWriter = New StreamWriter(filename, bool)
> End Sub
> Public Sub CloseFile()
> objStreamWriter.Close()
> End Sub
> Public Sub log(ByVal invoker As String, ByVal methodname As
> String)
> toLog(invoker, methodname)
> End Sub
> Sub toLog(ByVal MyCallerFile As String, ByVal methodname As
> String)
> Dim str As String
> str = MyCallerFile + "     " + methodname
> WriteToFile(str)
> End Sub
> End Class
> </code>
>
Author
9 Aug 2006 8:34 AM
AA
Hi,
I got it to work. The problem was I was inheriting classes improperly.
The inheritance should be:
<code>

Public Class Test1
    Inherits Test2
.......
End Class

Public Class Test2
    Inherits Test
.....
End Class
</code>


Regards,
AA