0

我进入TargetInvocationException下面的代码。

InnerException 是“System.NullReferenceException”。

我不明白这里的问题。谁能告诉我,问题可能出在哪里?

C#代码:

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

    }
}

例外是在线 -

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

2 回答 2

1

SelectSingleNodenull如果没有找到匹配的节点,将返回。不检查null您正在访问返回节点的属性(如InnerText)。它会导致System.NullReferenceException

如果你得到 node named 的异常fees,检查你在给定的 XML 中有这样的节点

于 2013-08-23T07:39:59.010 回答
0

猜猜实现某种“TryGet”函数来处理这种操作总是更整洁。

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
}
于 2013-08-23T08:04:24.187 回答