Unhandled exception errror
Navigates to RoboticsConnection.com Home RoboticsConnection.com HomePage
RoboticsConnection User Forum
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Unhandled exception errror Expand / Collapse
Author
Message
Posted Tuesday, June 24, 2008 5:22 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: Tuesday, July 01, 2008 7:18 PM
Posts: 1, Visits: 6
Hi all,
we are working on a summer robotics project at Mesa State College and have run into an early snag.
We have the serializer controller board with the bluetooth serial interface with one servo connected to servo pin 1 (digital 8). When we try to run this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RoboticsConnection.Serializer;
using RoboticsConnection.Serializer.Components;
using RoboticsConnection.Serializer.Controllers;
using RoboticsConnection.Serializer.Ids;
using RoboticsConnection.Serializer.Sensors;

namespace ServoApp
{
class Program
{
Serializer serializer;
ServoMotorController antennaServo;

private void InitializeSerializer()
{
serializer = new Serializer();
serializer.BaudRate = 19200;
serializer.PortName = "COM40";
serializer.CommunicationStarted += new SerializerEventHandler(serializer_CommunicationStarted);


antennaServo = new ServoMotorController(serializer);
antennaServo.ServoMotorId = ServoMotorId.ServoMotor1;
antennaServo.Position = -100;

serializer.Run();
}

void serializer_CommunicationStarted(Serializer sender)
{
serializer.BlinkLED(LedId.Led1, 50);
serializer.BlinkLED(LedId.Led2, 100);
serializer.Units = Units.English;
}

static void Main(string[] args)
{
Program p = new Program();

System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(p.InitializeSerializer));
thread.Start();


for (int position = -99; position <= 100; ++position)
{
p.antennaServo.Position = position;
for (int i = 0; i < 100000; ++i) { ; }
}
p.serializer.ShutDown();
}

}
}


we get this error:

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
at RoboticsConnection.Serializer.Internal.CommunicationTask.ThreadProc()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()




Does anyone have any ideas on what might be wrong here?
Thanks



Post #568
Posted Thursday, June 26, 2008 7:11 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: 2 days ago @ 6:31 AM
Posts: 270, Visits: 393
First of all, why are you attempting to new off a Thread?   Could you please explain, because this isn't needed at all.  Do you need to have a multithreaded application?  Maybe you have one application talking to many peripheral devices (one being the Serializer), and you want each one to run in their own thread?

The SerializerLib has two threads of execution.  The main thread handles invocations to the objects associated with the library, while the other handles communication with the Serializer.   So, using threads the way you are attempting to buys you nothing with respect to the Serializer. 

I think the reason you are seeing the error is because you define an instance of Program, 'p' in Main(), then you tell the new thread to invoke p.InitializeSerializer() when it starts.   However, the new thread knows nothing about the instance of 'p', which was instantiated in the main thread, nor does it know anything about 'serializer'.   When you attempt to instantiate a new Serializer() object in InitializeSerializer(), the library needs to have that instance of 'serializer', because it's going to go off and attempt to create it's needed threads.  Since 'serializer' doesn't exist in the new thread, an exception is thrown.  If you single step over the program, I suspect you will see this behavior.

If you can make a good case to me as to why you need the additional thread, I'll be glad to help you further.

I believe this link might be of use to you.  Notice how the Work class is defined outside of the Test class, and it hs all of it's members (Data) encapsulated in the class.  So, when the DoWork() and DoMoreWork() is invoked from Thread.Start(), it has instances of Data to work on:

     http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx

Best Regards,

Jason Summerour
President,
Summerour Robotics Corporation
Microsoft MVP
www.roboticsconnection.com

Post #569
Posted Thursday, June 26, 2008 7:27 AM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: 2 days ago @ 6:31 AM
Posts: 270, Visits: 393
I also moved this topic under the Serializer .NET Library topic.

Best Regards,

Jason Summerour
President,
Summerour Robotics Corporation
Microsoft MVP
www.roboticsconnection.com

Post #570
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: jsummerour

Permissions Expand / Collapse

All times are GMT -8:00, Time now is 12:10pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.125. 10 queries. Compression Disabled.