Writing to a file opened in another class

pukya78

Member
Joined
Jun 5, 2006
Messages
15
Programming Experience
Beginner
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

VB.NET:
Module Module1

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

End Module

VB.NET:
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

VB.NET:
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
 
Last edited by a moderator:
Your instance of Test1 and your instance of Test2 are two distinct objects. That means that they both have an objStreamWriter field. When you create a StreamWriter object in your Test1 instance, the instance of Test2 doesn't know anything about it. Its objStreamWriter field still refers to no object, so when you try to use it you get a null reference exception.

Let me present an analogy. In real life we have the Vehicle class. The Vehicle class has a field named objDriver. The Car class and the Truck class both inherit the Vehicle class, so they both inherit the objDriver field. Now, lets say that I have a car and a truck. They are instances of the Car and Truck class respectively. Now, let's say that I get into the drivers seat of the car. I just did this:
VB.NET:
theCar.objDriver = Me
Now, if you went to the truck, would you be surprised to see that I was not in the driver's seat? I don't think you would. These are two distinct objects and their objDriver fields are also distinct, so setting one has no effect on the other. Now, to extend that a bit, those vehicles could also have an objOwner field. Let's say that they are both sitting in a car yard. If I go and buy the car I've just done this:
VB.NET:
theCar.objOwner = Me
If I was to buy the truck as well then I've just done this:
VB.NET:
theTruck.objOwner = Me
so now the objOwner fields of both objects refer to the same object: Me. Alternatively, the salesman could say that I can have the truck as well as a bonus for buying the car. That means he's just done this:
VB.NET:
theTruck.objOwner = theCar.objOwner
Getting back to your code, if you want the objStreamWriter field of your instance of Test2 to refer to the same StreamWriter object as the objStreamWriter field of your instance of Test1 refers to then it's up to you to make that happen. You need to pass a reference to the StreamWriter object to the Test2 instance. You do that by either setting a property or passing a parameter to a method, which includes the constructor. It's up to you to write that property or method yourself.

I suggest that you follow the Start VB.NET link in my signature and read the OO section.
 
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,
P
 
Still your instance of Test1 and your instance of Test2 are two distinct objects. Unless you use your OpenLogFile method the StreamWriter will give nullreference exception.

PS, the code tags use [ ] not < >
 
Back
Top