NullReferenceException异常在C#中的HTMLDocument参考 [英] NullReferenceException with HtmlDocument reference in C#
问题描述
我使用 HtmlAgilityPack
为了凑信息关闭谷歌的翻译进行翻译计划。我已经下载了 HtmlAgilityPack
的dll,并成功地在我的程序中引用它。我正在使用统一大会。下面是我对两个程序代码:
I am using HtmlAgilityPack
in order to scrape information off of Google Translate for a translation program. I have downloaded the HtmlAgilityPack
dll, and successfully referenced it in my program. I am using Assembly in Unity. Below is my code for the two programs:
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));
}
}
这是引用我的<$ C $代码如下图所示C>字典类:
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 () {
}
}
在运行此,我收到错误消息:
When running this, I receive the error:
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,以及如何解决 NullReferenceException异常
,但由于某些原因,我想不出为什么我收到一个的NullReferenceException
。这个问题已经拖我的一两个星期,现在我需要用我的项目继续前进。 !任何帮助将不胜感激。
I have tried changing code, looking up solutions, the API for HtmlDocument
, and how to fix NullReferenceExceptions
, but for some reason I cannot figure out why I am getting a NullReferenceException
. This problem has been holding me back for a week or two now and I need to move on with my project. Any help would be greatly appreciated!
推荐答案
如果我计算正确,这是第32行:
If I've counted correctly, this is line 32:
return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText
这意味着要么 doc.DocumentNode
为空或 DocumentNode.SelectSingleNode( //跨度[@标题=输入])
终止返回null。
That means either doc.DocumentNode
is null or DocumentNode.SelectSingleNode("//span[@title=input]")
is returning null.
如果是前者,请检查您收到了实际文档的反面。您的网址可能不正确编码。另请参见为什么HTML敏捷性包HtmlDocument.DocumentNode为空?
If it is the former, check you are receiving an actual document back. Your URL may not be encoded correctly. See also why HTML Agility Pack HtmlDocument.DocumentNode is null?
如果是后者,它可能是一些奇怪的使用XPath发生。我不知道如何相关,这是因为 DocumentNode
应该是文档的根在的 http://htmlagilitypack.codeplex.com/discussions/249129 可以申请。根据这一点,'//'是从搜索的文件的,你可能必须尝试 doc.DocumentNode.SelectSingleNode(.//跨度[@title的根=输入])
代替(添加。
字符串的开头)。
If it is the latter, it could be something odd happening with XPath. I don't know how relevant this is as DocumentNode
should be the root of the document, the discussion at http://htmlagilitypack.codeplex.com/discussions/249129 could apply. According to this, '//' is searches from the root of the document, and you may have to try doc.DocumentNode.SelectSingleNode(".//span[@title=input]")
instead (adding a .
to the beginning of the string).
调试方法,看到这些调用的确切值将完成这项工作。
Debugging the method and seeing exactly the values of these calls will finish the job.
这篇关于NullReferenceException异常在C#中的HTMLDocument参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!