10 years, 5 months ago.

null reference RPC Interface c#

Hi

i am using the RPC interface library and creating my own by own variables. I have written code for a c# program but i can't get rid oy my null reference error. The timerReset object is giving the error when i write the character 'a'

Could anyone help?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using org.mbed.RPC;
using System.Diagnostics;


namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        DigitalOut led1;                // mbed digital led out
        DigitalOut led2;                // mbde digital led out
        mbed mbed;                      // create mbed object    
        RPCVariable<char> timerReset;    // value to reset timer  

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Debug.Print("mbed RPC Hello World");

            //Create an mbed object for communication over USB (serial)
            mbed = new SerialRxTxRPC("COM3", 9600);
            //Or over serial: mbed = new SerialRPC("COM10", 9600);

            //Create new Digital Outputs on the mbed
            led1 = new DigitalOut(mbed, mbed.LED1);
            led2 = new DigitalOut(mbed, mbed.LED2);
        }

        private void flash(int n)
        {
            try
            {
                for (int i = 0; i < n; i++)
                {
                    //Call the DigitalOut objects' methods 
                    led1.write(1);          // on
                    led2.write(0);          // off
                    Thread.Sleep(250);
                    led1.write(0);          // off
                    led2.write(1);          // on
                    Thread.Sleep(250);
                }
                led2.write(0);        // reset last state - all leds are off now
            }
            catch (NullReferenceException ex)
            {
                Debug.Print("No Reference: " + ex.Message);
            }
        }

        private void btnFlash_Click(object sender, EventArgs e)
        {
            btnResetTimer.Enabled = false;
            btnExit.Enabled = false;
            btnResetTimer.Text = "Is Flashing";
            flash(10);
            btnResetTimer.Text = "Start Flash";
            btnExit.Enabled = true;
            btnResetTimer.Enabled = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                mbed.delete();
                Debug.Print("Complete");
            }
            catch (NullReferenceException ex)
            {
               Debug.Print("No Reference: " + ex.Message);
            }
            this.Close();
        }

        private void btnResetTimer_Click(object sender, EventArgs e)
        {
            timerReset.write('a');
        }
 
     
    }
}

Thanks

Steve

Hello Steven Mattu,

have you ever tried to instantiate your RPCVariable object?

snippet

RPCVariable<char> timerReset = new RPCVariable<char>();

Maybe that solves your problem.

posted by iliketux the penguin 10 Aug 2014

Thanks for your help, i though i instantiated the object on line 21. Where would the line of code you suggested go?

posted by Steven Mattu 11 Aug 2014

Hello Steven Mattu,

you can replace line 21 by the snippet or put

timerReset = new RPCVariable<char>();

inside your Form1_Load() method like the other object initializers.

posted by iliketux the penguin 11 Aug 2014

Thanks for your help, i think i'm getting close to having it working. I copies the code you suggested into Form1_Load,

//Create new Digital Outputs on the mbed
            led1 = new DigitalOut(mbed, mbed.LED1);
            led2 = new DigitalOut(mbed, mbed.LED2);
            timerReset = new RPCVariable<char>(mbed, timerReset);

its showing an error for the arguments that i entered, could there be an error with that last string?

posted by Steven Mattu 11 Aug 2014

I've taken a look into the sources. And the constructor of the RPCVariable() (RPCVariable.cs line 43) wants a string as second argument. Therefore I think you should try the following:

timerReset = new RPCVariable<char>(mbed, "timerReset");
posted by iliketux the penguin 11 Aug 2014

That worked, thanks for your help : )

posted by Steven Mattu 13 Aug 2014
Be the first to answer this question.