0

I have a movie database where I'm trying to save a genre. I have the same project in winforms and there the code works, so it's probably something simple that I missed.

Anyways, here is the code:

    MovieCollectionEntities db = new MovieCollectionEntities();
    Genre g = new Genre();
    g.GenreName = TextBoxGenresAdd.Text;
    db.Genres.Add(g);
    db.SaveChanges();

The error message I get is:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.

edit:
Worth mentioning is probably that I'm using master paging and the code is from the AddGenre.aspx.

edit2: Complete answer can be found @ NullReferenceException in DbContext.saveChanges()

Community
  • 1
  • 1
grimsan55
  • 265
  • 3
  • 4
  • 20
  • On which line are you getting this 'NullReferenceException'? – AbhinavRanjan Dec 18 '13 at 14:26
  • Oh sorry, i get it on db.SaveChanges(); – grimsan55 Dec 18 '13 at 14:27
  • Is there a DBSet with this name and corresponding table exists in DB? – Nexus23 Dec 18 '13 at 14:29
  • @Nexus23, there is. I have the exact same code section in my winforms project and that works perfectly. IT uses same EF and database. – grimsan55 Dec 18 '13 at 14:30
  • Have a look at this. http://stackoverflow.com/questions/17136455/nullreferenceexception-in-dbcontext-savechanges May be your scenario is same. – AbhinavRanjan Dec 18 '13 at 14:31
  • @Abhinav , that was correct! – grimsan55 Dec 18 '13 at 14:45
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 18 '13 at 14:48
  • @JohnSaunders I don't think the link you have provided answers this question correctly. This is happening because there is another class with the same name as the entity. The link provided by grimsan55 in edit2 is probably a better link to use when this question is marked as a duplicate which it really is. – AbhinavRanjan Dec 18 '13 at 18:12
  • @Abhinav: you should consider adding that as one of the answers to the question I linked. I don't think I've heard of that one. – John Saunders Dec 18 '13 at 18:52

3 Answers3

1

@Abhinav linked to the correct page. The problem was that the aspx pages had the same names as the entity names which made the program crash.

grimsan55
  • 265
  • 3
  • 4
  • 20
0

grimsan55,

This error usually occurs when the data you are trying to insert into the database doesn't line up right with the fields in the database. For instance, if in your movie database you have fields for title, rating, and producer and each of these are NOT NULL. Then you go to insert the data, but you only have title and rating, this error will be thrown.

The best bet is to go back to your database and make sure that everything is setup correctly, then go to your Model in the project and update your model. You could have changed something in the database but forgot to update your model in the project.

Hopefully that helps. I come into this issue sometimes when I updated the database, forgot what I changed, and then try to save data like it was how it was before.

AVenger
  • 106
  • 4
-1

Consider, from this snippet, that db.Genres.Add(g); could be trying to add g to a non-existent list of Genres. Try making sure Genres is something before calling the Add method.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • Genres is a list of genres, if i use a listbox i can clearly see the current genres already in the list. for example if i use listbox1.datasource = db.Genres.ToArray(); listbox1.databind(); i see all the genres – grimsan55 Dec 18 '13 at 14:22