Author Topic: NullReferenceException when switching Forms  (Read 4808 times)

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
NullReferenceException when switching Forms
« on: September 09, 2013, 09:14:15 AM »
I have a NullReferenceException error, so I made a smaller program to try to isolate the problem , but I'm still not sure.

I have 2 forms, each has a tank control. They read 2 different addresses on the same PLC using 2 ModbusTCPComm driver instances, 1 for each form.
When I open the second form, using the form change button I get an error at line 553 of ModbusTCPComm: Object reference not set to an instance of an object.


How can I fix this?

Thank you,

Quinton Wideman
Rolan Inc.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: NullReferenceException when switching Forms
« Reply #1 on: September 09, 2013, 09:49:55 AM »
Insert this line of code prior to line 553:

        If Transactions(TNSByte) Is Nothing Then Exit Sub

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #2 on: September 09, 2013, 11:24:20 AM »
That takes care of the error, but now the second Comm Driver Instance doesn't return a value to the controls. I'm using a "gauge" now, but it doesn't read any value at all. (I did verify that there is a value there, the first comm driver reads it fine.)

Now I get an error at line:             348:  Dim x As New MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs(New String() {ex.Message}, PolledAddressList(i).Address.Address, 0)


Quinton

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #3 on: September 10, 2013, 08:56:51 AM »
I did a bit more messing around:

I started with a clean AdvancedHMI project and made 2 forms: each with a gauge, a Modbus TCPIP comm driver to one PLC, the two gauges reading different addresses on that same PLC.
I used a formchange button to switch forms.

When I run this, the first gauge works fine, and the gauge on form2 does not respond.
Next, I deleted the TCPIP driver for the first gauge. Obviously, that gauge now displays an error - but the second gauge works now!

It seems the second comm driver was preventing the first from working.

Why? Is this a bug, or is something extra required to set this up correctly?
This should be simple enough...


Any help would be much appreciated,

Regards,
Quinton Wideman

TSLJon

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #4 on: September 10, 2013, 09:57:08 AM »
Are you do everything from scratch?

I've just created 2 forms using 20 different drivers and fine with ModbusTCP.

I take it you are using 2 different drivers as in one on one form, and one on another? Within the drivers are they mapped to the correct forms?

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #5 on: September 10, 2013, 10:10:29 AM »
Are you do everything from scratch?
Yes, I did this with a new download of AdvancedHMI just to be sure.


Within the drivers are they mapped to the correct forms?
I'm not an expert, TSLJon, what do u mean by that?
I didn't have to write any code for this, I  did it all using the properties of the gauges and comm drivers. There is only one comm driver on each form, and that is the only option for the gauge's "CommComponent" setting.

Do I need to modify "ModbusTCPCom.vb"?



EDIT: Did I mention that I'm only connecting to one PLC? I have multiple "CommComponents", one per form. Is this a problem?
« Last Edit: September 10, 2013, 10:12:20 AM by qwideman »

TSLJon

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #6 on: September 10, 2013, 12:00:21 PM »
Neither am I an expert at all but I've produced exactly what I need so far with a few minor bits.

Very simply setup the ModbusTCPCom with the details of your PLC - IP Address, ensure port is correct and name it. Also check the SynchronisingObject is the correct form.

Choose a simple gauge to start with, then goto PLCValue and type in the holding register you wish to query. Set the scale and it should work. Add more one at a time and check it each time.

You do not need to change anything at present.

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #7 on: September 10, 2013, 02:03:27 PM »
There is no difference.  I can't use multiple drivers  connected to the same PLC, although Archie says it should work : http://advancedhmi.com/forum/index.php?topic=118.0    ???



Quinton

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #8 on: September 13, 2013, 05:16:42 PM »
This still isn't working.

     Just to make sure I have it right: When I want a control to communicate with a PLC, I put a driver instance on the same form as the control? If I have 2 forms, and I close one and open another, the first driver is no longer active so each form needs its own driver?
     How is it then that my second driver, (the one on "Form2") only works when I delete the driver on "Form1"?
I stand to be corrected, but it seems to me they might be interfering.

I have tried this with an Eaton PLC and with an EZ. Both act similar: The second CommComponent only seems to work after I delete the first one.

But importantly, why is this not working for me when it works fine for TSLJon?
This program doesn't contain any code that I wrote.
What else can I try?

More help would be greatly appreciated! (Thanks again for the help so far. Much appreciated!) Hopefully I can get to the bottom of this.

Quinton Wideman
Rolan Inc.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: NullReferenceException when switching Forms
« Reply #9 on: September 13, 2013, 10:36:43 PM »
I finally was able to track this down. Download and extract this file, then replace the one in your project:

http://www.advancedhmi.com/ModbusTCPComPatchV358.zip

qwideman

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: NullReferenceException when switching Forms
« Reply #10 on: September 16, 2013, 12:35:48 PM »
Thank you very much Archie!  That fixed the problem.

Quinton Wideman