-2
Public Class Json_Info
    Public fruit As Json_Info_Fruit
End Class

Public Class Json_Info_Fruit
    Public aa As String
    Public ab As Integer
End Class

Public Class Main
    Private Sub Example()
        Dim fruitInfo As New Json_Info
        fruitInfo.fruit.aa = "apple" 'Error On This Line
        fruitInfo.fruit.ab = 1

        Dim output As String = JsonConvert.SerializeObject(loginInfo)
        MsgBox(output)
    End Sub
End Class

Error On fruitInfo.fruit.aa = "apple"

What's wrong? (what.. all examples on json.net is C# examples. no one vb.net. so hard to learn)

i need to make..

{
    "fruit": {
        "aa": "apple",
        "ab": 1
    }
}

sry for my bad english :P help me

Blank
  • 355
  • 1
  • 3
  • 8
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Dec 07 '14 at 13:58
  • 2
    *"Error On fruitInfo.fruit.aa = "apple""* Always say ***what*** error, not just that you're getting one. In this case, we could guess, but... – T.J. Crowder Dec 07 '14 at 14:02

1 Answers1

1

You never initialize fruitInfo.fruit, and there's no Json_Info constructor to do it, so the fruit property is initially Nothing.

Either:

  1. Add a constructor to initialize it, or

  2. If you want to do it per-use, be sure you do that:

    Dim fruitInfo As New Json_Info
    fruitInfo.fruit = New Json_Info_Fruit        ' This is the new line
    fruitInfo.fruit.aa = "apple"
    
  3. Or maybe you can use the New keyword in the declaration of the fruit member, I don't now VB.Net well and MSDN isn't being useful:

    Public fruit As New Json_Info_Fruit
    

    But again, double-check that.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875