0

I develop an application , and i have a problem , after reading this help , i try to use this instruction

Public Function subfolder_creation(id_sub As String) As String
         Dim subfolder As New Google.Apis.Drive.v2.Data.File()
        subfolder.Title = VF.Text
    Dim reference As ParentReference ' référencier au dossier parent
    reference.Id = id_sub
    subfolder.Parents.Add(reference)
    subfolder.MimeType = "application/vnd.google-apps.folder"
 Dim reference As ParentReference ' référencier au dossier parent
    reference.Id = id_sub

    subfolder.Parents.Add(reference)

Exception is in instruction : subfolder.parent.add(reference)

System.NullReferenceException : 'La référence d'objet n'est pas définie à une instance d'un objet.'

translate to english : System.NullReferenceException: 'The object reference is not set to an instance of an object.'

Zied.M
  • 215
  • 4
  • 17
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Visual Vincent Aug 03 '17 at 10:46
  • but when i get break point and inspect my variable value , all are correct subfolder title is correct reference = id_sub is correct id_sub has a same value from another method – Zied.M Aug 03 '17 at 10:49
  • no i change it to Dim reference = new ParentReference() ....and the same exception – Zied.M Aug 03 '17 at 10:58
  • Well something is null. Either it is something in `reference`, `subfolder`, `subfolder.Parents` or something internal. – Visual Vincent Aug 03 '17 at 10:59
  • reference has a same value with id_sub (also same value from another method) , sudfolder has a correct title , subfolder.parents is nothing but i think is no problem because after .add instruction , subfolder.parents will take a value... but first it's nothing – Zied.M Aug 03 '17 at 11:04
  • I think you're setting `subfolder.MimeType` too late. Move it up above the `subfolder.Parents.Add()` call. – Visual Vincent Aug 03 '17 at 11:08
  • i move it , but the same result – Zied.M Aug 03 '17 at 11:11
  • My final suggestion (I've never used the Drive API so my suggestions are just based on research): Reading the [**documentation**](https://developers.google.com/drive/v2/web/folder) they seem to create the list themselves, so what if you try `subfolder.Parents = New List(Of String)` before the first `subfolder.Parents.Add()`? – Visual Vincent Aug 03 '17 at 11:15
  • System.InvalidCastException : 'Impossible d'effectuer un cast d'un objet de type 'System.Collections.Generic.List`1[System.String]' en type 'System.Collections.Generic.IList`1[Google.Apis.Drive.v2.Data.ParentReference]'.' – Zied.M Aug 03 '17 at 11:22
  • Whoops, sorry. I was reading another version of the documentation. I meant: `New List(Of ParentReference)`. – Visual Vincent Aug 03 '17 at 11:24
  • yes , i used parentreference I think it's work , thank you very much – Zied.M Aug 03 '17 at 11:29
  • So the list was null after all ;). Glad I could help! Good luck! – Visual Vincent Aug 03 '17 at 11:34

1 Answers1

1

It appears it is the Parents list that is null. Try initializing it before the first Add() call:

subfolder.Parents = New List(Of ParentReference)
subfolder.Parents.Add(reference)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75