-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
  • 305
  • 1
  • 3
  • 8

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
  • 959,406
  • 173
  • 1,780
  • 1,769