Re: My Array. Help Please
I have several questions for you. First of all, class book looks like an entity class, so why not use set and get methods for data access and retrieval? This ensures proper data encapsulation and adheres to java best practices.It also enables you to retreive more specific attribute values. Second, to me it looks like you are using num as an ID, so why not rename the field ID?
If your intention is to use num as a count for the total number of book objects created, you can always declare static field count that is incremented in both constructors. You can then use the variable count to auto-increase the ID field and only set a retrieval or 'get' method to access this field.
My next question focuses on the use of your bookstore. Why are you using an array? Wouldn't it be easier to use a generic books collection to represent the many-to-one relationship between books and bookstore i.e. many books are found in one bookstore? I assume this is the relationship you are trying to model. You can add individual books to the collection bookstore and iterate through them to access/print the details.
Generally, arrays are fixed length i.e. they have a predetermined value, whereas collections are dynamic and re-sizable and used when you do not know the total number of objects being modeled - in this case, individual books. One of the reasons why you will be having difficulty (besides the above) is for starters, you create the array books - but in your for loop, you do not assign books[i] any new values to reference. So even though you've created the array type of books, you haven't assigned any books to it - thus you are referencing objects that are assigned a null value. You need to add code similar to the following:
Code Java:
for ( int i = 0; i < books.length; i++ )
{
books[i]= new Book();
...let me know your thoughts before I go on.
Re: My Array. Help Please
Quote:
...let me know your thoughts before I go on.
Hey Switcha, just a quick note that I'd bet the original poster has solved or moved on from this problem, considering the initial post date is over a year old ;)
Re: My Array. Help Please
...Good point. Ahwell, still good to practice on other people's prob's - it keeps the mental tool sharp :P