error NullReferenceException

Hello!

I'm absolutely new in NX Journaling, so I need a help. I recorded journal in NX with GUI to change one parameter in expressions table and export geometry in .iges format. It works well in GUI. Now I want to run it in command prompt ("C:\Program Files\Siemens\NX 8.0\UGII\run_journal" C:\...recorded file location...\journal.vb -args C:\..part file location..\file.prt). But it doesn't work - System.NullReferenceException with error in line 28:
Dim expression1 As Expression = CType(workPart.Expressions.FindObject("L1"), Expression)

Help me solve my problem, please!

Journal file:

Option Strict Off
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.windows.forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Tools->Expression...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

Dim expression1 As Expression = CType(workPart.Expressions.FindObject("L1"), Expression)

Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)

workPart.Expressions.EditWithUnits(expression1, unit1, "250")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)

' ----------------------------------------------
' Menu: File->Export->IGES...
' ----------------------------------------------
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim igesCreator1 As IgesCreator
igesCreator1 = theSession.DexManager.CreateIgesCreator()

igesCreator1.ExportModelData = True

igesCreator1.ExportDrawings = True

igesCreator1.MapTabCylToBSurf = True

igesCreator1.BcurveTol = 0.0508

igesCreator1.IdenticalPointResolution = 0.001

igesCreator1.MaxThreeDMdlSpace = 10000.0

igesCreator1.ObjectTypes.Curves = True

igesCreator1.ObjectTypes.Surfaces = True

igesCreator1.ObjectTypes.Annotations = True

igesCreator1.ObjectTypes.Structures = True

igesCreator1.ObjectTypes.Solids = True

igesCreator1.SettingsFile = "C:\Program Files\Siemens\NX 8.0\iges\igesexport.def"

igesCreator1.SysDefmaxThreeDMdlSpace = True

igesCreator1.SysDefidenticalPointResolution = True

igesCreator1.InputFile = "C:\Users\mamchits\Desktop\kronsh_parameter_shell.prt"

igesCreator1.OutputFile = "C:\Program Files\Siemens\NX 8.0\UGII\kronsh_parameter_shell.igs"

theSession.SetUndoMarkName(markId2, "Export to IGES Options Dialog")

igesCreator1.OutputFile = "C:\Users\mamchits\Desktop\kr_MF\kronsh_parameter_shell.igs"

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Export to IGES Options")

igesCreator1.FileSaveFlag = False

igesCreator1.LayerMask = "1-256"

igesCreator1.DrawingList = ""

Dim objects1(-1) As TaggedObject
igesCreator1.SetDrawingArray(objects1)

igesCreator1.ViewList = "TOP,FRONT,RIGHT,BACK,BOTTOM,LEFT,TFR-ISO,TFR-TRI,User Defined"

Dim nXObject1 As NXObject
nXObject1 = igesCreator1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId2, "Export to IGES Options")

igesCreator1.Destroy()

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

You have correctly passed in the part file as an argument to the journal; now your journal will need to act on the file passed in. The first thing the journal should do is open the part file that was passed as an argument; otherwise, there is no "workPart" and you get a null reference error.

See the following comment in a related thread for some example code:
http://nxjournaling.com/comment/1406#comment-1406

Yes, I saw your comments in that problem and I tried to do it, but now I have another problem with command OpenPart(args(0)) : OpenPart is not declared. It may be inaccessible due to its protection level. How can I solve this problem?

Changed 1st part of code:

Module NXJournal
Dim theSession As Session = Session.GetSession()
dim lw as listingwindow = theSession.listingwindow
Dim dp as part
Dim wp as part
Sub Main(args() as string)

lw.open
OpenPart(args(0))

' ----------------------------------------------
' Menu: Tools->Expression...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Expression")

Dim expression1 As Expression = CType(wp.Expressions.FindObject("L1"), Expression)

Dim unit1 As Unit = CType(wp.UnitCollection.FindObject("MilliMeter"), Unit)

wp.Expressions.EditWithUnits(expression1, unit1, "550")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)

"OpenPart" is a subroutine that is declared in the other journal. You will need to copy & paste it into your journal, or write an equivalent subroutine in your journal, or use other commands to open your file. The easy way to solve this error would be to copy & paste the subroutine into your journal. Add it after Sub Main but before the End Module statement (as shown below).

Module

Sub Main(args() as string)
'code for sub main
End Sub

Sub OpenPart(byval thePart as string)
Dim basePart1 As BasePart
Dim partLoadStatus1 As PartLoadStatus
basePart1 = s.Parts.OpenBaseDisplay(thePart, partLoadStatus1)

dp = theSession.Parts.Display
wp = theSession.parts.work

partLoadStatus1.Dispose()
End Sub

End Module

Thank you very much, everything is ok now. It's really suprise that you answered so quickly and so useful. Thank you!