0

I've made a program by X-Copying from a computer model. By clicking a button it starts running and it goes so fine. But after its run process finished when I click that button again (without closing the program), it prompts the error :"Object reference not set to an instance of an object." I've written here the sub main that I X-Copied Visual studio:

Public Sub Main1(ByVal FileName As String)

    AddHandler myModel.Init, AddressOf OnInitilize
    AddHandler myModel.IterBottom, AddressOf OnIterationBottom
    AddHandler myModel.IterTop, AddressOf OnIterationTop
    AddHandler myModel.Converged, AddressOf OnIterationConverge
    AddHandler myModel.End, AddressOf OnFinished
    AddHandler myModel.OnMessage, AddressOf OnMessage
    AddHandler myModel.OnModsimError, AddressOf OnMessage
    XYFileReader.Read(myModel, FileName)
    Dim myModsim As New Modsim
    Csu.Modsim.NetworkUtils.ManageUnits.Unit_Conversion_Controller(myModel)
    myModsim.RunSolver(myModel)

End Sub

The error happens for "myModel"! I don't know why it's null referenced in the second run!

decyclone
  • 30,394
  • 6
  • 63
  • 80
Jalal
  • 1
  • 1
    I don't think xcopy means what you think it means. Why are you using code you don't understand? What line are you getting the exception on? – Oded Feb 01 '11 at 14:12
  • I hope I could write it by myself, but its not open source. – Jalal Feb 04 '11 at 11:09
  • On the last line error occurs : myModsim.RunSolver(myModel) – Jalal Feb 04 '11 at 11:14

2 Answers2

0

Looks like after the first run, myModel is being set to Nothing. Try to step through your code to find out where.

If it is necessary to do so, create a new instance for myModel variable.

decyclone
  • 30,394
  • 6
  • 63
  • 80
  • well I've tried checking by break points, but the error happens on the last line:" myModsim.RunSolver(myModel) " unfortunately myModel is the most important object in the code, and its been used frequently through my program. let me ask you something! do you think that this error happens in the next run, because all the objects and variables are filled in the previous run? – Jalal Feb 04 '11 at 11:10
  • Actually I know null reference means something else, but I'm really confused – Jalal Feb 04 '11 at 11:12
  • Well, what I think is happening is, after the first run, one (or more) of your variable is set to null that is being used in RunSolver method. Since myModsim is created just a couple of lines back, I feel a property of myModel must be causing this. Check all the properties of myModel for null values. – decyclone Feb 04 '11 at 11:14
0

decyclone has a point. Put a breakpoint and go thorugh it line by line. I would also look at the XYFileReader object. On the other hand, most of the time I see such error is because I call a function of another object and I do something with an object that is Nothing there. So, check inside Unit_Conversion_Controller as well.

When you see the error message, which line is the error on?

Miro J.
  • 4,270
  • 4
  • 28
  • 49