c# - Null reference thrown when object is not null mvc 6 -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i'm working on asp.net 5 reader application needs display files stored in memory, stored list of paths each chapter. pass model containing chapter name , list of paths. in view acces using this:
@foreach (var item in model.paths) { string path = item.p; var array = path.split('\\'); var filename = array[array.length - 1]; // code removed brevity }
when test "system.nullreferenceexception object reference not set instance of object." on last code line shown, if variables not null
update had forgot link whole project here
update 2 i've tried suggested me (thank lot support, guys great) but, unfortunately error keeps occuring whenever try access filename.
i've tried firstly see if model.paths null, making echo path, , went along nicely, use system.io.path.getfilename method threw same error, reason seems every attempt access filename rejected
update 3
i've moved logic in controller, code in foreach says:
@foreach (var item in model.paths) { <img src="~/images/@item.p" alt="alternate text" /> }
but still throws same error
final update i'm sorry have taken time, found error in different part of view, error-reporter reported wrong line
thank anyway, loved support!!!
there potential nullreferenceexception
on 2 of lines:
here:
@foreach (var item in model.paths)
...if model
null
here:
string path = item.p;
...if item
null
so based on limited info given 1 of 2 need fix.
Comments
Post a Comment