PDA

View Full Version : NullReferenceException creating container


habuma
05-14-2005, 03:28 PM
I'm trying to recreate the Knight example from my book (http://http://www.amazon.com/exec/obidos/tg/detail/-/1932394354), this time using Spring.NET instead of the Java edition of Spring. But I'm running into some trouble early on.

To setup the problem, you should know that I'm compiling and running this on my MacOS X (Tiger), using Mono 1.1.7.

I have a simple appContext.xml file which looks like this:

<?xml version="1.0" encoding="utf-8" ?>

<objects>
<object name="lancelot" type="Knight, Knight"/>
</objects>


And I have a KnightMain class that looks like this:


using Spring.Context;
using Spring.Context.Support;

public class KnightMain
{
public static void Main()
{
IApplicationContext ctx = new XmlApplicationContext("appContext.xml");

IKnight lancelot = (IKnight) ctx.GetObject("lancelot");
lancelot.embarkOnQuest();
}
}


And I have an IKnight interface and an implementation of IKnight called Knight (but I'll spare you the code...it's rather simple).

I compile all of this stuff into Knight.exe, thus the name of the assembly shold be "Knight", right? (I confirmed this by running monodis.)

It compiles fine...but when I run it, I get the following error:


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
in <0x000a4> System.Xml.XmlInputStream:Initialize (System.IO.Stream stream)
in (wrapper remoting-invoke-with-check) System.Xml.XmlInputStream:Initialize (System.IO.Stream)
in <0x00030> System.Xml.XmlInputStream:.ctor (System.IO.Stream stream)
in (wrapper remoting-invoke-with-check) System.Xml.XmlInputStream:.ctor (System.IO.Stream)
in <0x00044> System.Xml.XmlStreamReader:.ctor (System.IO.Stream input)
in (wrapper remoting-invoke-with-check) System.Xml.XmlStreamReader:.ctor (System.IO.Stream)
in <0x00044> System.Xml.XmlTextReader:.ctor (System.IO.Stream input)
in <0x0002c> System.Xml.Schema.XmlSchema:Read (System.IO.Stream stream, System.Xml.Schema.ValidationEventHandler validationEventHandler)
in <0x00108> Spring.Objects.Factory.Xml.XmlObjectDefinitionRead er:GetXmlReader (System.IO.Stream stream, Boolean validating)
in <0x00234> Spring.Objects.Factory.Xml.XmlObjectDefinitionRead er:LoadObjectDefinitions (IResource resource)
in <0x000c0> Spring.Context.Support.AbstractXmlApplicationConte xt:LoadObjectDefinitions (Spring.Objects.Factory.Xml.XmlObjectDefinitionRea der objectDefinitionReader)
in <0x000b0> Spring.Context.Support.AbstractXmlApplicationConte xt:RefreshObjectFactory ()
in <0x00150> Spring.Context.Support.AbstractApplicationContext: Refresh ()
in <0x0002c> Spring.Context.Support.XmlApplicationContext:.ctor (System.String[] configurationLocations)
in <0x00054> Spring.Context.Support.XmlApplicationContext:.ctor (System.String configurationLocation)
in <0x00038> KnightMain:Main ()


It seems that a NullReferenceException is getting thrown deep down in the bowels of the XML parser. But I can't figure out why. Does anyone have a clue? Am I doing something wrong or is this an issue with Mono?
[/code]

Mark Pollack
05-14-2005, 05:36 PM
Hi!

Not sure if it is mono or some other trival thing. Griffin, one of the developers of Spring.NET, has done development on mac/mono so Spring should be up to the simple task you are giving it on mac/mono.

Make sure that objects.xml is in the same directory as the .exe. Do you have a namespace for the definition of Knight? It should be included in the type defintion if that is the case. Try out the movie finder example and other examples in the distribution and see if they work.

Just for completeness here is something similar I threw together that works on vs.net.


IApplicationContext ctx = new XmlApplicationContext("objects.xml");
IKnight knight = ctx["lancelot"] as IKnight;
knight.EmbarkOnQuest();



namespace PropPlayApp
{
public class Knight : IKnight
{
public object EmbarkOnQuest()
{
Console.WriteLine("Embarking on quest");
return null
}
}
}



<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="lancelot" type="PropPlayApp.Knight, PropPlayApp">
</object>
</objects>


Cheers,
Mark

Congrats on the book BTW! Got a copy right here next to me :)

habuma
05-15-2005, 03:33 AM
Hmmm...except for a few minor details, my example is just about like yours.

Upon further investigation, it seems that the XML parser is having some trouble loading the schema definition. As I understand it (from the Spring.NET code), it loads the schema from the Spring.Core assembly. Running monodis, I can see that it's there in the assembly, so I'm not sure why it's having trouble.

I know that I have the appContext.xml file in the correct place, because if I change the name of it, I get a different error indicating that it can't find the file. And I don't have any of my stuff in namespaces, but I've tried it in a namespace and get the same result.

At this point, I'm thinking that it's either (1) my Mono installation is hosed--but I doubt it, or (2) I'm compiling it wrong--much more likely.

I'm a newbie to .NET and Mono, so I'm still trying to sort out everything. Here's the command line I'm using to compile my code:


% mcs -addmodule:Spring.Core.dll,log4net.dll -out:Knight.exe *.cs


Is this anywhere near correct?

Congrats on the book BTW! Got a copy right here next to me :)

Thanks...it's nice to hear from people who actually have bought it. It's selling really well, but I have no clue who's buying it. So, it's really encouraging to put some names to my readers. Hope you're enjoying it.
[/code]

habuma
05-16-2005, 03:50 AM
I finally got it to work. The trick was in correctly specifying the namespace for the Spring XML as follows:

<?xml version="1.0" encoding="utf-8" ?>

<objects [b]xmlns="http://www.springframework.net"[/b]>
<object name="lancelot" type="Knight, Knight">
<property name="quest">
<ref object="dragonQuest" />
</property>
</object>

<object name="dragonQuest" type="SlayDragonQuest, Knight">
</object>

</objects>


Before, I was specifying xmlns differently or not at all.

Woohoo! Now I'm a Spring.NET programmer!