473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Writing to a file opened in another class

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>

Aug 9 '06 #1
2 1016
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
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>

Aug 9 '06 #2
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

Aug 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
6
by: dast | last post by:
Hello, I’m having trouble writing the contents of a dataset to a csv file. I have made a solution that loops through the rows in the dataset, but it’s too slow. There must be a faster and...
1
by: RahimAsif | last post by:
I am designing a data acquisition program that peridically collects data from a panel and writes to a file. The way I am doing it right now, every time I have data from the panel, I open the file,...
5
by: grinder | last post by:
first off, i am an extreme newbie to C. i am an undergrad research assistant and i have been shifted to a project that involves building a fairly involved c program. The part that i am stuck on now...
1
by: Smita Prathyusha | last post by:
I am facing a problem in writing to COM1. I am using a Win 32 Console mode Program in VC++ the following is the code: If anyone can help me out it will be of great help : // SC_Using_Classes.cpp...
3
by: katz911 | last post by:
Hello, I've encountered a strange problem which I can't seem to explain. I've written a simple C++ class that deals with a certian binary file. In one of the methods there, I wish to update a...
2
by: mauricesmith42 | last post by:
Sorry i know this is rather large to be posting, but in order to understand the question you have to see all the code //#include <windows.h> //needed for opening folders #include...
6
by: Studlyami | last post by:
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a...
8
by: zaheer031 | last post by:
I am using the following code typedef struct A { int x; int y; int z; }; int main(void) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.