[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

System.NullReferenceException was unhandled

Posted on 2008-06-10
10
Medium Priority
?
595 Views
Last Modified: 2013-12-17
Hi all,
i'm getting this error and im trying everything to resolve it.  below is the code that is the problem.
when i run it,
this.HeadersNeededSearch(myHeaderData);
pulls back the following error:
"System.NullReferenceException was unhandled"
      - "Object reference not set to an instance of an object."

Basically, what it does, passes a number of variables into a <list>.
any ideas where this is wrong?what am i missing?
public delegate void HeadersNeededSearchHandler(HeaderSearchData e);
public event HeadersNeededSearchHandler HeadersNeededSearch;
        
private void HeaderSearch(ManagementInterfaces.EDIStatusSelection HeaderStatus, DateTime OrderDate, string SupplierID, string PONo)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
 
            HeaderSearchData myHeaderData = new HeaderSearchData();
            HeadersNeededSearchHandler test = HeadersNeededSearch;
 
            myHeaderData.HeaderStatus = HeaderStatus;
            myHeaderData.TargetDate = OrderDate;
            myHeaderData.SupplierID = SupplierID;
            myHeaderData.PONo = PONo;
 
            this.HeadersNeededSearch(myHeaderData);
 
            this.NoOrdersSearchLabel.Visible = !(myHeaderData.HeaderCount > 0);
            this.OrderSearchView.DataSource = myHeaderData.Headers;
 
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

Open in new window

0
Comment
Question by:zell71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 21750646
have you gone into that method to see where the error occurs-- i would suggest breakpoint at the top of the
 this.HeadersNeededSearch(myHeaderData) method.
0
 
LVL 3

Expert Comment

by:NIMTUG_Simon
ID: 21750665
The error is in side the HeadersNeededSearch find this method and add a break line on the first line of code inside the method and then step over(F10), otherwise break no the HeaderNeedSearch(myHeaderData) and step into (F11) rather than step over this line.
0
 

Expert Comment

by:dev_kittu
ID: 21750695
this.headerneededsearch(myheaderdata)
here the object is referring a null object , , , i'm sure i will get an error.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:p_davis
ID: 21750708
asker has that object instantiate before making that call -- as well as setting property values, so it shouldn't be null.
0
 
LVL 4

Accepted Solution

by:
intrwrks earned 2000 total points
ID: 21750718
Not sure what Line # 9 (HeadersNeededSearchHandler test = HeadersNeededSearch;) is accomplishing. I would remove that line9 and then check for null on the event. If you are not subscribed to the event in whatever form you are using this control, then the event call will throw a NullReferenceException. I have attached recommended new code below.

Jason
public delegate void HeadersNeededSearchHandler(HeaderSearchData e);
public event HeadersNeededSearchHandler HeadersNeededSearch;
        
private void HeaderSearch(ManagementInterfaces.EDIStatusSelection HeaderStatus, DateTime OrderDate, string SupplierID, string PONo)
        {
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
 
            HeaderSearchData myHeaderData = new HeaderSearchData();
             
            myHeaderData.HeaderStatus = HeaderStatus;
            myHeaderData.TargetDate = OrderDate;
            myHeaderData.SupplierID = SupplierID;
            myHeaderData.PONo = PONo;
 
            if (this.HeadersNeededSearch != null)
            {
                 this.HeadersNeededSearch(myHeaderData);
            }
 
            this.NoOrdersSearchLabel.Visible = !(myHeaderData.HeaderCount > 0);
            this.OrderSearchView.DataSource = myHeaderData.Headers;
 
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }
 
Open in New Window 

Open in new window

0
 
LVL 6

Author Comment

by:zell71
ID: 21750898
ok intrwrks, firstly HeadersNeededSearchHandler test = HeadersNeededSearch; was used as testing and not removed for purpose for you guys to see..so that can be ignored.
using your method, it works and passes over this.HeadersNeededSearch(myHeaderData);
however doesnt return expected data, any other suggestions out there please feel free to post.  im just testing over code
0
 
LVL 4

Expert Comment

by:intrwrks
ID: 21751300
What do you mean it doesn't return the expected data? Do you mean the event is not firing?

If the HeadersNeededSearch is null, then that means the form or class using the control has not subscribed to the event properly.

Are you subscribing to the event somewhere in your code? Example in code window.

void someFunction()
{
     YourCustomControl.HeadersNeededSearch += new HeadersNeededSearchHandler(YourCustomControl_HeadersNeededSearch);
}
 
protected void YourCustomControl_HeadersNeededSearch(object sender, HeaderSearchData e)
{
      // process data here
}

Open in new window

0
 
LVL 6

Author Comment

by:zell71
ID: 21751667
abit of background..it was a project left last year, so i've been asked to pick it up and finish it.
I think your right..its not hitting this..which is surely the event.
so
YourCustomControl.HeadersNeededSearch += new HeadersNeededSearchHandler(YourCustomControl_HeadersNeededSearch); is the subscription code?

class clsServerConnection
    {
        public clsServerConnection(EDIManagementConsole FormToConnect)
        {
        FormToConnect.HeadersNeededSearch += new EDIManagementConsole.HeadersNeededSearchHandler(HandleHeadersNeededSearch);
        }
...

Open in new window

0
 
LVL 4

Expert Comment

by:intrwrks
ID: 21751723
That code should subscribe to the event as long as the value passed into the Constructor is a reference to the proper control. You should then have a function in that class named HandleHeadersNeededSearch that would actually handle the event and receive the appropriate data.

Jason
protected void HandleHeadersNeededSearch(object sender, EDIManagementConsole.HeaderSearchData e)
{
      // handle data.
      // e should contain the values from the event.
}

Open in new window

0
 
LVL 6

Author Closing Comment

by:zell71
ID: 31465708
Jason, cheers mate, a little bit of backwards engineering sorted it.
Basically, gonna have to scrap the events and start it in my way.
i have it working now so if i want to call from events i can do it once i got more time.
A rating because you replied fast and VERY helpful.  your a star *
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question