Results 1 to 2 of 2

Thread: Null Reference Exception

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    1

    Question Null Reference Exception

    Hi all. I am new here. This site looks great. I am just starting Visual Basic.net and I am having a problem with a null reference exception. Here is my code, can someone tell me if they see what is wrong here.

    First of all-- here is the error i am getting when i build my program....
    *******An unhandled exception of type 'System.NullReferenceException' occurred in SportsStatistics.exe*****
    Additional information: Object reference not set to an instance of an object.

    *****************************
    My code:

    Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Declaration of Total Number of Games Constant
    Const intTotalOfGames As Integer = 40

    'Declare variables for team one
    Dim intWins1 As Integer = Val(TextBox6.Text)
    Dim intLosses1 As Integer = Val(TextBox11.Text)
    Dim decWinPercent1 As Decimal = Val(TextBox16.Text)

    'Declare variables for team two
    Dim intWins2 As Integer = Val(TextBox7.Text)
    Dim intLosses2 As Integer = Val(TextBox12.Text)
    Dim decWinPercent2 As Decimal = Val(TextBox17.Text)

    'Declare variables for team three
    Dim intWins3 As Integer = Val(TextBox8.Text)
    Dim intLosses3 As Integer = Val(TextBox13.Text)
    Dim decWinPercent3 As Decimal = Val(TextBox18.Text)

    'Declare variables for team four
    Dim intWins4 As Integer = Val(TextBox9.Text)
    Dim intLosses4 As Integer = Val(TextBox14.Text)
    Dim decWinPercent4 As Decimal = Val(TextBox19.Text)

    'Declare variables for team five
    Dim intWins5 As Integer = Val(TextBox10.Text)
    Dim intLosses5 As Integer = Val(TextBox15.Text)
    Dim decWinPercent5 As Decimal = Val(TextBox20.Text)

    'Define a variable for the magic numbers and games back boxes.
    Dim intMagicNumber As Integer = Val(TextBox21.Text)
    Dim intGamesBack2 As Integer = Val(TextBox22.Text)
    Dim intGamesBack3 As Integer = Val(TextBox23.Text)
    Dim intGamesBack4 As Integer = Val(TextBox24.Text)
    Dim intGamesBack5 As Integer = Val(TextBox25.Text)

    ' Calculating winning percentage

    decWinPercent1 = intWins1 / (intWins1 + intLosses1)
    decWinPercent2 = intWins2 / (intWins2 + intLosses2)
    decWinPercent3 = intWins3 / (intWins3 + intLosses3)
    decWinPercent4 = intWins4 / (intWins4 + intLosses4)
    decWinPercent5 = intWins5 / (intWins5 + intLosses5)

    'Insert games back & magic number text
    TextBox26.Text = "Magic Number"
    TextBox27.Text = "Games Back"
    TextBox28.Text = "Games Back"
    TextBox29.Text = "Games Back"
    TextBox30.Text = "Games Back"


    'Calculating Magic Number (team 1)
    intMagicNumber = intTotalOfGames - (intWins1 + intLosses1) - intGamesBack2
    ' Calculating games back for team 2
    intGamesBack2 = ((intWins1 - intWins2) * 0.5) + ((intLosses2 - intLosses1) * 0.5)
    ' Calculating games back for team 3
    intGamesBack3 = ((intWins1 - intWins3) * 0.5) + ((intLosses3 - intLosses1) * 0.5)
    ' Calculating games back for team 4
    intGamesBack4 = ((intWins1 - intWins4) * 0.5) + ((intLosses4 - intLosses1) * 0.5)
    'Calculating games back for team 5
    intGamesBack5 = ((intWins1 - intWins5) * 0.5) + ((intLosses5 - intLosses1) * 0.5)
    End Sub
    End Sub
    End Class

    ********************************EOF


    Thank you for any help you can provide...

    //mystic

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi & welcome.

    A couple of obvious things here.


    Firstly, your post ends with 2 End Subs, but there is no beginning line for any sub.

    Secondly, you have not declared any of the following;

    decWinPercent1
    decWinPercent2
    decWinPercent3
    decWinPercent4
    decWinPercent5

    In .NET you have to specifically declare all variables.

    If that does not sort it out, find out which line is giving the problem.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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