Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text was "null"

I have a problem with my code.

I got the code from a tutorial and at first it worked fine, but then it stopped working and got the error code text was "null".

Here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private static void Client_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
{
    var id = e.Message.Chat.Id;
    var text = e.Message.Text;

    text = text.Split(' ')[1];      //error right here!
    switch (text)
    {
        case "now":
            var response = DateTime.Now.ToString();
            Client.SendTextMessageAsync(id, response);
            break;
    }
}

EDIT:

I tried something else and got another error, maybe im one step foward?!

System.IndexOutOfRangeException: "Index was outside the bounds of the array.

”’
var id = e.Message.Chat.Id;
var text = e.Message.Text ?? "";

        text = text.Split(' ')[1] ?? "";
        switch (text)

”’

>Solution :

change this line

var text = e.Message.Text

to

 var text = e.Message.Text ?? "";

then change

text = text.Split(' ')[1]; 

to

if(text.Split(' ').Length >= 2)
  text = text.Split(' ')[1];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading