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:
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
– user153240
@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.
– user153240
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.
– Maniero
My error is in this location yes. If I comment on the line, the post is normally done @Maniero
– user153240
@Maniero, I edited the question by putting the code.
– user153240
@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 happenOnPost(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.– Leandro Angelo