1

I have this function in C# which loads elements in a PropertyGrid.

public void LoadElements(Document selectedDocument)
{
    try
    {
        if (this.PropertyGrid != null)
        {
            DocumentProperties DP = new DocumentProperties(selectedDocument);
            if(DP != null)
            {
                this.PropertyGrid.SelectedObject = DP;
            }
        }
    }
    catch (Exception ex)
    { Logger.LogError(ex); }
}

The problem is that it throws an exception everytime.

HRESULT -2147467261
The object reference is not set to an instance of an object.    
   in System.Windows.Forms.PropertyGrid.set_SelectedObjects(Object[] value)
   in System.Windows.Forms.PropertyGrid.set_SelectedObject(Object value)
   in FloatingForms.FloatingPropertiesForm.LoadElements(Document selectedDocument) dans         d:\DEV\FloatingForms\FloatingPropertiesForm.cs:ligne 108

I don't understand because I test if PropertyGrid is not null and if my DocumentProperties Object is not null. Evrything is not null but that doesn't work.

Could you help me to find a solution please ?

EDIT

I have a WPF grid in which there are Canvas. Everytime a Canvas is selected Activated event the following Element_GotFocus eventHandler is called.

private void Element_GotFocus(object sender, EventArgs e)
{
    try
    {
        if (MyApplication.PropertiesPanel != null)
        {
            if (sender is Canvas)
            {
                if ((sender as Canvas).Tag != null)
                {
                    if ((sender as Canvas).Tag is Document)
                    {
                        if (MyApplication.PropertiesPanel != null)
                        {
                            MyApplication.PropertiesPanel.LoadElements((sender as Canvas).Tag as Document);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    { Logger.LogError(ex); }
}

EDIT 2

DocumentProperties Class

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

namespace Entities
{
    public class DocumentProperties
    {
        #region Properties

        private int m_iDocumentId;
        private string m_sDocumentName;
        private string m_sDocumentDescription;

        #endregion

        #region Getters / Setters

        public int Id
        {
           get { return this.m_iDocumentId; }
        }

        public string Name
        {
           get { return this.m_sDocumentName; }
        }

        public string Description
        {
           get { return this.m_sDocumentDescription; }
        }

        #endregion

        #region Constructors

        public DocumentProperties(Document Document)
        {
            try
            {
                this.m_iDocumentId = Document.Id;
                this.m_sDocumentName = Document.Name;
                this.m_sDocumentDescription = Document.Description;
            }
            catch (Exception ex)
            { Logger.LogError(ex); }
        }

        #endregion
    }
}

PS : Document Class is a complex object in which there is lots of designing properties I don't want to display in the propertygrid. That's why I defined DocumentProperties, to limit user access to properties.

T00rk
  • 1,603
  • 2
  • 11
  • 17
  • 4
    What about `selectedDocument`? – Sayse Sep 23 '14 at 07:41
  • Yeah, why don't put a guard around selectedDocument? Could be null? – alessandro Sep 23 '14 at 07:42
  • It is OK. It implements a non null DocumentProperties object – T00rk Sep 23 '14 at 07:42
  • Have you added anything into the ProperttGrid before attempting to select it? – gbjbaanb Sep 23 '14 at 07:44
  • I just tried adding a if(selectedDocument != null) but it's ok, it is not null – T00rk Sep 23 '14 at 07:44
  • @gbjbaanb No, there is nothing in it. that's my only function which defines the selectedObject. – T00rk Sep 23 '14 at 07:46
  • @Patrick Hofman - I'm not convinced that is a valid duplicate for this question, the op already has safeguards in place for NRE's but is still unable to solve their issue, T00rk, can you create a short reproducible problem to help? i'd imagine some property on one of your objects isn't set correctly which `SelectedObject` needs – Sayse Sep 23 '14 at 07:52
  • I agree on that. Reopened. I guess it concerns the properties of the object too, that's why I sticked longer with the duplicate. – Patrick Hofman Sep 23 '14 at 07:52
  • From what I see the OP is using Microsoft.Office.Core there? (for documentproperties and word.Document? or am I mistaken there?) – Thomas Sep 23 '14 at 07:53
  • No DocumentProperties is a custom class I defined. – T00rk Sep 23 '14 at 07:54
  • Does your property grid have objects in it? I'd imagine the issue would be something like that. – Sayse Sep 23 '14 at 07:54
  • 1
    One of your properties in `DocumentProperties` is throwing a NRE. – leppie Sep 23 '14 at 07:54
  • 1
    @T00rk: Yes I did. I explained in the comment above. If not closed as duplicate, I would close as 'not reproducable' or 'unclear', since it isn't reproducable with this code only. – Patrick Hofman Sep 23 '14 at 07:54
  • @KirillShlenskiy I'm gonna investigate on it ... Thank you all for your time – T00rk Sep 23 '14 at 07:56
  • 1
    TOOrk: Leppie could have a point there. As you said it is a custom class it would be good to see the code for that class (especially also the constructor and the properties themselves of that class) – Thomas Sep 23 '14 at 07:56
  • Did you see which line was throwing error? – mrsrizan Sep 23 '14 at 07:58
  • @Thomas Nope, there is no problem with the `DocumentProperties` object. It only has 3 properties which are all defined. I'm in debug mode step-by-step, I put a break point just before `this.PropertyGrid.SelectedObject = DP;` and every properties are defined. None of them throw an exception – T00rk Sep 23 '14 at 08:00
  • @mrsrizan Yes this is this line `this.PropertyGrid.SelectedObject = DP;` – T00rk Sep 23 '14 at 08:01
  • @T00rk: Can you post the full code that reproduces this problem? – Patrick Hofman Sep 23 '14 at 08:02
  • @PatrickHofman It's a huge application. I can post the event which calls this function if you want, but I can't post all my source code. – T00rk Sep 23 '14 at 08:05
  • I understand, but I am dying to paste something in VS to check the issue, but I can't... – Patrick Hofman Sep 23 '14 at 08:06
  • 2
    @T00rk A really useful information would be the definition of DocumentProperties and Document, mainly their properties and any code the getter of those properties might call. – Dirk Sep 23 '14 at 08:11
  • Sorry but I really can't post all my Solution. There is 12 projects in it – T00rk Sep 23 '14 at 08:12
  • @T00rk: Place a try/catch around every getter in `DocumentProperties`. Even better, attach a debugger and tell it to break on all handled exceptions. This will give you the exact location. – leppie Sep 23 '14 at 08:16
  • for your answer to my inquiry: how did you then define propertygrid? (never used it myself but possibly that the problem then lies with that one) – Thomas Sep 23 '14 at 08:16
  • FYI, there is a tool called dotPeek. It can server as a symbol server for the VS debugger and if you set it up you can actually step into the .NET framework code. – Dirk Sep 23 '14 at 08:17
  • @Thomas I simply defined it using visual designer. I haven't changed any properties. – T00rk Sep 23 '14 at 08:20
  • Thanks to all of you for your time, I'm gonna find another way to do that without using propertygrid ... – T00rk Sep 23 '14 at 08:38

0 Answers0