Error mounting String with parameter . NET

Asked

Viewed 47 times

0

So guys, I’m developing a project with . NET Core Razor, and I’m assembling a string there, as follows:

string TotalInformacoes = "";
TotalInformacoes += (!String.IsNullOrEmpty(cl_1.Trim()) ? "/CLIENTE:" + cl_1 : "");
TotalInformacoes += "ÿ";

cl_1 is a parameter I’m receiving when I run a post, and in the case of this test, I’m purposely leaving the field empty. It’s an input type text.

when I debug the code I can see that cl_1 is null, but for some reason, it is giving this error in my browser:

Nullreferenceexception: Object Reference not set to an instance of an Object.

And show me in red this line:

TotalInformacoes += (!String.IsNullOrEmpty(cl_1.Trim()) ? "/CLIENTE:" + cl_1 : "";

Like this on the print:

inserir a descrição da imagem aqui

EDIT1: Code of the Onpost:

public IActionResult OnPost(string cl_1, string dt_in)
{
    string TotalInformacoes = "";
    TotalInformacoes = checkDataInicial == "1" ? "/DI:" + dt_in : "";
    TotalInformacoes += (!String.IsNullOrEmpty(cl_1.Trim()) ? "/CLIENTE:" + cl_1 : "");
    TotalInformacoes += "ÿ";

    return Page();
}

Input:

<input name="cl_1" id="cl_1" class="form-control pl-2" type="text">
<input type="date" class="form-control" id="dt_in" name="dt_in">
  • Before you ask, this input is not as required

  • @Maniero, I don’t think the link you gave me has the answer to what I need. The error is the same, but they are different situations, including his is in a get. I can see the value of this my variable, through a debug.

  • 1

    Your question also does not have the code necessary to identify where the error is. With a generic question the answer ends up being generic. These answers have an equal problem and explanation why the error occurs, so you can solve it in your code. reading this will learn that this error is always generated elsewhere, and then you can learn not to commit it more than it is a point solution.

  • My error is in this location yes. If I comment on the line, the post is normally done @Maniero

  • @Maniero, I edited the question by putting the code.

  • @Lima, see that you’re not getting anything to cl_1 in your method, debug your code, see that if you assign a default value to the parameter, the error will not happen OnPost(string cl_1="")...But that still won’t make your code work... maybe a [FromBody] help, but read the other questions pointed out by Maniero and understand what you are doing.

Show 1 more comment

1 answer

0


The error occurs because the parameter is null, as you mentioned, and you are calling the String.IsNullOrEmpty but when passing the field in the method parameter you call the Trim, then the first thing to be executed is the Trim, and the Trim expects the value to never be null, so you have the error of NullReferenceException.

You can use the IsNullOrWhiteSpace, checking that the text is not null and not just blank spaces:

(!String.IsNullOrWhiteSpace(cl_1) ? "/CLIENTE:" + cl_1 : "");

If you want to know more about what these methods do, you can see their source here:

Isnullorempty

Isnullorwhitespace

  • It worked... so the mistake was in mine Trim() not in the IsNullOrEmpty. I get it, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.