C#中带有HtmlDocument引用的NullReferenceException

时间:2012-10-23 13:39:30

标签: c# unity-container html-agility-pack nullreferenceexception

我正在使用HtmlAgilityPack来从谷歌翻译中获取翻译程序的信息。我已经下载了HtmlAgilityPack dll,并在我的程序中成功引用了它。我在Unity中使用Assembly。以下是我的两个程序的代码:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;

public class GUIScript : MonoBehaviour {
    private string textField = "";
    private string input;
    public Texture2D icon;
    Dictionary search;
    Encoding code;
    // Use this for initialization
    void Start () { 
        search = new Dictionary();
        input = " ";
        code = Encoding.UTF8;
        //This is what is run to translate
        print (search.Translate("Hola","es|en",code));
    }

    // Update is called once per frame
    void Update () {

    }
    void OnGUI(){
        textField = GUI.TextField(new Rect(0, Screen.height -50, Screen.width-80, 40), textField);
        if(GUI.Button(new Rect(Screen.width-80, Screen.height -50, 80,40), icon)){
            input = textField;
            textField = "";

        }
        //GUI.Label(new Rect(0,Screen.height -70, Screen.width-80,20), search.Translate("Hola","es|en",code));
        //print (search.Translate("Hola","es|en",code));
    }
}

这是引用我的Dictionary类的代码,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Collections;
using System.Net;
using HtmlAgilityPack;


public class Dictionary{
    string[] formatParams;
    HtmlDocument doc;
    public Dictionary(){
        formatParams = new string[2];
        doc = new HtmlDocument();
    }
    public string Translate(String input, String languagePair, Encoding encoding)
     {
        formatParams[0]= input;
        formatParams[1]= languagePair;
        string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", formatParams);

        string result = String.Empty;

        using (WebClient webClient = new WebClient())
        {
            webClient.Encoding = encoding;
            result = webClient.DownloadString(url);
        }       
        doc.LoadHtml(result);
        return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText;
    }
    // Use this for initialization
    void Start () {

    }
}

运行时,我收到错误:

NullReferenceException: Object reference not set to an instance of an object
Dictionary.Translate (System.String input, System.String languagePair,System.Text.Encoding encoding) (at Assets/Dictionary.cs:32)
GUIScript.Start () (at Assets/GUIScript.cs:22)

我尝试更改代码,查找解决方案,HtmlDocument的API以及如何修复NullReferenceExceptions,但出于某种原因,我无法弄清楚为什么我会收到NullReferenceException 。这个问题一直阻碍我一两个星期,我需要继续我的项目。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

如果我算得正确,这就是第32行:

return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText

这意味着doc.DocumentNode为空或DocumentNode.SelectSingleNode("//span[@title=input]")返回null。

如果是前者,请检查您是否收到了实际的文件。您的网址可能无法正确编码。另请参阅why HTML Agility Pack HtmlDocument.DocumentNode is null?

如果是后者,XPath可能会发生奇怪的事情。我不知道这是多么相关,因为DocumentNode应该是文档的根,http://htmlagilitypack.codeplex.com/discussions/249129的讨论可能适用。根据这个,“//”是从文档的根目录搜索,您可能需要尝试doc.DocumentNode.SelectSingleNode(".//span[@title=input]")(在字符串的开头添加. )。

调试方法并准确查看这些调用的值将完成该作业。

答案 1 :(得分:0)

你想要检索什么?我打开了你正在使用的网址并为title=input做了一个简单的查找并且没有返回任何内容。我猜你正在寻找Hola的翻译,你好吗?

如果是这样,我在控制台应用程序中执行此操作。希望这会有所帮助。

    static void Main(string[] args)
    {
        string Input = "Hola";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load("http://www.google.com/translate_t?hl=en&ie=UTF8&text=Hola&langpair=es|en");

        string definition = doc.DocumentNode.SelectSingleNode(string.Format("//span[@title='{0}']",Input)).InnerText;
        Console.WriteLine(definition);
        Console.ReadKey();
    }
编辑:刚刚意识到你不是在寻找title=input而是title=Hola。正如您在我的代码中看到的那样尝试String.Format(("//span[@title='{0}']",Input)。这会将变量Input的文本插入到字符串行中。