bpayne111 Posted May 10, 2004 Posted May 10, 2004 i'm using divils article to teach me how to do plug ins.... I've gotten stuck... This is my main file where i'd like to use the plug... the rest of the files mimic divil's *note i removed his form and included that in the 'main' file. i hope this code is enough public class jrnMain { public static void Main() { jrn.IjrnHost objHost = new jrn.Host(); int index; //get a list of plugins jrn.PlugServices.availablePlug[] plugins = jrn.PlugServices.findPlug(Path.GetDirectoryName(Application.ExecutablePath), "jrn.IjrnPlug"); //loop through available plugins, creating instances and adding them to listbox for(index = 0; index <= plugins.Length - 1;index++) { jrn.IjrnPlug objPlug; objPlug = (jrn.IjrnPlug)PlugServices.CreateInstance(plugins[index]); objPlug.Initialize(objHost); Console.WriteLine(((jrn.IjrnPlug)objPlug).Name()); }//for } }//class I get the following error "object reference not set to an instance of an object" on the line Console.WriteLine( .....Name); I understand what that error means but i don't get how i'm supposed to create an instance. In Divil's vb version he does a DirectCast. Which ithought my line of code was the = of. I'm just confused. I had all this working at one point now it's seems shot. Any ideas? thanks brandon Quote i'm not lazy i'm just resting before i get tired.
Arch4ngel Posted May 10, 2004 Posted May 10, 2004 Maybe that means that your application isn't a Console Application ? When I use Console.WriteLine(...) with a Form based application... I got the same error as you. However... I didn't followed the divil's course so... I might be wrong. I think it don't work because of this line... but ... it's just my opinion. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Administrators PlausiblyDamp Posted May 10, 2004 Administrators Posted May 10, 2004 Does your object (objPlug) implement the IjrnPlug interface? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bpayne111 Posted May 10, 2004 Author Posted May 10, 2004 yes they implement the interface. Correction to my ealier post it is the obj.Initialize line that throws the exception Should i attach the whole project? (it's not very big). I have a feeling it's somethign stupid but i'm just stuck and i think my eyes are going to fall out. thanks, brandon Quote i'm not lazy i'm just resting before i get tired.
Arch4ngel Posted May 10, 2004 Posted May 10, 2004 Give us the line in the Initialize where it crash and some related code. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Administrators PlausiblyDamp Posted May 10, 2004 Administrators Posted May 10, 2004 May as well attach the project if it's not too big, just remove the binaries (executables etc) first ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bpayne111 Posted May 10, 2004 Author Posted May 10, 2004 Here are the files... Interfaces is compiled as it's own dll MTUer is compiled as it's own dll PluginServices, Main and Host are part of a console application. All files are compiled in the same dirHost.zip Quote i'm not lazy i'm just resting before i get tired.
Administrators PlausiblyDamp Posted May 11, 2004 Administrators Posted May 11, 2004 (edited) firstly change the line Console.WriteLine(((jrn.IjrnPlug)objPlug).Name()); to Console.WriteLine(((jrn.IjrnPlug)objPlug).Name); Also change your implementation of ExamineAssembly to public static void ExamineAssembly(Assembly dll, string thisInterface, ArrayList plugs) { Type objInterface=null; availablePlug plug; //loop through each type in dll foreach(Type objType in dll.GetTypes()) { //only check public types if(objType.IsPublic) { //ignore base classes and interfaces if(!(objType.IsAbstract)) { objInterface = objType.GetInterface(thisInterface,true); //see if this interface implements our interface if (thisInterface != null) { plug = new availablePlug(); plug.assemblyPath = dll.Location; plug.className = objType.FullName; plugs.Add(plug); }//if }//if }//if }//foreach }//void you had a misplaced opening brace after the check for abstract type. Edited May 11, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bpayne111 Posted May 12, 2004 Author Posted May 12, 2004 dude you are the man... thanksf or all the time you put into looking at that... i haven't tested it yet but it think it will work now. throughout changes i missed some little things that were tough to see About removing the parens from the Name property.... It's telling me i need them¿?¿ There are some other bugs in other projects in the solution.... could those keep it from checking sytax of this project correctly? The interface is declared properly... i'm gonna clean up these other bugs and see if that helps. THANKS AGAIN, udaman later brandon I'll Quote i'm not lazy i'm just resting before i get tired.
bpayne111 Posted May 12, 2004 Author Posted May 12, 2004 (edited) uggg Object Reference not set to an instance of an object? objPlug.Initialize(objHost); // i changed that code to this if(objPlug != null) { objPlug.Initialize(objHost); Console.WriteLine(((jrn.IjrnPlug)objPlug).Name()); } Now i get the error Specified cast is not valid after skipping a few null values. Could the PlugServices.CreateInstance method be screwy??? (see above attachment for the file) here is the code minus the added code above public class jrnMain { [sTAThread] public static void Main() { jrn.IjrnHost objHost = new jrn.Host(); int index; //get a list of plugins jrn.PlugServices.availablePlug[] plugins = jrn.PlugServices.findPlug(Path.GetDirectoryName(Application.ExecutablePath), "jrn.IjrnPlug"); //loop through available plugins, creating instances and adding them to listbox for(index = 0; index <= plugins.Length - 1;index++) { jrn.IjrnPlug objPlug = null; objPlug = (jrn.IjrnPlug)PlugServices.CreateInstance(plugins[index]); objPlug.Initialize(objHost); Console.WriteLine(((jrn.IjrnPlug)objPlug).Name()); }//for } }//class i could of sworn i had a firmer grasp of programming than this....what's the deal? ugggggg brandon Edited May 12, 2004 by bpayne111 Quote i'm not lazy i'm just resting before i get tired.
Administrators PlausiblyDamp Posted May 12, 2004 Administrators Posted May 12, 2004 Not sure what the problem could be, the version I was hacking around with here works fine - running it just displays MTUer to the console. I've attached my working copy - may be worth doing a diff with your sources to see if anything else has changed.Plugins.zip Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bpayne111 Posted May 12, 2004 Author Posted May 12, 2004 I solved my problem. Thanks for all of your help. You are the man Quote i'm not lazy i'm just resting before i get tired.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.