This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Putting data into array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Putting data into array" Watch "Putting data into array" New topic
Author

Putting data into array

Arthur Donting
Greenhorn

Joined: Mar 22, 2009
Posts: 10
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

Joined: Oct 13, 2005
Posts: 32689
    
    4
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
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

Joined: Mar 22, 2009
Posts: 10
solved everything. thanks a lot guys
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
You're welcome
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Putting data into array
 
Similar Threads
Some Codes
Convert 1d array to 2D boolean
A char array question
floating point division
Keyboard.Class/Anyone with brains.pt2