4 Replies - 435 Views - Last Post: 12 December 2012 - 09:14 PM Rate Topic: -----

#1 SAlford6129  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 03-September 12

Issues with Structure and List or Collection

Posted 06 December 2012 - 08:21 AM

Okay so I'm making a clone of a game (Pokémon) and I've run into an issue. I have a Structure which is the basic layout for the Pokémon you use. I have a Collection, List, etc... inside the structure. However, even though it compiles and runs, I get an exception every time I try and add something into the list, and it says there is no object instance.

 
Public Structure PKMN
     Public name as String
     Public pkmnMoves as Collection '(or List, etc)
End Structure

Public Structure Move
     Public name as String
     Public damage as int
End Structure

Public Bulbasaur as new PKMN
Public pkmnMoves as new Collection '(because Public Bulbasaur.pkmnMoves As New Collection doesn't work)
Public Struggle as new Move

Public Sub AddMove()
Bulbasaur.name = "Bulbasaur"
Struggle.name = "Struggle"
Struggle.Damage = 35
Bulbasaur.pkmnMoves.Add(Struggle, 0)
End Sub



Placed into a simple button, AddMove() returns the error:
"An unhandled exception of type 'System.NullReferenceException' occurred in PokémonRainbow.exe

Additional information: Object reference not set to an instance of an object."

I have tried a collection, and specifiying a Key, I have tried a list, and adding an item, and nothing seems to work. I get the same error no matter what I do, and the only way I can add an object is outside of the structure, which does me absolutely no good as it's not linked to that particular Pokémon. I'm hoping there is an easy solution tp this, if this just isn't possible, then I suppose I'll have to look into some kind of Array or Linked List, because otherwise there is no way to keep track of which Pokémon knows what moves, and whether or not you've learned a new/changed a move.

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with Structure and List or Collection

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Issues with Structure and List or Collection

Posted 06 December 2012 - 09:04 AM

In line 3 put New keyword before Collection, this way you will instantiate it.
Was This Post Helpful? 0
  • +
  • -

#3 SAlford6129  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 03-September 12

Re: Issues with Structure and List or Collection

Posted 06 December 2012 - 10:04 AM

View Postlucky3, on 06 December 2012 - 04:04 PM, said:

In line 3 put New keyword before Collection, this way you will instantiate it.


lol Thanks but I tried that already. That was my first idea. VB gives the error "Non shared members in a Structure cannot be declared 'New'."
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Issues with Structure and List or Collection

Posted 06 December 2012 - 10:21 AM

Then change from structure to class.
Was This Post Helpful? 0
  • +
  • -

#5 SAlford6129  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 03-September 12

Re: Issues with Structure and List or Collection

Posted 12 December 2012 - 09:14 PM

Sadly this is what I had to do. Now everything works fine, I just really thought my decision on a structure was right, but I guess later on down the line a class really is better.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1