Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Putting data into array

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
solved everything. thanks a lot guys
 
Campbell Ritchie
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic