C
C#3mo ago
morry329#

System.ArgumentNullException: Value cannot be null. (Parameter 's')

I wanted to create a new card view inserting images, names and review text etc as per short video attached. The card view is not created but showed an exception instead:
System.ArgumentNullException: Value cannot be null. (Parameter 's')
at System.ArgumentNullException.Throw(String paramName)
at System.Text.Encoding.GetBytes(String s)
at WebApplication1.Controllers.HomeController.CreateUser(ListingProjects_ver2 obModel)
System.ArgumentNullException: Value cannot be null. (Parameter 's')
at System.ArgumentNullException.Throw(String paramName)
at System.Text.Encoding.GetBytes(String s)
at WebApplication1.Controllers.HomeController.CreateUser(ListingProjects_ver2 obModel)
` And this exception seems to point this part in controller
public async Task <JsonResult> CreateUser([Bind("ListingName","Id","ListingReview")]ListingProjects_ver2 obModel)
{
try
{

if (true)
{

if (obModel.ListingName == null)
{
return new JsonResult(Problem());
}

//this bytecode points to the exception --- ListingImgName is set to null
byte[] bytesListingImageName = Encoding.ASCII.GetBytes(obModel.ListingImgName);
Upload(bytesListingImageName);
_context.ListingVer2_DBTable.Add(obModel);
//more codes..
public async Task <JsonResult> CreateUser([Bind("ListingName","Id","ListingReview")]ListingProjects_ver2 obModel)
{
try
{

if (true)
{

if (obModel.ListingName == null)
{
return new JsonResult(Problem());
}

//this bytecode points to the exception --- ListingImgName is set to null
byte[] bytesListingImageName = Encoding.ASCII.GetBytes(obModel.ListingImgName);
Upload(bytesListingImageName);
_context.ListingVer2_DBTable.Add(obModel);
//more codes..
` I have no idea why it gets null. Any insights highly appreciated. My full code for your reference https://pastesio.com/title-8921
2 Replies
SelfLess
SelfLess3mo ago
I saw that code file.. actually its an exception,thus we should check for values , my suggestion is...add a debug point before: at line 4..where '{' method starts and arguments have values.. or at this line.. byte[] bytesListingImageName = Encoding.ASCII.GetBytes(obModel.ListingImgName);
SleepWellPupper
SleepWellPupper3mo ago
Looks like your model is not binding to the request as you expect it to. For one, you're not referencing the property ListingImgName in the BindAttribute.Included list (in the constructor). I assume ASP is thus simply ignoring the property when binding.

Did you find this page helpful?