0

if I pass wrong id in FindByIdAsync(),program crash immediately with Exception null reference..... if the id is correct, it returns the user with no problem.

btw the catch not working when the scope arrive at findByIdAsync it crash there with Exception null

the problem happened when i use GetUser(int id)

public class UserRepository
{

    private ApplicationDbContext Db { get; set; }
    private readonly UserManager<ApplicationUser> UserManager;
    private readonly IMapper mapper;
    private readonly IJSRuntime jSRuntime;
    private readonly FileUpload fileUpload;
    public UserRepository(IMapper mapper, IJSRuntime jSRuntime, ApplicationDbContext Db, FileUpload fileUpload, UserManager<ApplicationUser> UserManager)
    {
        this.mapper = mapper;
        this.jSRuntime = jSRuntime;
        this.Db = Db;
        this.fileUpload = fileUpload;
        this.UserManager = UserManager;
    }



    public async Task<List<ApplicationUser>> GetAll()
    {
        try
        {
            var customers = await UserManager.GetUsersInRoleAsync(SD.Role_Customer);
            return customers.ToList();

        }
        catch (Exception e)
        {
            await jSRuntime.ToastrError(e.Message);
            return null;
        }
    }

    public async Task<ApplicationUser> GetUser(string id)
    {
        try
        {
            var user = await UserManager.FindByIdAsync(id);
            if(user != null)
            {
                return user;

            }
            return null;

        }
        catch (Exception e)
        {
            await jSRuntime.ToastrError(e.Message);
            return null;
        }
    }





 public class UnitOfWork : IUnitOfWork
{
    private readonly ApplicationDbContext context;
    private readonly IMapper mapper;
    private readonly IJSRuntime jSRuntime;
    private readonly FileUpload fileUpload;
    private readonly UserManager<ApplicationUser> userManager;
    public ProductRepository ProductRepository { get; private set; }
    public ImageRepository ImageRepository { get; private set; }
    public UserRepository UserRepository { get; set; }
    public CategoryRepository CategoryRepository { get; private set; }

    public UnitOfWork(ApplicationDbContext context, IMapper mapper, IJSRuntime jSRuntime ,FileUpload fileUpload , UserManager<ApplicationUser> userManager)
    {
        this.context = context;
        this.mapper = mapper;
        this.jSRuntime = jSRuntime;
        this.fileUpload = fileUpload;
        this.userManager = userManager;
        ProductRepository = new ProductRepository(mapper,jSRuntime,context,fileUpload);
        ImageRepository = new ImageRepository(context, mapper, jSRuntime);
        CategoryRepository = new CategoryRepository(mapper, jSRuntime, context);
        UserRepository = new UserRepository(mapper, jSRuntime, context, fileUpload, userManager);

    }



    public async Task<int> Complete()
    {
        try
        {
            return await context.SaveChangesAsync();

        }
        catch (Exception e)
        {
            await jSRuntime.ToastrError(e.Message);
            return 0;
        }
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

some pictures

here the picture

adel
  • 125
  • 8
  • You may have to show more code the GetUser method seems ok'ish... the try contents is equivelent to one line of code : `return await UserManager.FindByIdAsync(id);`. This could be because its prerendered. Is jsRuntime available at that time?? – Brian Parker Jun 20 '21 at 10:24
  • @BrianParker i add the full code.. i tried to use only server it didn't work also tried to remove JsRunTime still the problem exist – adel Jun 20 '21 at 10:29
  • This works when the user exists but not if they don't? Tried null checking the Id parameter?... – Brian Parker Jun 20 '21 at 10:36
  • You didn't post the razor (Blazor) code. And that is where the problem is, you are not handling the `null` case correctly. – H H Jun 20 '21 at 10:37

0 Answers0