Need help with grocery app project

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2993 Views - Last Post: 14 July 2016 - 04:17 PM Rate Topic: -----

#1 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Need help with grocery app project

Posted 14 July 2016 - 11:00 AM

I have a grocery app project due for school and have everything working, accept for adding items to my grocery basket. When i run my project in visual basic 2012, it works fine until i try to add an item to my grocery basket. I get an error that says: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.VisualBasic.dll and then says: Additional information: object variable or with block variable not set. I will attach my project in a zip file titled groceryapp. The error highlights this code in the Public Class GroceryItemForm, under Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, ToolStripMenuItem.Click:

(Highlighted code)

Dim item As New GroceryItem(txtScanNumber.text, txtBrandName.text, numPrice.Value, txtDescription.Text, [Enum].Parse(GetType(Aisle), cboAisle.Text))
basket.Add(item)
clearform()
End Sub



I'm new to this forum and have spent days trying to figure this out. The online school i'm enrolled in doesn't get back to you for days. Supposedly, it has to do with the object variables, but i'm still having trouble, even thought i checked those. Please help...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with grocery app project

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: Need help with grocery app project

Posted 14 July 2016 - 11:13 AM

It means you are trying to use an object that hasn't been instantiated.

I would suggest putting a break point on that line 2.. then mouse over and see if 'basket' has been instantiated (aka - 'new' used).
Was This Post Helpful? 1
  • +
  • -

#3 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 11:18 AM

Here is the full GroceryItemForm code and GroceryItem code:
Imports System.IO
Public Class GroceryItemForm

    Dim txtDescription As Object
    Dim OpenDialog As Object
    Dim SaveDialog As Object



    Private Sub GroceryItemForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim GroceryItemForm As New LoginForm
        GroceryItemForm.ShowDialog()
    End Sub

    Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, ToolStripMenuItem1.Click
        txtScanNumber.Text = txtBrandName.Text.Substring(0, 3) & "1019"

        Dim item As New GroceryItem(txtScanNumber.Text, txtBrandName.Text, numPrice.Value, txtDescription.Text,
                                        [Enum].Parse(GetType(Aisle), cboAisle.Text))
        basket.Add(item)
        clearForm()
    End Sub

    Public Sub clearForm()
        txtScanNumber.Text = ""
        txtBrandName.Text = ""
        numPrice.Value = 0.0
        txtDescription.Text = ""
        cboAisle.SelectedIndex = -1

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Application.Exit()
    End Sub

    Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem5.Click
        'For Each item As GroceryItem In Basket
        'item.Print()
        'See GroceryItem.vb for Print() code
        'Next
        Dim bdf As New BasketDisplayForm
        bdf.ShowDialog()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem6.Click
        If SaveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim fileName As String = SaveDialog.FileName
            Dim sFile As New FileStream(fileName, FileMode.Create)
            Dim wFile As New StreamWriter(sFile)
            For Each item As GroceryItem In basket
                wFile.Write(item.Values) 'See GroceryItem.vb for Values code
            Next
            wFile.Close()
        End If
    End Sub

    Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem7.Click
        If OpenDialog.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = OpenDialog.FileName
            Dim sFile As New FileStream(fileName, FileMode.Open)
            Dim rFile As New StreamReader(sFile)
            Dim item As GroceryItem         'GroceryItem for each slot in basket
            Dim itemValues As String()      'Holds the read array
            basket.Clear()                  'Empties the basket
            Do Until rFile.Peek() = -1
                itemValues = rFile.ReadLine().Split(",")
                item = New GroceryItem(itemValues(0), itemValues(1),
                                       CDbl(itemValues(2)), itemValues(3),
                                       [Enum].Parse(GetType(Aisle), itemValues(4)))
                basket.Add(item)
            Loop
            rFile.Close()
        End If
    End Sub
End Class

Public Class GroceryItem
    'Variables
    Public _ScanNumber As String
    Public _BrandName As String
    Public _Description As String
    Public _numPrice As Double
    Public _Aisle As Aisle
   
    'Unique serial item number
    Public ReadOnly Property ScanNumber As String
        Get
            Return _ScanNumber
        End Get
    End Property

    'Item name
    Public Property BrandName As String
        Get
            Return _BrandName
        End Get
        Set(value As String)
            _BrandName = value
        End Set
    End Property

    'Item description
    Public Property Description As String
        Get
            Return _Description
        End Get
        Set(value As String)
            _Description = value
        End Set
    End Property

    'Price amount
    Public Property numPrice As Double
        Get
            Return _numPrice
        End Get
        Set(value As Double)
            If value > 0 Then
                _numPrice = value
            End If
        End Set
    End Property

    'Grocery store aisle
    Public Property Aisle As Aisle
        Get
            Return _Aisle
        End Get
        Set(value As Aisle)
            _Aisle = value
        End Set
    End Property

    'Constructors
    Public Sub New(scanNumber As String)
        Me.New(scanNumber, Nothing, 0)
    End Sub

    Public Sub New(scanNumber As String, brandName As String, numprice As Double)
        Me.New(scanNumber, Nothing, 0, Nothing, Aisle.Bakery)
    End Sub

    Public Sub New(scanNumber As String, brandName As String, numprice As Double, description As String,
                   aisle As Aisle)
        _ScanNumber = scanNumber
        Me._BrandName = brandName
        Me._numPrice = numprice
        Me._Description = description
        Me._Aisle = aisle
    End Sub

    Public Sub Print()
        MessageBox.Show("Aisle: " & Me.Aisle.ToString & vbCrLf & "Scan Number: " & _
                        ScanNumber & vbCrLf & "Brand Name: " & Me.BrandName)
    End Sub

    Public Function Values() As String
        Return _ScanNumber & "," & Me.BrandName & "," & Me.numPrice & "," &
            Me.Description & "," & Me.Aisle.ToString & vbCrLf
    End Function
End Class

Public Enum Aisle
    Bakery
    CannedGood
    Drinks
    Deli
    DryGoods
    FrozenFood
    Produce
End Enum


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: Need help with grocery app project

Posted 14 July 2016 - 11:24 AM

My suggestion still stands. Put a break point where you are adding things and step through the code. Find the object you have not instantiate, but are trying to use.
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2343
  • View blog
  • Posts: 9,398
  • Joined: 03-December 12

Re: Need help with grocery app project

Posted 14 July 2016 - 11:25 AM

I don't see basket anywhere in that sub. Which is the problem Modi pointed at.
Was This Post Helpful? 1
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1459
  • View blog
  • Posts: 3,702
  • Joined: 01-February 13

Re: Need help with grocery app project

Posted 14 July 2016 - 11:28 AM

I did not see any indication of where in your GroceryItemForm class that you have declared a List or a Control named "basket". Maybe i missed it as i only skimmed through the form code and the forms designer code with a text editor. However, i believe modi123_1 is correct, you have not declared a New instance of "basket", whatever that might be.
Was This Post Helpful? 1
  • +
  • -

#7 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 11:45 AM

@Modi123_1

I instantiated in my main, here is the code:

Module Main
    'Declare variables
    Friend basket As New GroceryBasket
    Friend blnLoggedIn As Boolean
    Dim arrUsernames() As String = {"Admin", "Clerk", "Manager"}
    Dim arrPasswords() As String = {"[email protected]", "pa$$word", "passwOrd"}

    


    Sub Login(username As String, password As String)
        blnLoggedIn = False

        If VerifyUserName(username) And VerifyPassword(password) Then
            'Find index for username
            Dim userIndex As Integer
            For loopIndex = 0 To arrUsernames.Length - 1
                If arrUsernames(loopIndex) = username Then
                    userIndex = loopIndex
                    Exit For
                End If
            Next

            'Check for password match
            If arrPasswords(userIndex) = password Then
                blnLoggedIn = True
            Else
                Throw New LoginException
            End If
        End If
    End Sub

    Function VerifyUserName(username As String) As Boolean
        Dim arrUsernames() As String = {"Admin", "Clerk", "Manager"}
        username = "Admin, Clerk, Mangager"
        For Each Str As String In arrUsernames
            If Str.Contains(username) Then
                MsgBox("Found" & username & "at index" &
                       Str.IndexOf(username))
            End If
        Next
        Return True
    End Function

    Function VerifyPassword(password As String) As Boolean
        Dim arrPasswords() As String = {"[email protected]", "pa$$word", "passwOrd"}
        password = "[email protected], pa$$word, passwOrd"
        For Each Str As String In arrPasswords
            If Str.Contains(password) Then
                MsgBox("Found" & password & "at index" &
                       Str.IndexOf(password))
            End If
        Next
        Return True
    End Function
End Module



Was This Post Helpful? 0
  • +
  • -

#8 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 12:25 PM

I tried to add new basket object to sub, but still get same error.
the instructions for that part of code for project is:

1. In the Click event of the btnAddToBasket button, perform the following steps:

a. Create a GroceryItem object using the values from the controls and add it to the basket variable.
Note: Remember the basket variable is the GroceryBasket collection.

b.Verify all controls except txtScanNumber contain a value.

c. Set the value of the txtScanNumber control using the following code:
txtScanNumber.Text = txtBrandName.Text.Substring(0,3) & "1019"

d.Instantiate the GroceryItem class, using the control values

e. Use the following expression to set the Aisle property. The expression converts the text into an Aisle enumeration.
[Enum].Parse(GetType(Aisle), cboAisle.Text)

f.Add the GroceryItem object to the basket variable.

Where would i instantiate "basket" and would i add code: Dim basket As New GroceryBasket?

Here is the grocerybasket class:
Public Class GroceryBasket
    Inherits List(Of GroceryItem)
End Class


Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: Need help with grocery app project

Posted 14 July 2016 - 12:38 PM

IS this a webform, winform, MDI, or WPF app?
Was This Post Helpful? 0
  • +
  • -

#10 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 12:45 PM

Modi123_1 it is a windows form app...
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: Need help with grocery app project

Posted 14 July 2016 - 12:48 PM

Okay.. so it seems odd to have a 'mod main' in a winform app. What's up with that?
Was This Post Helpful? 0
  • +
  • -

#12 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 12:56 PM

Not sure...I'm taking this programmming course online and basically following a textbook and some online material provided by the school. If i need help, i have to email the instructor and wait for at least 24 hours, very difficult to learn this way. I usually end up going to forums for help, since instructions for projects can be very vague. They also have you skip chapters in the book(one ended up being how to debug), so i haven't learned how to do that correctly. The response i got back from my instructor is that the error is a result of program looking for a variable that it can't find(it wants to populate it with a value, but when it looks for the identity, the program says that the ID is null, it doesn't exist) Check your object definitions (variables) carefully...they must match in each file where they are used.

I get that, but most of the code i'm having trouble with is the one provided by the school! so frustrating...
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13562
  • View blog
  • Posts: 54,110
  • Joined: 12-June 08

Re: Need help with grocery app project

Posted 14 July 2016 - 12:58 PM

... okay? Not really responsive in why you have a mod main with a winform, but let's move forward.

Make a backup zip of your project.. then move all the 'mod main' crud into your winform. Remove the 'mod main'. See what's up then.
Was This Post Helpful? 0
  • +
  • -

#14 cbadguy79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-July 16

Re: Need help with grocery app project

Posted 14 July 2016 - 12:59 PM

All i know is that line of code is what the error highlights.(in bold)
 Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, ToolStripMenuItem1.Click
        txtScanNumber.Text = txtBrandName.Text.Substring(0, 3) & "1019"

       [b] Dim item As New GroceryItem(txtScanNumber.Text, txtBrandName.Text, numPrice.Value, txtDescription.Text,
                                        [Enum].Parse(GetType(Aisle), cboAisle.Text))
[/b]
        basket.Add(item)
        clearForm()
    End Sub




Was This Post Helpful? 0
  • +
  • -

#15 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1459
  • View blog
  • Posts: 3,702
  • Joined: 01-February 13

Re: Need help with grocery app project

Posted 14 July 2016 - 01:00 PM

After looking into your code further, your "TextDescription" that is used in the constructor of the new GroceryItem is actually declared at the top of your code as an Object. Not only did you not create a New instance of it but, an Object does not have a Text property.

Are you sure that the "TextDescription" should not be a TextBox on the GroceryItemForm Form? If so, then remove the line at the top of the code shown here.
Dim txtDescription As Object

and then add a TextBox to the GroceryItemForm Form and name it "TextDescription".

That should fix your problem i think.
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3