That sounds all fun and good...but when I try passing an iNews item to the AddNews() method of the NewsLogic class, I keep getting the following error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:
Line 17: inews.author="Me, myself and I"
Line 18: inews.source="EQ2Nexus.com"
Line 19: inewsLogic.addnews(inews)
Line 20: end sub
Line 21: </script
With the following Stack Trace:
Stack Trace:[NullReferenceException: Object reference not set to an instance of an object.]
EQ2NexusLogic.newsLogic.AddNews(News p_news)
ASP.addnews_aspx.page_load(Object sender, EventArgs e) in E:\web\uoxcenterco\htdocs\admin\addnews.aspx:19
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
The error occurs on line 19, where I call the AddNews method with the iNews variable as parameter. What I dont understand is how can the iNews be not set to an instance of an object when on the preceding line (18) I use it?
Here is the code for the AddNews method:
Public Sub AddNews(ByVal p_news As News)Dim newsDB As New newsDB
Dim newsDataSet As New DataSet
...
End sub
Its probably a simple thing having to do with the way I pass the object paramter or how I call the method, but since I am new to VB and .NET could someone point me in the right direction?
BTW I also tried declaring the parameter as ByRef in AddNews() and it still threw same error.
Any help would be greatly appreciated, thx!
Ducky>What I dont understand is how can the iNews be not set to an instance of
> an object when on the preceding line (18) I use it?
In all likelihood, it can't. I think it's saying that inewsLogic is not set to an instance of an object.
Hmmm I am pretty sure it is initialized, but maybe I dont grasp yet the idea of passing parameters by value or reference...
Here is the actual whole code for the page_load where the error occurs:
sub page_load(sender as Object, e as EventArgs)
dim inewsLogic as New NewsLogic
dim inews as New News
inews.title="Test News"
inews.description="Testing my class AddNews() method"
inews.text="Lets see if this works on first try...yeah right!"
inews.newsdate=DateTime.Now
inews.author="Me, myself and I"
inews.source="EQ2Nexus.com"
inewsLogic.addnews(inews)
end sub
As you can see, iNews logic is declared with the New keyword...shouldnt this be creating an instance of the iNewsLogic object? or do I need to do anything else to initilaize the instance of an object for that class?
Also, can someone confirm that the way I am actually declare the parameters to pass to the method is correct? I am such a .NET n00b ;)
Public Sub AddNews(ByVal p_news As News)
...
End sub
Thanks in advance
Ducky
Duck,
Try one small difference: add parentheses to the object you're instantiating, i.e.
dim inewsLogic as New NewsLogic()
This may seem small, but it explicitly calls the constructor function (assuming this object has one, which is why you'd need to use the "New" keyword in the first place).
Cheers,
Upon further reflection, are you compiling in debug mode? It may be that your error is actually being thrown inside the method somewhere.
Also, I don't know why you're specifying ByVal, but I'd be inclined to leave it as ByRef (just because that's a little more "normal", not for any concrete technical reason).
Thanks a lot, will try that ;)
The ByVal comes by default in Visual Basic somehow hehe. Will change it to ByRef wich seemed more "normal" to me anyway.
I dont think its the method causing the error as I tested with a Return statement as its first line. I didnt know the error could be coming from further down, so thanks for the tip ;)
I will try your parenthesis suggestion and see if that helps. There is a new declaration in my class of course.
Lets see...
Wow ;)
Hard to believe the powers invested in a simple Parenthesis...I think I will have to start a new cult, Parenthesis Worshippers or somehting...theyre so powerful...
Thanks a lot, it worked out.
Ducky
0 comments:
Post a Comment