473,410 Members | 1,953 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,410 software developers and data experts.

system.NullReferenceError

Leo
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.

Public sw As StreamWriter = New StreamWriter("OutPut.txt")

In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw"
is nothing.)

Can somebody tell me why ?
Nov 20 '05 #1
6 995
I have used the follwoing (similar scenario) code in my program which works
without any problem:

dim fst as new filestream("C:\myfile.txt", Filemode.create)
dim sr as streamwriter
sr = new streamwriter(fst)
sr.writeline(1)
sr.writeline(2)

"Leo" <le*****@msn.com> wrote in message
news:03****************************@phx.gbl...
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.

Public sw As StreamWriter = New StreamWriter("OutPut.txt")

In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw"
is nothing.)

Can somebody tell me why ?

Nov 20 '05 #2
It sounds like you didn't declare it in a Public Function..
Hope this helps..
Ronald Walker

"Leo" <le*****@msn.com> wrote in message
news:03****************************@phx.gbl...
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.

Public sw As StreamWriter = New StreamWriter("OutPut.txt")

In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw"
is nothing.)

Can somebody tell me why ?

Nov 20 '05 #3
leo
It didn't work in my machine.
I tried the following.
In module1:
Public fst as new
filestream "C:\myfile.txt",Filemode.create)
Public sr as streamwriter
sr = new streamwriter(fst)
And in my startup form:
sr.writeline(1)
sr.writeline(2)

It's weird. :(

-----Original Message-----
I have used the follwoing (similar scenario) code in my program which workswithout any problem:

dim fst as new filestream("C:\myfile.txt", Filemode.create)dim sr as streamwriter
sr = new streamwriter(fst)
sr.writeline(1)
sr.writeline(2)

"Leo" <le*****@msn.com> wrote in message
news:03****************************@phx.gbl...
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.
Public sw As StreamWriter = New StreamWriter ("OutPut.txt")
In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw" is nothing.)

Can somebody tell me why ?

.

Nov 20 '05 #4
StreamWriter is a reference type and you declared it with
= New Streamwriter. Reference type assignments only
assign a pointer, and the object New Streamwriter is
pointing to can be destroyed because THAT particular
StreamWriter is no longer in use. Declare it like this
Public sw As New StreamWriter("OutPut.txt")
That makes the object stay in memory as long as sw is in
existance, not as long as New StreamWriter is in existance.

-----Original Message-----
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.

Public sw As StreamWriter = New StreamWriter("OutPut.txt")

In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw"
is nothing.)

Can somebody tell me why ?
.

Nov 20 '05 #5
Found it.. it's staying in the buffer.. after you write the line add:

sw.Flush()

now should work...
Regards,
Ronald Walker
"leo" <le*****@msn.com> wrote in message
news:04****************************@phx.gbl...
It didn't work in my machine.
I tried the following.
In module1:
Public fst as new
filestream "C:\myfile.txt",Filemode.create)
Public sr as streamwriter
sr = new streamwriter(fst)
And in my startup form:
sr.writeline(1)
sr.writeline(2)

It's weird. :(

-----Original Message-----
I have used the follwoing (similar scenario) code in my

program which works
without any problem:

dim fst as new filestream("C:\myfile.txt",

Filemode.create)
dim sr as streamwriter
sr = new streamwriter(fst)
sr.writeline(1)
sr.writeline(2)

"Leo" <le*****@msn.com> wrote in message
news:03****************************@phx.gbl...
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.
Public sw As StreamWriter = New StreamWriter ("OutPut.txt")
In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw" is nothing.)

Can somebody tell me why ?

.

Nov 20 '05 #6
Leo

StreamWriter is a reference type and you declared it with
= New Streamwriter. Reference type assignments only
assign a pointer, and the object New Streamwriter is
pointing to can be destroyed because THAT particular
StreamWriter is no longer in use.

[Leo](what happens if the streamwriter is declared with
New in module level? Will it be destroyed ? )

Declare it like this Public sw As New StreamWriter
("OutPut.txt") That makes the object stay in memory as
long as sw is in existance, not as long as New
StreamWriter is in existance.
[Leo] Can you explain in more details?
Thanks
Leo
-----Original Message-----
In my vb.net project, I created a module1.
and in module1, I declared the "sw" as a new streamwriter.
Public sw As StreamWriter = New StreamWriter ("OutPut.txt")
In Form1.vb, I got an error with the following code.
sw.writeLine(datetime.now)
(I tracked the code and in the watch window, I found "sw"is nothing.)

Can somebody tell me why ?
.

.

Nov 20 '05 #7

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

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
2
by: Scott | last post by:
Not sure if this is the right place to post this or not, but I am in the process of trying to find a Web Hosting/Isp Billing system that is reasonable in price and uses Access or SQL Server for a...
0
by: monkey king | last post by:
I have a given dll file(PDFCreator.dll) - probably generated by VC++. I want to use its method(PDFCreator()) in dotNet environment. It works well in WindiowsApplication, but bad in WebApplication...
5
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
2
by: =?Utf-8?B?TmF0aGFuIFdpZWdtYW4=?= | last post by:
Hi, I am wondering why the .NET Framework is quite different from Win32 API when it comes to displaying system modal message boxes. Consider the four following types of system modal message...
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.