• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting data into array

 
Arthur Donting
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, I need to create an array of classes. for now i have this fragment:



I keep getting NullPointerException. When i use this:


everything works just fine but i need to have an array. Coud anyone help out with this?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you really been given a CarData class with public fields like that? Very bad design.

Have you initialised the array to anything before trying to gain access to its 1st element? If not, you will suffer a NullPointerException.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats becaus you have to initialize your array indexes with Cars.



As a little advice, try not to access properties directly. Encapsulate them by using JavaBean Properties.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you initialised the 1st element in the array to anything? If not you will suffer a NullPointerException there too.

Remember if you declare anything, that doesn't have any value until you initialise it. If you have a 15-member array, then you have 15 references which all point to null until you tell them otherwise.
 
Arthur Donting
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved everything. thanks a lot guys
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic