System.NullReferenceException: Object reference not set to an instance of an object WPF

2.9k views Asked by At

I'm getting TargetInvocationException in below code.

InnerException is "System.NullReferenceException".

I don't understand problem here. Can anyone tell me, where could be the problem?

C# code:

public class Harvest_Project
{

    public string _name { get; set; }
    private DateTime _over_budget_notified_at;
    private bool _billable;
    private DateTime _created_at;
    private bool _active;
    private enum _bill_by { Tasks, People, none };
    public int _client_id;
    private string _code;        
    private string _notes;
    private enum _budget_by { project, project_cost, task, person, none };
    private float _budget; //Budget in hrs
    private DateTime _latest_record_at; 
    private DateTime _earliest_record_at;
    private int _fees;
    public int _id { get; set; }
    private DateTime _updated_at;
    private XmlNode _node;

    public int getId() { return this._id; }

    public Harvest_Project()
    { }

    public Harvest_Project(XmlNode node)
    {
        this._node = node;
        this._name = node.SelectSingleNode("name").InnerText;
        this._created_at = Harvest_Base.storeTime(node.SelectSingleNode("created-at").InnerText);
        this._updated_at = Harvest_Base.storeTime(node.SelectSingleNode("updated-at").InnerText);
        this._over_budget_notified_at = Harvest_Base.storeTime(node.SelectSingleNode("over-budget-notified-at").InnerText);
        this._latest_record_at = Harvest_Base.storeTime(node.SelectSingleNode("hint-latest-record-at").InnerText);
        this._earliest_record_at = Harvest_Base.storeTime(node.SelectSingleNode("hint-earliest-record-at").InnerText);
        this._billable = bool.Parse(node.SelectSingleNode("billable").InnerText);
        try
        {
            this._id = Convert.ToInt32(node.SelectSingleNode("id").InnerText);
            this._client_id = Convert.ToInt32(node.SelectSingleNode("client-id").InnerText);
            this._budget = float.Parse(node.SelectSingleNode("budget").InnerText);
            this._fees = Convert.ToInt32(node.SelectSingleNode("fees").InnerText);
        }
        catch (FormatException e)
        {
            Console.WriteLine("Input string is not a sequence of digits.");
        }
        catch (OverflowException e)
        {
            Console.WriteLine("The number cannot fit in an Int32.");
        }

        this._code = node.SelectSingleNode("code").InnerText;
        this._notes = node.SelectSingleNode("notes").InnerText;

        //TODO
        //_this.bill_by
        //this._budget_by

    }
}

Exception is at line -

this._fees = Convert.ToInt32(node.SelectSingleNode("fees").InnerText);
2

There are 2 answers

0
cscmh99 On

Guess it's always neater to implement some sort of "TryGet" function to handle this kinda operation.

public static class XmlNodeExtension
{
    public static bool TryGetNodeInnerText(this XmlNode node, string xPath, out string innerText)
    {
        Debug.Assert(!string.IsNullOrEmpty(xPath));

        innerText = null;

        if (node == null)
            return false;

        var selectedNode = node.SelectSingleNode(xPath);
        if (selectedNode == null)
            return false;

        innerText = selectedNode.InnerText;
        return true;
    }
}

string fees;
if (!node.TryGetNodeInnerText("fees", out fees))
{
     // log error or any handling
}
0
Damith On

SelectSingleNode will return null if no matching node is found. without checking for null you are accessing properties of returned node ( like InnerText). it will cause System.NullReferenceException

if you get exception for node named fees, check you have such node in given XML