System.ArgumentNullException was unhandled

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello
I am getting the errorSystem.ArgumentNullException was unhandled
Message=Argument cannot be Nothing.
Parameter name: file
ParamName=file
Source=Microsoft.VisualBasic
StackTrace:
at Microsoft.VisualBasic.FileIO.FileSystem.CheckFilePathTrailingSeparator(String path, String paramName)
at Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(String file, String text, Boolean append, Encoding encoding)
at Microsoft.VisualBasic.MyServices.FileSystemProxy.WriteAllText(String file, String text, Boolean append)
at ADA.frmMain.txtsave_Click(Object sender, EventArgs e) in C:UsersArjunDocumentsVisual Studio 2010ProjectsMy ADAMy ADAForm1.vb:line 247
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ADA.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

This happens when I dont type the file name
Here is my project codeImports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Imports System.IO.StreamWriter
Imports System.IO
Imports System.Windows.Forms


Public Class frmMain
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)
Private stopwatch As New Stopwatch

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() Get all com ports available
cmbBaud.Items.Add(9600) Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(2400)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)

For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) Set cmbBaud text to the first Baud rate on the list

btnDisconnect.Enabled = False Initially Disconnect Button is Disabled

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text
SerialPort1.BaudRate = cmbBaud.Text

SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()

btnConnect.Enabled = False Disable Connect button
btnDisconnect.Enabled = True and Enable Disconnect button

Me.stopwatch.Reset()
Label3.Text = "00:00:00"
Timer1.Start() starts the timer
Me.stopwatch.Start() start watch

End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()

btnConnect.Enabled = True
btnDisconnect.Enabled = False

Timer1.Stop()
Me.stopwatch.Stop()

Me.stopwatch.Reset()
Label3.Text = "00:00:00:00"



End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub

Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text pop a message box to user if he is changing ports
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text pop a message box to user if he is changing baud rate
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings is ok
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font1 As New Font ("Arial", 16, Fontstyle.Regular)
e.Graphics.DrawString(rtbReceived.Text, rtbReceived.Font, Brushes.Black, 100, 100)
End Sub

Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
PintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub


Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged
Me.rtbReceived.Select(Me.rtbReceived.TextLength, 0)
Me.rtbReceived.ScrollToCaret()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Label3.Text = String.Format("{0:00}:{1:00}:{2:00}", _
Math.Floor(elapsed.TotalHours), _
elapsed.Minutes, elapsed.Seconds)
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Label3.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", _
Math.Floor(elapsed.TotalHours), _
elapsed.Minutes, elapsed.Seconds, _
elapsed.Milliseconds)
End Sub

Private Sub txtsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsave.Click
My.Computer.FileSystem.WriteAllText("Results.txt", rtbReceived.Text, False)

Dim saveFileDialog1 As New SaveFileDialog

saveFileDialog1.InitialDirectory = "C:"

saveFileDialog1.Title = "Save text Files"

saveFileDialog1.CheckFileExists = False

saveFileDialog1.CheckPathExists = True

saveFileDialog1.DefaultExt = "txt"

saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"

saveFileDialog1.FilterIndex = 2

saveFileDialog1.RestoreDirectory = True


If (saveFileDialog1.ShowDialog() = DialogResult.OK) Then

rtbReceived.Text = saveFileDialog1.FileName

End If

Dim dDilaoge As New SaveFileDialog
dDilaoge.ShowDialog()
Dim filename As String = dDilaoge.FileName
Dim streamWriter As StreamWriter = File.CreateText(filename)
streamWriter.Write("Your Text here")
streamWriter.Flush()

saveFileDialog1.Filter = "All Files|*.*"

saveFileDialog1.ShowDialog()
rtbReceived.SaveFile(rtbReceived.SelectAll, RichTextBoxStreamType.PlainText)







Dim textType As RichTextBoxStreamType


If (saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then


If (saveFileDialog1.FilterIndex = 1) Then

textType = RichTextBoxStreamType.RichText


Else

textType = RichTextBoxStreamType.PlainText


End If


End If


Dim fs As New FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
saveFileDialog1.DefaultExt = ".txt"
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"

rtbReceived.SaveFile(fs, textType)

fs.Close()



End Sub

Private Sub txtsave_Click(sender As System.Object, e As System.EventArgs) Handles txtsave.Click
SaveFileDialog1.ShowDialog()

the application will check if the file is already exists, if exists, it will ask the user if they want to replace it



If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then

Dim ask As MsgBoxResult

ask = MsgBox("File already exists, would you like to replace it?", MsgBoxStyle.YesNo, "File Exists")

if the user decides not to replace the existing file

If ask = MsgBoxResult.No Then

SaveFileDialog1.ShowDialog()

if the user decides to replace the existing file

ElseIf ask = MsgBoxResult.Yes Then

My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)

End If

if the file doesnt exist

Else

My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)

End If

End Sub

End Class

How do I solve this probem?
Thank You

View the full article
 
Back
Top