草庐IT

NullReferenceException

全部标签

c# - 为什么尝试访问 null 的属性会导致某些语言出现异常?

对于某些编程语言(例如C#、Javascript),真正困扰我的是尝试访问null的属性会导致错误或异常发生。例如,在下面的代码片段中,foo=bar.baz;如果bar为null,C#将抛出一个讨厌的NullReferenceException并且我的Javascript解释器将提示Unabletogetvalueoftheproperty'baz':object为空或未定义。理论上我可以理解这一点,但在实际代码中我经常有一些深层次的对象,比如foo.bar.baz.qux如果foo、bar或baz中的任何一个为null,则我的代码已损坏。:(此外,如果我在控制台中评估以下表达式,结

javascript - 在 WCF 服务方法中使用 JSON

在一个较大的项目中,我无法让WCF服务方法使用JSON参数。所以我制作了一个较小的测试用例,并且行为得到了回应。如果我调试服务,我可以在服务调用时看到参数值为null。Fiddler确认正在发送JSON,JsonLint确认它是有效的。下面的代码带有调试注释。[ServiceContract]publicinterfaceIWCFService{[OperationContract][WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest,ResponseFormat=WebMessageFormat.Json,UriTemplat

pointers - 如何跨包中的文件使用全局变量?

我有以下文件结构:模型/db.gotypeDBstruct{*sql.DB}vardb*DBfuncinit(){dbinfo:=fmt.Sprintf("user=%spassword=%sdbname=%ssslmode=disable",DB_USER,DB_PASSWORD,DB_NAME)db,err:=NewDB(dbinfo)checkErr(err)rows,err:=db.Query("SELECT*FROMprofile")checkErr(err)fmt.Println(rows)}funcNewDB(dataSourceNamestring)(*DB,error

c# - 为什么此 LINQ 抛出 NullReferenceException?

privateSmtpClientgetServer(){return(fromeindoc.Elements("emailsetting")selectnewSmtpClient(){Host=e.Attribute("server").Value,Port=Convert.ToInt32(e.Attribute("port").Value)}).FirstOrDefault();}xml配置文件:为什么抛出异常:NullReferenceException未处理对象引用未设置到对象的实例。我是LINQ的新手,请帮忙。 最佳答案

c# - 你调用的对象是空的

我有以下代码-privatestaticvoidconvert(){stringcsv=File.ReadAllText("test.csv");XDocumentdoc=ConvertCsvToXML(csv,new[]{","});doc.Save("update.xml");XmlTextReaderreader=newXmlTextReader("update.xml");XmlDocumenttestDoc=newXmlDocument();testDoc.Load(@"update.xml");XDocumentturnip=XDocument.Load("update.x

c# - 当 .Count() 大于零时从 .Select() 获取 NullReferenceException LINQ to XML

我有一个XML文件:BlahdiddyBlah-Blah因此,鉴于以下情况:XNamespacenameSpace="http://www.vmware.com/vcloud/v1";vardoc=XDocument.Parse(xml);当我在VS立即窗口中调试以下代码时:doc.Root.Elements(nameSpace+"Link").Count()我得到一个期望值8。但是当我进一步使用以下方法时:varvdcs=doc.Root.Elements(nameSpace+"Link").Select(x=>newvDC(){Name=x.Attribute("name").Va

c# - 错误 : exception of type 'System.NullReferenceException' occurred in exe

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭19小时前。Improvethisquestion大家好每当我运行这个程序时,我总是会收到上面的错误,并烦扰我为什么。我执行了StepInto并发现当我点击将对象添加到集合时发生异常(在下面的代码中标记)。关于可能导致此问题的任何想法?Img和Category类是带有inotify接口(interface)的普通ol类,而Movies类有一个O

c# - 无法使用 foreach 获取 XML 值

我想使用foreach获取subdata和subdata2值,但由于某种原因,我得到了空引用异常。XML:Name11132代码:XmlNodedatanode=doc.DocumentElement.SelectSingleNode("/project/data");XmlNodeinnerDataNode;foreach(XmlNodedataVarindatanode){innerDataNode=datanode.SelectSingleNode("/subdata");intsubdataVal=XmlConvert.ToInt16(innerDataNode.InnerTe

windows - 终结器中的 WCF NullReferenceException

我们遇到了WCF的问题,花了我们很长时间来诊断,所以我将它们张贴在这里以防有人遇到类似的问题(在其他任何地方都找不到引用)。我们的流程经常会因以下未处理的异常而崩溃:System.NullReferenceException:Objectreferencenotsettoaninstanceofanobject.atSystem.ServiceModel.Channels.OverlappedContext.CompleteCallback(UInt32error,UInt32numBytes,NativeOverlapped*nativeOverlapped)atSystem.Run

xml - 在 Windows Phone 7 上使用 XDocument 时从抛出 NullReferenceException 的 XML 文档加载数据

好的。因此,对于WindowsPhone7应用程序,假设我有以下XML文件Value3Value3我有以下类定义publicclassmyObject{publicstringProperty1{get;set;}publicstringProperty2{get;set;}publicstringProperty3{get;set;}publicmyObject(string_property1,string_property2,string_property3){this.Property1=_property1this.Property1=_property1this.Prope