✅ asp.net handling image uploads.
error: NullReferenceException: Object reference not set to an instance of an object.
controller:
here is the error
here is after the error
here is the viewModel's image properties
the actual model
28 Replies
here is the function being called
System.NullReferenceException: 'Object reference not set to an instance of an object.'
it looks like _photoService is null
yeah it can only be the
_photoService
being null, do u register the service correctly?this is something im new to.
u also need to register the service:
(https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-7.0)
also u can use $code to provide code itself
Dependency injection in ASP.NET Core
Learn how ASP.NET Core implements dependency injection and how to use it.
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/aha!
tyvm let me add that in.
also, has the controller an appropriate constructor for the services?
i only have this. should i add it inside the controller constructor?
that would only inject the
IClubRepository
dependency, u also need to add the photo service as parameter and assign itis this new to .7? i wonder why the tut that I'm watching didn't do this
tyvm
that missing parameter is also the reason why it didnt complain at instantiation of the controller that its missing a required dependency
i may have missed it by mistake. ive been learning for three years now and switched to asp.net. i wish i started here first
there are two ways for dependency injection, via constructor or via properties. tho i only have experience with the constructor variant. (feels also cleaner to me than the properties variant)
properties as in this?
these are fields not properties. but that + the shown constructor should work, if u have registered the service as mentioned earlier
oooohhhhh i got a different error! we're in business.
thats a topic i've yet to discover
note that there are 3 different methods on how u can register a service
AddSingleton<TInterface, TImplementation>()
-> one instance for all which require the TInterface
AddTransient<TInterface, TImplementation>()
-> everthing gets its own instance
AddScoped<TInterface, TImplementation>()
-> one instance per scope (asp creates a scope for each incoming request)
with properties u can add functionality to accessing the data:
of course there is a lot more to properties, but explaining that all here would take too long ;p
cant help ya if u dont give information about it, but ofc its better to try to fix it urself ;pokay thank you very much. i greatly appreciate this.
yes once i got a different error, i waws successful in fixing it.
it works now
you're the best! ❤️
glad i could help o7
also its worth reading the whole article, its a great resource on how DI works
i will read it.
if ur questions are answered please use /close to mark the thread as answered
Use the
/close
command to mark a forum thread as answeredty again