M
M
miXtik7892021-01-02 09:37:19
C++ / C#
miXtik789, 2021-01-02 09:37:19

Object reference not set to an instance of an object how to fix?

var param = new VkParameters();
                param.Add<string>("group_id", "59412507");
                dynamic lpresponce = null;
                lpresponce = JObject.Parse(vkapi.Call("groups.getLongPollServer", param).RawJson);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Запрос значений");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"server: {lpresponce?.response?.server?.ToString()}\nkey: {lpresponce?.response?.key?.ToString()}\nts: {lpresponce?.response?.ts?.ToString()}");
                try
                {
                    while (true)
                    {


                        
                        URI = string.Format("{0}?act=a_check&key={1}&ts={2}&wait=25",
                            lpresponce?.response?.server?.ToString(),
                            lpresponce?.response?.key?.ToString(),   
                            json != string.Empty ? JObject.Parse(json)["ts"].ToString() : lpresponce?.response?.ts?.ToString()
                                                                                                                              
                            );

did a VK bot and in case of a bug, a restart occurs, but when restarting, an error occurs Object reference not set to an instance of an object, here is the code where the error is:
URI = string.Format("{0}?act=a_check&key={1}&ts={2}&wait=25",
                            lpresponce?.response?.server?.ToString(),
                            lpresponce?.response?.key?.ToString(),   
                            json != string.Empty ? JObject.Parse(json)["ts"].ToString() : lpresponce?.response?.ts?.ToString()
                                                                                                                              
                            );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-01-02
@miXtik789

JObject.Parse(json)["ts"].ToString()
Most likely, the error occurs here.
So the json variable may be null, or there may not be a ts property inside, or it may be invalid json.
And in general it is better not to use dynamic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question