Find and Replace in Clone Log

Hello,
I am working on a journal to find and replace values in a clone log file.
Currently we use an excel file to update expressions.
We write the excel file and then it updates things like job and part number expressions in NX.
The journal I am trying to create should find a word in the clone log and replace it with the value of the expression. Then it should save the log.

When I run the journal I get a Journal Execution Error:
System.NullReferenceException: Object reference not set to an instance of an object, at Module1.Main() in (C: Drive Path).

Any help or guidance would be appreciated. Here is the current code below:


Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim myStreamReaderL1 As System.IO.StreamReader
Dim myStreamWriter As System.IO.StreamWriter
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim as_num As Expression = CType(workPart.Expressions.FindObject("AS_Num"), Expression)
Dim myStr As String
myStreamReaderL1 = System.IO.File.OpenText("U:\mfrank\Journals\TEST_CLONE.clone")
myStr = myStreamReaderL1.ReadToEnd()
myStreamReaderL1.Close()

myStr = myStr.Replace("JobNum", (as_num.ToString))
myStreamWriter.WriteLine(myStr)
myStreamWriter.Close()

lw.WriteLine(as_num.ToString)
lw.WriteLine("")

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

My best guess is that it is the stream writer that is throwing the error. It appears that you have a reference to a stream writer, but have not instantiated an actual stream writer object (i.e. you have not told it what file to write to).

Some example code for using a stream writer in a journal can be found here:
http://nxjournaling.com/content/write-text-file

I ended up with this journal that works like it should :)My hope is to replace the hard coded file location with a file picker so the user can choose which clone log to work on.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim As_Num As Expression = CType(workPart.Expressions.FindObject("AS_Num"), Expression)
Dim As_Num_Value As String = As_Num.StringValue

System.IO.File.WriteAllText("U:\mfrank\Journals\TEST_CLONE.clone", System.IO.File.ReadAllText("U:\mfrank\Journals\TEST_CLONE.clone").Replace("JobNum", (As_Num_Value)))

lw.WriteLine("AS Job Number Set To: ", As_Num_Value)
lw.WriteLine("")

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module

Thank you for your help and quick response.

Here is the journal I made that has a file picker built in:

Option Strict Off
Imports System
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String

fd.Title = "Open File Dialog"
fd.InitialDirectory = "T:\"
fd.Filter = "All files (*.*)|*.*|Allfiles (*.*|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True

If fd.ShowDialog() = DialogResult.OK Then
strFileName = fd.FileName
End If

Dim As_Num As Expression = CType(workPart.Expressions.FindObject("AS_Num"), Expression)
Dim As_Num_Value As String = As_Num.StringValue

System.IO.File.WriteAllText((strFileName), System.IO.File.ReadAllText((strFileName)).Replace("JobNum", (As_Num_Value)))

lw.WriteLine("AS Job Number Set To: " & As_Num_Value)
lw.WriteLine("")

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

End Module