-1

I was learning Java and came across this code snippet. Have an answer for that but I am not exactly understanding how it works.

I have a class Books

class Books {
   String author;
   String title;
}

Then I have test class

class TestForBook {

   Books[] myBooks = new Books[3]; // doubt : 1
   System.out.println('myBooks length is ::' + myBooks.length);//prints 3. Its correct.

  myBooks[0].title = 'Book1 title'; // This throws null pointer exception

  myBooks[0] = new Books();
  myBooks[0].title = 'Book1 title'; //works fine after above line
}

I want to understand why even after declaring array of type Books, array values have null(doubt : 1 in comments, I am referring to that line).

I do not know what concept of Java I am missing. And how/from where I can learn these kind of topics. Any resource or books suggestion also would be appreciable. Thanks.

It is not duplicate of question id : 1922677. Solution is available there(I have also given the solution) but I wanted to know why it is like that. I mean even after declaring, why it has null was my question.

Pradeep
  • 140
  • 9
  • "I want to understand why even after declaring array of type Books, array values have null" - because when you create an array, every element of the array has the default value for that type. The default value for classes is null. – Jon Skeet Jan 03 '18 at 10:24
  • you have declared the array, but not the elements in them, so they have the default value, being null – Stultuske Jan 03 '18 at 10:24

4 Answers4

4

When creating an array of objects, remember that the new command applies to the array, which will initially contain the requested slots to hold the objects, but will not be populated by any specific object. This means that in order to have an array of 3 Books, you need to

  • Create an array object that can hold 3 books new Book[3]
  • Create 3 Book objects new Book(), and put them in the array (one in each slot)
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
3

This is because you just created an empty array of size (length) 3.

Books[] myBooks = new Books[3];

With

myBooks[0] = new Books();

you define a book in your array. Therefore you can set the title afterwards.

Chris311
  • 3,794
  • 9
  • 46
  • 80
2

When you create Books[] myBooks = new Books[3] .It will create array which can hold object of books,But all value is null.You have not put any thing into that array.While accessing myBooks[0].title it is actually calling title on null value so throwing null pointer exception.

In second scenario you are assigning book object and then calling title on that object. So Main point is when you call method or attribute on a null object it will throw exception. myBooks[0] = new Books(); myBooks[0].title = 'Book1 title';

gati sahu
  • 2,576
  • 2
  • 10
  • 16
1

Comments inline to make sense of it

class TestForBook {

   Books[] myBooks = new Books[3]; // here you have initialized an array of size 3, but it is currently empty, but three Book object can fit in it
   System.out.println('myBooks length is ::' + myBooks.length);//prints 3. Its correct. // since you have specified the length of the array, this will return 3 despite it being empty

  myBooks[0].title = 'Book1 title'; // This throws null pointer exception because your books array is empty, myBooks[0] = null, hence the null pointer exception.

  myBooks[0] = new Books(); //now you have added a book object in the empty book array at index 0
  myBooks[0].title = 'Book1 title'; //since myBooks[0] now contains a books object that your created above, this will return that object instead of null and will work. However if you try this with myBooks[1] that will be null as you still have not put a book object at index 1
}
Vardaan Sharma
  • 1,125
  • 1
  • 10
  • 21