0

Im getting an error "Object reference not set to an instance of an object" in Visual Studio when I try to start the application. I'm using Visual Basic

This is where the error occurs:

Public Overrides Function ToString() As String
    Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address.ToString())
    Return strOut
End Function

m_address.ToString() is: (no error on the code below)

Public Overrides Function ToString() As String
    Dim strOut As String = String.Format("{0, -20}{1,-10}{2, -10}{3, -10}",
                                         m_street, m_zipCode, m_city, GetCountryString())
    Return strOut
End Function

Does anyone know how to fix the error? Thanks!

UPDATE:

Private m_address As Address

'Creates the m_address object in the constructor
Public Sub New()
    m_address = New Address()
End Sub

UPDATE V2:

Public Class Address

    Private m_street As String
    Private m_zipCode As String
    Private m_city As String
    Private m_country As Countries

    'Defualt constructor
    Public Sub New()

        Me.New(String.Empty, String.Empty, "Malmö")
    End Sub

    'Constructors
    'Constructors calling another constructor
    Public Sub New(ByVal steet As String,
                   ByVal zip As String,
                   ByVal city As String,
                   ByVal country As Countries)
        Me.m_street = Street
        Me.m_zipCode = zip
        Me.m_city = city
        Me.m_country = country
    End Sub

    Public Sub New(ByVal street As String, ByVal zip As String, ByVal city As String)
        Me.m_street = street
        Me.m_zipCode = zip
        Me.m_city = city
    End Sub

    Public Sub New(ByVal theOther As Address)
        Me.m_street = theOther.Street
        Me.ZipCode = theOther.ZipCode
        Me.m_city = theOther.City
    End Sub
'Propoties..

Public Function GetCountryString() As String
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

UPDATE V3

'Here i list all the countries in the world
        Public Enum Countries
    Country 1
    Country 2
    Country 3 
    Country 4
    etc..
    End Enum
John Saunders
  • 157,405
  • 24
  • 229
  • 388
David. R
  • 17
  • 6
  • 2
    have you put a breakpoint on the offending line to find out **what** your problem is? – Mark Hall Dec 27 '14 at 20:11
  • 1
    may have to do with `GetCountryString` since it *looks like* a list , collection or object is involved with `country As Countries`. a breakpoint would have solved this 20 mins ago – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:13
  • Can you post the code for `GetCountryString`? I think @Plutonix is on to something. – Tim Dec 27 '14 at 20:14
  • Updated the information – David. R Dec 27 '14 at 20:17
  • whatever `Countries` is, it is not initialized. the linked dupe above shows how to ferret these out in [the VB answer](http://stackoverflow.com/a/26761773/1070452) – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:18
  • Countries is actully an Enum of all the counties in the world in which the user can choose from ComboBox. I'll update the question – David. R Dec 27 '14 at 20:21
  • @David.R - Is the selection from the ComboBox getting passed into the class? Have you verified that in the debugger? – Tim Dec 27 '14 at 20:25
  • 2
    Back to basics: set a breakpoint; hold the mouse over the vars and see if any report `Nothing`; if not step into `Address.ToString` and repeat. Something somewhere is `Nothing` or a function is returning `Nothing`. This wont be your only visit from NRE, so take a minute to learn how to find the culprit. – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:28
  • 1
    @Plutonix I think our suggestion is being ignored, only way to know for sure, otherwise it is just guess work – Mark Hall Dec 27 '14 at 20:41
  • put the cursor on the line and press F9; that is fake code by the way; it doesnt compile – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:46
  • There is no error anymore. The problem is that only shows the City and Country in a listbox – David. R Dec 27 '14 at 20:47
  • @Plutonix Thanks, I will try to learn using a breakpoint. I'm kinda new to programming and haven't really learned about breakpoints yet – David. R Dec 27 '14 at 20:50
  • 1
    the "fix" just means you are no longer trying to do stuff with the thing which is nothing. [NullReference Exception -- Visual Basic](http://stackoverflow.com/a/26761773/1070452) (includes how to find them) – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:54
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 28 '14 at 00:35

1 Answers1

0

Be sure to initialize all your used variables or:

Change

Public Overrides Function ToString() As String
   Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address.ToString())
   Return strOut
End Function

to

Public Overrides Function ToString() As String
   Dim strOut As String = String.Format("{0, -20} {1}", LastName, m_address)
   Return strOut
End Function

also change

Public Function GetCountryString() As String
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

to

Public Function GetCountryString() As String
    If m_country Is Nothing Then Return Nothing
    Dim strCountry As String = m_country.ToString()
    strCountry = strCountry.Replace("_", " ")
    Return strCountry
End Function

ToString() on uninitialized objects will fail with the error you get.

When using String.Format() you do not have to use a .ToString() on each variable you are adding. This happens automatically and should the object be null it will not fail with an "Object reference not set to an instance of an object". It will default to String.Empty.

Wolf5
  • 14,590
  • 11
  • 57
  • 55
  • 1
    How will this resolve the OP's problem? – Tim Dec 27 '14 at 20:23
  • 1
    `m_address` is a Type – Ňɏssa Pøngjǣrdenlarp Dec 27 '14 at 20:24
  • String.Format will fail with object.ToString() as variable if null/nothing. You do not explicitly have to do a .ToString() when using String.Format(). It does that implicitly, and will not fail on null. – Wolf5 Dec 27 '14 at 20:28
  • m_country is an Enum of all the countries in the world and is chosen with a ComboBox by the user. It does not have an Nothing, it always has a value at the start of the application so there will never be a Nothing for countries – David. R Dec 27 '14 at 20:34
  • I see that. Well. Then only the first part is usable here. That will remove the error if that is the culprit. – Wolf5 Dec 27 '14 at 20:38
  • The first change you suggested (m_address instead of m_address.ToString() did somewhat work. But it only shows the city and country and ignores the Lastname and the rest of the address information – David. R Dec 27 '14 at 20:38
  • It did remove the error, but it doesn't display all all the information, just the City and Country – David. R Dec 27 '14 at 20:39
  • Then put a breakpoint in the ToString() and see what values all your variables have. Then you need to trace down why some values are empty. – Wolf5 Dec 27 '14 at 20:46
  • I found out that the object m_address is Nothing, the reason is probably because it has Countries as reference and it is used a reference itself. But I don't know how to initialize a reference that has a reference on its own.. – David. R Dec 27 '14 at 22:35
  • The only reason m_address is Nothing is because it has not been set yet. The line m_address = New Address() has yet to be run. Or somewhere in your code there is a m_address = Nothing. What you should do is create a property instead of field for m_address. The first time anyone asks for it, it gets initialized. – Wolf5 Dec 27 '14 at 23:39
  • And the solution was? – Wolf5 Dec 28 '14 at 14:07