-1

I am trying to regex my string but I get this error:

Object reference not set to an instance of an object

my codes to get the string from supportxmr api and regex first match :

.........................................

HttpWebRequest req3 = (HttpWebRequest)WebRequest.Create("https://supportxmr.com/api/miner/43wWsbfr7zbZUjfmvbJBDUEYo3xzZqtAEQ6pEhuwnrAZ4zG1ge4ePni7snYcV1crYpUUPm82uSg2na9Jy6DT5zkTHHy54Yt/chart/hashrate/41.225.232.3");
req3.ContinueTimeout = 20000;
req3.ReadWriteTimeout = 20000;
req3.Timeout = 20000;
req3.KeepAlive = true;
req3.ContentType = "application/json";
req3.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 OPR/70.0.3728.95";
req3.Accept = "*/*";
req3.Method = "GET";
req3.Host = "supportxmr.com";
req3.Headers.Add("Sec-Fetch-Site", "same-origin");
req3.Headers.Add("Sec-Fetch-Mode", "cors");
req3.Headers.Add("Sec-Fetch-Dest", "empty");
req3.Headers.Add("Accept-Language", "en-US,en;q=0.9");
req3.Headers.Add("Accept-Encoding", "gzip, deflate");
req3.AutomaticDecompression = DecompressionMethods.GZip;
req3.ServicePoint.Expect100Continue = false;
HttpWebResponse response3 = (HttpWebResponse)req3.GetResponse();
try
{
response3 = (HttpWebResponse)req3.GetResponse();
}
catch (WebException ex)
{
response3 = (HttpWebResponse)ex.Response;
}
string result3 = new StreamReader(response3.GetResponseStream()).ReadToEnd();
return Regex.Match(result3,"\"hs\":(.*?)}").Groups[1].Value;
robsiemb
  • 4,959
  • 7
  • 24
  • 33
  • "I am trying to regex my string" what exactly are you trying to match? obviously from your code is that you have a `null` value at `Groups[1]` we don't know your expected result – Mong Zhu Aug 10 '20 at 11:10
  • How did you format and store that input string? Please show how do you create that `myString`, if it's well-formatted your code perfectly works (result: 1660) – al1812 Aug 10 '20 at 11:16
  • Your code is working. Can you post how your myString is declared from the code? – tontonsevilla Aug 10 '20 at 11:23
  • Parsing JSON with regex is a recipe for disaster. Any reason you're doing it this way? – Jamiec Aug 10 '20 at 11:24
  • Just a guess, but you're likely hitting you exception handler and getting a completely different response to what you're expecting. Have you tried stepping through and seeing whats inside `response3`? – Jamiec Aug 10 '20 at 11:32
  • What is the stack trace of your exception? – SomeBody Aug 10 '20 at 11:32
  • @Jamiec, yes i checked response3 and i didn't saw any problem – LuciferWhit3 Aug 10 '20 at 11:34
  • 4
    Please do not vandalize your posts. Once you've posted a question, it belongs to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-for-a-dissociation-request) – robsiemb Aug 10 '20 at 11:51

1 Answers1

2

This code in the question assumes at least 1 group result will be found:

return Regex.Match(result3,""hs":(.*?)}").Groups[1].Value;

First check if Regex.Match(result3,"\"hs\":(.*?)}").Groups contains 1 or more items. When it contains zero items, you'll see an Object reference not set to an instance of an object error.

Bernard Vander Beken
  • 4,315
  • 5
  • 46
  • 70