none
System.NullReferenceException when I load Autocad Point3d

    Question

  • Hello,

    I am trying to create a program that it will take data from a buzz controller and use that data to create dimensions for the AutoCad. The program was working perfectly until I declared autocad Point3d object

    Public class AutocadDraw2
    {
        private static Autodesk.AutoCAD.Geometry.Point3d newPoint;
        private static Autodesk.AutoCAD.Geometry.Point3d lastPoint;

        public static void addLine()
        {
            newPoint = new Autodesk.AutoCAD.Geometry.Point3d(5, 5, 0);
            lastPoint = new Autodesk.AutoCAD.Geometry.Point3d(10, 10, 0);
            AddLine(newPoint, lastPoint);
        }

    When a certain key is pressed  in the buzz controller it calls the addline and draw a certain line to Autocad

    foreach (ButtonStates button in args.Buttons)
    {
    BuzzHandsetControl oCtl = GetHandsetControl(nHandset++);


    // reflect the button status on the UI
    oCtl.RedLit = button.Red;
    oCtl.OrangeLit = button.Orange;
    oCtl.BlueLit = button.Blue;
    oCtl.GreenLit = button.Green;
    oCtl.YellowLit = button.Yellow;


                        switch (nHandset)
                        {
                            case 0:         //1st handset
                               if (oCtl.RedLit)
                                    AutocadDraw2.addLine();
                                break;
                            case 1:         //2nd handset

                                break;
                            case 2:         //3d handset

                                break;
                            case 3:         //4th handset

                                break;
                        }

    If i don't call addline the program is working properly. But when i call it, it gives me the error System.NullReferenceException in the Async Read of the usb device

     private void BeginAsyncRead()
            {
              
                    byte[] arrInputReport = new byte[m_nInputReportLength];
               // put the buff we used to receive the stuff as the async state then we can get at it when the read completes
                 m_oFile.BeginRead(arrInputReport, 0, m_nInputReportLength, new AsyncCallback(ReadCompleted), arrInputReport);
             }

    Could please someone help me? I am really newbie in c#. Thank you in advance!!!

    Wednesday, March 07, 2012 7:40 PM

Answers

All replies

  • Has m_oFile been initialized?


    Paulo Morgado

    Thursday, March 08, 2012 1:34 AM
  • Yes

    private void Initialise(string strPath)
    {
    // Create the file from the device path
    m_hHandle = CreateFile(strPath, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
    if ( m_hHandle != InvalidHandleValue ) // if the open worked...
    {
    IntPtr lpData;
    if (HidD_GetPreparsedData(m_hHandle, out lpData)) // get windows to read the device data into an internal buffer
    {
    try
    {
    HidCaps oCaps;
    HidP_GetCaps(lpData, out oCaps); // extract the device capabilities from the internal buffer
    m_nInputReportLength = oCaps.InputReportByteLength; // get the input...
    m_nOutputReportLength = oCaps.OutputReportByteLength; // ... and output report lengths
    m_oFile = new FileStream(m_hHandle, FileAccess.Read | FileAccess.Write, true, m_nInputReportLength, true); // wrap the file handle in a .Net file stream
    BeginAsyncRead(); // kick off the first asynchronous read
    }
    finally
    {
    HidD_FreePreparsedData(ref lpData); // before we quit the funtion, we must free the internal buffer reserved in GetPreparsedData
    }
    }
    else // GetPreparsedData failed? Chuck an exception
    {
    throw HIDDeviceException.GenerateWithWinError("GetPreparsedData failed");
    }
    }
    else // File open failed? Chuck an exception
    {
    m_hHandle = IntPtr.Zero;
    throw HIDDeviceException.GenerateWithWinError("Failed to create device file");
    }
    }

    Thursday, March 08, 2012 7:14 PM
  • Put a breakpoint here:

       m_oFile.BeginRead(arrInputReport, 0, m_nInputReportLength, new AsyncCallback(ReadCompleted), arrInputReport);

    Single step through code until you find root cause, each of those parms may or may not be NULL.  NOTE you move you NEW AsyncCallback code to a variable and use the var instead of Newing up callback in parameter list.  


    JP Cowboy Coders Unite!

    Thursday, March 08, 2012 8:28 PM
  • The m_oFile is returning null in the initalise. I changed the code in this

    m_oFile = new FileStream(new SafeFileHandle(m_hHandle, false), FileAccess.Read | FileAccess.Write, m_nInputReportLength, true);

    but it stills return null. I removed the autocad commands from the code bu the m_ofile still returns Null. However the programm is running smoothly. When i put in the programm the autocad command the program freezes. But in both cases the m_ofile returns null. It driving me crazy!

    Thursday, March 08, 2012 9:44 PM
  • Do this remove all of your new operators in the parms passed in and put them into vars.

    I think you are saying that you cannot create a new filestream right? 

    Here's a pretty good article on SAFEFILEHANDLES which I've never heard of until today.

    http://codeidol.com/csharp/csharpckbk2/Security/Obtaining-a-Safer-File-Handle/


    JP Cowboy Coders Unite!

    Thursday, March 08, 2012 11:27 PM
  • So I am guessing that you are using this method because the file is already opened somewhere else?  I've never had to use NON-Managed file handle pointers.  But perhaps that the reason it is returning NULL....  Or because it's UNMANAGED and the call is crossing the COM divide, then this could be a situation where the MANAGED COM side is not allowing the file to be accessed.  Sometimes, the COM side will not bubble and exception to .NET Framework, but I don't know that this always happens except in IE when attempting to access HTMLELEMENTs when the state is not correct. 

    JP Cowboy Coders Unite!

    Thursday, March 08, 2012 11:32 PM