Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi my problem are the Object reference not set to an instance of an object
code is
public class ProfileController:BaseController
    {
        private readonly IProfileRepository _profileRepository;
        private readonly IUnitOfWorkFactory _unitOfWorkFactory;

        public ProfileController(IProfileRepository profileRepository, IUnitOfWorkFactory unitOfWorkFactory)
        {
            _profileRepository = profileRepository;
            _unitOfWorkFactory = unitOfWorkFactory;
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult index(CreateAndEditProfile createAndEditProfile)
        {
                //if (ModelState.IsValid)
                //{
                    try
                    {
                            using (_unitOfWorkFactory.Create())
                            {
                                Profiles profile = new Profiles();
                                int studentID = 2;
                                profile.StudentId = studentID;
                                Mapper.Map(createAndEditProfile, profile);
                                _profileRepository.Add(profile);
                                return RedirectToAction("index");
                            }
                    }
                    catch (ModelValidationException mvex)
                    {
                        foreach (var error in mvex.ValidationErrors)
                        {
                            ModelState.AddModelError(error.MemberNames.FirstOrDefault() ?? "", error.ErrorMessage);
                        }
                    }
                //}
            return View();
        }

        public ActionResult index(string id)
        {
            return View();
        }
    }

someone can help me out this problem

The error in line --
public ActionResult index(CreateAndEditProfile createAndEditProfile)
Posted
Updated 5-Jun-15 1:50am
v2
Comments
[no name] 5-Jun-15 8:05am    
Put a break point then check it...:)
C. Nakul 5-Jun-15 8:06am    
i will try to set breakpoints but the error are come again
F-ES Sitecore 5-Jun-15 8:06am    
It's unlikely anyone can help with this sort of thing, you'll need to debug the code and find out what line the error is happening (it's not happening on the line you posted), then work out what is null, then trace your code back to see why that something is null when you expect it to have a value. It could be an issue with your DI system, or something else entirely.

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn

Run the debugger and set breakpoints at the desired locations?
Cheers
Andi
 
Share this answer
 
Comments
C. Nakul 5-Jun-15 8:05am    
i will try to set breakpoints but the error are come again
Andreas Gieriet 5-Jun-15 8:07am    
Of course! The debugger does not solve the problem - it allows to step through and identify which data is not properly initialized.
Or am I misreading your reply?
Andi
Kenneth Haugland 5-Jun-15 8:30am    
Jupp, It's a lazy bugger :laugh:
Kenneth Haugland 5-Jun-15 8:29am    
Error usually comes up when you are missing a new statement on an object somewhere.
[no name] 5-Jun-15 8:34am    
Of course it does. The breakpoint must be set to a line before the error actually occurs. Since you already got the message out of the exception, you may also take a look at the stack trace, which is also a property of the exception. The stack trace shows the call hierarchy down to the line where the exception occured. There you can get the name of the method and the line number in the code without debugging. Look at that line, place the breakpoint for debugging there. When executed, the debugger will stop right there and you can examine all variables in that line and look for null values.
You did not really point out the line of code there the exception is used. The method declaration is not a point in the runtime code, not the instruction where the instruction pointer could point to.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
Comments
Andreas Gieriet 6-Jun-15 19:25pm    
Hello Sergey,
My 5 for the detailed answer!
For the second part I'd add a note that the Visual Studio build configurations Debug and Release have mainly one crucial difference: the first defines the symbol DEBUG, the second doesn't. As a consequence, the project compiled under one or the other configuration may result in differing control flow for the very same input data.
It is a common misconception that Release configuration does not allow to have debug information generated (see project settings on the Debug tab). Of course, Release usually includes also additionally some optimization settings (which are little in C#, though).
Cheers
Andi
Sergey Alexandrovich Kryukov 6-Jun-15 22:35pm    
Thank you, Andi.

There is also TRACE and or the other symbols, the compilation option may differ... It depends on project type. And another big misconception is: lack of understanding that "Debug" is nothing but the name of some Configuration, and that configuration is nothing but some default of some project template. You can create more configurations for a project, and a different project template with no Debug configuration or with 10 different configurations. Many who don't understand it write "I did it under Debug", not realizing that, strictly speaking, "under Debug" does not mean anything certain. It cannot even guarantee that debugging is possible at all... :-)

—SA
Andreas Gieriet 7-Jun-15 15:15pm    
I fully agree!
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900