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