Results 1 to 5 of 5

Thread: Need help understanding null reference

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    7

    Need help understanding null reference

    Code:
    Imports System.ComponentModel.DataAnnotations
    
    Public Class Hardware
        <Key>
        Public Property HardwareID As Integer
        Public Property SeriesID As Integer
        Public Property Series As Series          <==this is another class, it is the null below
        <Required, Display(name:="Model")>
        Public Property Model As String
        <Display(name:="Item ID")>
        Public ReadOnly Property ItemID As String
            Get
    
                Dim db As New Test3.Models.Test3Context
                Dim codeString = Me.Series.Manufacturer.HardwareType.HardwareTypeCode & Me.Series.Manufacturer.ManufacturerCode & Me.Series.SeriesCode   <==(null ref here)
                Dim count = 1
                Dim ItemIDString As String
                For Each h In db.Hardwares
                    If IsNothing(h.ItemID) Then
                    Else
                        If h.ItemID.StartsWith(codeString) Then
                            count = count + 1
                        End If
                    End If
                Next
    
                ItemIDString = codeString & count.ToString
    
                Return ItemIDString
            End Get
        End Property
    
    End Class
    What am I doing wrong???? I have gotten this to work where it would just give the codeString as the Id, but I want to add a suffix for each one based on count, it should see how many with the same code string and make this one the next one. How is this failing when next didn't? I originally tried without the ME prefix, but got same result.

    Code:
    Imports System.ComponentModel.DataAnnotations
    
    Public Class Hardware
        <Key>
        Public Property HardwareID As Integer
        Public Property SeriesID As Integer
        Public Property Series As Series
        <Required, Display(name:="Model")>
        Public Property Model As String
        <Display(name:="Item ID")>
        Public ReadOnly Property ItemID As String
            Get
                Return Series.Manufacturer.HardwareType.HardwareTypeCode & Series.Manufacturer.ManufacturerCode & Series.SeriesCode
            End Get
        End Property
    This one works fine.
    Last edited by papasmurf61706; Dec 30th, 2014 at 01:40 PM.

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Need help understanding null reference

    maybe it's a naming convention issue and Series.Manufacturer.HardwareType.HardwareTypeCode and Series.Manufacturer.ManufacturerCode & Series.SeriesCode are all static members?
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Need help understanding null reference

    Public Property Series As Series <-- dont' ever do that... because it just leads to confusion... when you reference Series.SomeProperty ... what are you getting? the Shared member of Series the class or an actual property of the instance of that class? You don't know.

    This fails
    Dim codeString = Me.Series.Manufacturer.HardwareType.HardwareTypeCode & Me.Series.Manufacturer.ManufacturerCode & Me.Series.SeriesCode <==(null ref here)

    because you're trying to access the INSTANCE variable of Series from the current instance of you class. Since Series the variable was never instanciated, it stands to reason it's nothing and results in the error you're getting.

    So, why did this work:
    Return Series.Manufacturer.HardwareType.HardwareTypeCode & Series.Manufacturer.ManufacturerCode & Series.SeriesCode
    Because you used Series generically, since the variable was nothing, it defaulted to the Class type and returned the shared members.

    -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??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    7

    Re: Need help understanding null reference

    @techgnome, thanks for the response, I am still confused though. The me.~ was just what I had tried on the last attempt to get it to work before I ran out of ideas. I also tried the same code as was returned in the second code sample. I know my naming convention is bad, forgive me. I have been learning vb.net for 4 months and MVC for a month. I was using those names for my sake to keep track. I have 4 classes, I changed the naming this time.

    Code:
    Imports System.ComponentModel.DataAnnotations
    
    Public Class Hardware
        <Key>
        Public Property HardwareID As Integer
        Public Property SeriesID As Integer
        Public Property SeriesForThisHardware As Series
        <Required, Display(name:="Model")>
        Public Property Model As String
        <Display(name:="Item ID")>
        Public Property ItemID As String
            Get
    
                Dim db As New Test4.Models.Test4Context
                Dim count = 1
                For Each h In db.Hardwares
                    If IsNothing(h.ItemID) Then
                        Return SeriesForThisHardware.ManufacturerForThisSeries.HardwareTypeForThisManufaturer.HardwareTypeCode & SeriesForThisHardware.ManufacturerForThisSeries.ManufacturerCode & SeriesForThisHardware.SeriesCode & count.ToString
                    Else
                        If h.CatlabID.StartsWith(SeriesForThisHardware.ManufacturerForThisSeries.HardwareTypeForThisManufaturer.HardwareTypeCode & SeriesForThisHardware.ManufacturerForThisSeries.ManufacturerCode & SeriesForThisHardware.SeriesCode) Then
                            count = count + 1
                        End If
                    End If
                Next
    
                Return SeriesForThisHardware.ManufacturerForThisSeries.HardwareTypeForThisManufaturer.HardwareTypeCode & SeriesForThisHardware.ManufacturerForThisSeries.ManufacturerCode & SeriesForThisHardware.SeriesCode & count.ToString
            End Get
            Set(value As String)
    
            End Set
        End Property
    
    End Class
    
    Public Class Series
        <Key>
        Public Property SeriesID As Integer
        Public Property ManufacturerID As Integer
        Public Overridable Property ManufacturerForThisSeries As Manufacturer
        <Required, Display(name:="Series")>
        Public Property SeriesName As String
        <Required, Display(name:="Series Code"), StringLength(2, ErrorMessage:="Please enter a 2 character code to be used in the Item ID", MinimumLength:=2)>
        Public Property SeriesCode As String
    End Class
    
    Public Class Manufacturer
        <Key>
        Public Property ManufacturerID As Integer
        Public Property HardwareTypeID As Integer
        Public Overridable Property HardwareTypeForThisManufacturer As HardwareType
        <Required, Display(name:="Manufacturer")>
        Public Property ManufacturerName As String
        <Required, Display(name:="Manufacturer Code"), StringLength(2, ErrorMessage:="Please enter a 2 character code to be used in the Item ID", MinimumLength:=2)>
        Public Property ManufacturerCode As String
    End Class
    
    Public Class HardwareType
        <Key>
        Public Property HardwareTypeID As Integer
        <Required, Display(name:="Type of Hardware")>
        Public Property HardwareTypeName As String
        <Required, Display(name:="Hardware Type Code"), StringLength(2, ErrorMessage:="Please enter a 2 character code to be used in the Item ID", MinimumLength:=2)>
        Public Property HardwareTypeCode As String
    This is how i understand you have to do it for code first using entity framework to generate the relationships and database. I guess I am asking a few questions. If this is not best way, what should I change? Can I get the application to generate an integer suffix to the itemID, and if so how? I want to be able to sort by each class and use dropdowns during item creation. i have been using Microsoft Virtual Academy and ASP.net website PluralSite videos, thanks again for any advice.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Need help understanding null reference

    Public Property SeriesForThisHardware As Series -- that's better... probably should call it HardwareSeries ... shorter... anyways... still getting the Null reference error? Not surprised. Simply declaring something (such as you have) doesn't actually create it... it just says "Hey I need a box." ... before you can use the box you first have to create the box. Actually, you've got that problem all the way down through...

    If I have Dim someX as TextBox ... that simply says to VB, I need a box and it looks like a TextBox ... but it doesn't exist yet until I create it.
    someX = New TextBox ... NOW it exists...
    or
    Dim someX as TextBox = New TextBox ... I declare it and create it in one go, verbose style
    or
    Dim someX as New TextBox ... I declare it and create it in one go, concise style

    Since you're exposing properties, I think you may need to new-up the backing members in the constructor.

    But I don't use EF so I'm not even sure what you're doing is possible.

    -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??? *

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