Results 1 to 3 of 3

Thread: [RESOLVED] Best to way to trap NullException

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] Best to way to trap NullException

    Hello. Anybody can suggest whats the best way to trap the null exeption from this code of mine.

    vb Code:
    1. long last_Sent;
    2.         long last_Rec;
    3.  
    4.         private void timerPackets_Tick(object sender, EventArgs e)
    5.         {
    6.             try
    7.             {
    8.                 string strSent = null;
    9.                 string strReceived = null;
    10.                 long strStatdon;
    11.                 long strStatup;
    12.                 long curr_Sent = 0;
    13.                 long curr_Rec = 0;
    14.                 long speedDown = 0;
    15.                 long speedUp = 0;
    16.  
    17.                
    18.                 strSent = clsMethods.strLines(5);
    19.                 strReceived = clsMethods.strLines(4);
    20.                 string[] arrSent = strSent.Split(',');
    21.                 string[] arrRec = strReceived.Split(',');
    22.                 curr_Sent = long.Parse(arrSent[1]);
    23.                 curr_Rec = long.Parse(arrRec[1]);
    24.                 speedDown = curr_Rec - last_Rec;
    25.                 speedUp = curr_Sent - last_Sent;
    26.                 strStatdon = speedDown * 8;
    27.                 strStatup = speedUp * 8;
    28.                 StatusDown.Text = clsMethods.convertSize(strStatdon) + " /s";
    29.                 StatusUP.Text = clsMethods.convertSize(strStatup) + " /s";
    30.                 lblUP.Text = clsMethods.convertSize(last_Sent);                
    31.                 lblDown.Text = clsMethods.convertSize(last_Rec);
    32.                 last_Rec = long.Parse(arrRec[1]);
    33.                 last_Sent = long.Parse(arrSent[1]);
    34.             }
    35.             catch (Exception ex)
    36.             {
    37.                 MessageBox.Show(ex.ToString(),"Information");
    38.             }
    For I just popup the error being generated.
    On my code sometimes this two line
    strSent = clsMethods.strLines(5);
    strReceived = clsMethods.strLines(4);

    doesn't return a value.
    that's why this line
    string[] arrSent = strSent.Split(',');
    string[] arrRec = strReceived.Split(',');
    throw me an exeption.
    I want when nullreference error occured just to continue the timer tick until that two line have value in it..

    THanks.
    br.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Best to way to trap NullException

    be proactive not reactive... check for the null values first... if the string isn't null, then continue with your code... if it is null, then skip it...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Best to way to trap NullException

    It is advised to check the array length before getting the value from the array. If you still need to pass the null reference exception then go like this


    vb Code:
    1. try
    2.             {
    3.             }
    4.             catch (NullReferenceException ne)
    5.             {
    6.                 //Log the error and continue
    7.             }
    8.             catch (Exception ex)
    9.             {
    10.                 MessageBox.Show(ex.Message);
    11.  
    12.             }
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width