These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Object reference not set to an instance of an object.
PostPosted: Mon Jul 03, 2006 4:54 pm 
Newbie

Joined: Mon Jul 03, 2006 4:49 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: LAST

Mapping documents:

using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Entidade
{
public class Turma
{
private Int64 _id;
private string _nome;
private short _particular;
private IList<Aluno> _alunos;

public Turma()
{
}

public virtual Int64 id
{
get { return _id; }
set { _id = value; }
}

public virtual string nome
{
get { return _nome; }
set { _nome = value; }
}

public virtual short particular
{
get { return _particular; }
set { _particular = value; }
}

public virtual IList<Aluno> alunos
{
get { return _alunos; }
set { _alunos = value; }
}
}
}

Code between sessionFactory.openSession() and session.close():

ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();

Aluno aluno = (Aluno) session.Load(typeof(Aluno), Convert.ToInt64(5));

Turma turma = new Turma();
turma.nome = "Teste";
turma.particular = 0;
turma.alunos.Add(aluno);

session.Save(turma);
session.Close();

Full stack trace of any exception that occurs:

[NullReferenceException: Object reference not set to an instance of an object.]
_Default.Page_Load(Object sender, EventArgs e) in c:\Projetos\EnglishService\Default.aspx.cs:35
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
System.Web.UI.Control.OnLoad(EventArgs e) +88
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035

Name and version of the database you are using: SQL Server 2000

[b]The generated SQL (show_sql=true):[ Yes/b]

When i try to acess any method or variable in turma.aluno, the debug give me this error.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 7:49 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
It looks like the collection "Aluno" hasn't been instantiated.

Either directly after the declaration, or in the constructor, you need to instantiate your collection Aluno like this:

IList<Aluno> _alunos = new List<Aluno>();

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 8:06 pm 
Newbie

Joined: Mon Jul 03, 2006 4:49 pm
Posts: 5
I tryed this, but when i initialize aluno in the constructor, the system give me a error during compilation. IList is abstract class and cannot be initializate ...

Sorry for the poor english, i hope you understand !

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 8:09 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
These are not NHibernate questions, so you may need to read a book on general C# development.

What is happening is that IList is a list interface. You can never instantiate an interface, because by-design they have no implementation.

IList provides a contract for collections that wish to be treated as lists. List<T> implements IList, ArrayList implements IList, etc.

You need to pick a concrete list to instantiate. If you see my example above, I said _alunos = new List<Aluno>(); (notice no 'I' - I'm using the concrete list class here not the IList interface).

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 8:36 pm 
Newbie

Joined: Mon Jul 03, 2006 4:49 pm
Posts: 5
Hmmmm, sorry to my fault ! Very thanks !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.