C# Application: NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michelle Monty
    New Member
    • Oct 2007
    • 24

    #1

    C# Application: NullReferenceException

    I've created a program that will allow a user to choose a shoe style from a listbox and the program will return a price in a textbox. I have the code finished but I'm getting an error when I try to run the program. Can anyone give me an idea of how to fix it? The code is below. I've bolded the line where I'm getting the error. The error is "NullReferenceE xception was unhandled"

    [CODE=vbnet]Option Explicit On
    Option Strict On

    Public Class mainform

    Structure shoes
    Public price As Decimal
    Public stylenumber As String
    End Structure
    Private items(9) As shoes
    Private Sub mainform_load(B yVal sender As Object, ByVal e As System.EventArg s) Handles Me.Load

    Dim path As String = "C:\Course Technology\8362 3-0\VbReloaded\Ch ap09\Shoe Circus Solution\Shoe Circus Project\"
    Dim text As String
    Dim newlineindex As Integer
    Dim recordindex As Integer
    Dim record As String
    Dim commaindex As Integer

    If My.Computer.Fil eSystem.FileExi sts(path & "shoesinfo.txt" ) Then text = My.Computer.Fil eSystem.ReadAll Text(path & "shoesinfo.text ")
    For subscript As Integer = 0 To 9

    'Error Occours here
    newlineindex = text.IndexOf(Co ntrolChars.NewL ine, recordindex)

    record = text.Substring( recordindex, newlineindex - recordindex)
    commaindex = record.IndexOf( ",", 0)
    items(subscript ).stylenumber = record.Substrin g(0, commaindex)
    items(subscript ).price = Convert.ToDecim al(record.Subst ring(commaindex + 1))
    stylelistbox.It ems.Add(items(s ubscript).style number)
    recordindex = newlineindex + 2

    Next subscript


    stylelistbox.Se lectedIndex = 0
    End Sub

    Private Sub stylelistbox_se lectedindexchan ged(ByVal sender As Object, ByVal e As System.EventArg s) Handles stylelistbox.Se lectedIndexChan ged
    pricetextbox.Te xt = items(stylelist box.SelectedInd ex).price.ToStr ing("N2")

    End Sub
    End Class[/CODE]
    Last edited by Shashi Sadasivan; Nov 21 '07, 03:31 AM. Reason: placing vbnet code tags, adding comment where error occoured
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Most probably the string "text" is being set to null when you read the file.
    Does the text file have any data in it?

    Comment

    • Michelle Monty
      New Member
      • Oct 2007
      • 24

      #3
      Thanks for your help. While double checking my file, I noticed that I had typed shoes instead of shoe so that was the problem. Duh. It's working now! Thanks again.


      Originally posted by Shashi Sadasivan
      Most probably the string "text" is being set to null when you read the file.
      Does the text file have any data in it?

      Comment

      Working...