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

という名前のノードで例外が発生した場合はfees、指定された XML にそのようなノードがあることを確認してください

于 2013-08-23T07:39:59.010 に答える