为什么我会收到 c# oneshot 搜索异常 "Object reference not set to an instance of an object"?

Why am I getting c# oneshot search exception "Object reference not set to an instance of an object"?

我最近遇到了这个异常,我不知道为什么。 2天前一切都很好,我完全不知道现在发生了什么。 现在我知道这是空异常,但同样 - 不明白为什么。它出现在 "using (var stream = service.Oneshot(oneshotQuery, outArgs))"。 service.Oneshot 都不为空,outArgs。我唯一想到的是 "stream",但为什么呢?

不难看出我对 c# 比较陌生。

异常消息框显示:

System.NullReferenceException: Object reference not set to an instance of an object. at Splunk.Service.VersionCompare(String right) at Splunk.Service.SetSegmentationDefault(Args& args) at Splunk.Service.Oneshot(String query, Args inputArgs) at Transkrypter_2.0.Form1.Form1_Load(Object sender, EventArgs e) in c:...\Form1.cs:line 45

第 45 行是 "using (var stream = service.Oneshot(oneshotQuery, outArgs))"

非常感谢您对此的帮助:

 using Splunk;



private void Form1_Load(object sender, EventArgs e)
     {
         try
         {
             var connect = new ServiceArgs
             {
                 Host = "myip",
                 Port = 8089,
                 Scheme = "https"
             };

             Splunk.Service service = new Splunk.Service(connect);

             var oneshot = new Splunk.Client.JobArgs();
             //oneshot.EarliestTime = "2015-08-09 10:25:00";
             //oneshot.LatestTime = "2015-08-09 11:25:00"
             String oneshotQuery = "search * | head 10";

             var outArgs = new JobResultsArgs
             {
                 OutputMode = JobResultsArgs.OutputModeEnum.Xml,
                 Count = 0,
             };

             using (var stream = service.Oneshot(oneshotQuery, outArgs))
             {
                 using (var rr = new ResultsReaderXml (stream))
                 {
                     foreach (var @event in rr)
                     {
                         richTextBox1.Text += "Event:" + Environment.NewLine;
                         foreach (string key in @event.Keys)
                         {
                             richTextBox1.Text += "   " + key + " -> " + @event[key];
                         }
                     }
                 }
             }

             button1.BackColor = Color.Green;
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString() ,ex.Message.ToString());
         }

另外我想提一下,这是第二个应用程序,只是为了测试这个错误,这里和我的主应用程序一样发生了同样的异常。

@NigelK 说得对。我只是忘记在 Splunk.Service service = new Splunk.Service(connectArgs); 之后添加 service.Login("login", "password"); 我发现了这一点,但上面的 @halr9000 代码只是让它更明显 - 愚蠢的我。只有一个仍然是个谜:它以前到底是怎么工作的……;)所以是的,话题结束了,非常感谢大家的帮助!