• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer problem  RSS feed

 
Nicholas Gravagne
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello -- new to JR and to Java, though lots of experience with VB, VBA and Open Office. The following code comes from the excellent Head First Java book. The class is called Boks, while in the book it is called Books (long story). Anyway, the main code stops running per a null pointer issue at line "myBoks[0].title = "The Grapes of Java";". I can't figure out why. Apparently the public class Boks is not talking with the main test drive class. All the curly-ques and ;s are in place, just keep slamming into the null pointer thing.

Thanks for being out there! --- Nick
______________________________________________________



_______________________________________________________

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line:

...does not fill the array with three objects; it allocates space for three book objects.  You still have to initialize each element of the array.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notes on your post: I corrected the code tags for you.  Basically, you need to surround the code with the tags.  You can press the "Preview" button before the "Submit" button to see if you did it correctly.

Also, if you are reporting an exception, please post the stack trace of the error.
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line only allocates references for 3 Boks. It does not assign a new Boks to each reference, hence the references are null.

You'd need this
 
Nicholas Gravagne
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Knute and Carey: but note that in order for build and output to work, I had to initialize array multiple times for each title var. This works, but doesn't look right. Correct output, thought looks like this.

The Grapes of Java by bob
The Java Gatsby by sue
The Java Cookbook by ian
BUILD SUCCESSFUL (total time: 0 seconds)

------------------------------------------------------
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note on post: you are still not using code tags correctly. 

What about the code looks wrong?  You do have to initialize each element of the array before you use it.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The next step in your project would be to add a constructor to the Boks class that takes the title and author and saves them in class. 
That would clean up the code by getting rid of all the assignment statements that are used to set the title and author.
 
Nicholas Gravagne
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks -- code seems redundant to me as is, that something slimmer could be used. But I am just a greenhorn and have a lot to learn. I did try the code tags, but must have missed something.

Nick
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch

You will realise that is the normal behaviour of arrays of reference types; they are filled with null references until you replace them with real values.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you declare the array:

you have created a SPOT for each Boks.  You have a bookshelf, but no actual books. You have to create each and every one.

Now, you could write a loop that creates one for each slot, which would save on some of the redundant code...
 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you implemented a constructor in Boks that takes a title and an author, your initialization could be simplified to this

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To eliminate the redundant looking code, just write a function to create a Bok:


or you could edit the Boks class and create a constructor that takes the title and author arguments
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yet another way
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway, why your Boks fields are public?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because the fields in the HFJ example are public. I think it is later in the book that K&B explain that fields shou‍ld be private.
 
Nicholas Gravagne
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all --- thanks for all the valuable posts. The big lesson for me is that an array begins by allocating a storage space Boks [] myBoks = new Boks[3], but that objects must then be created to fill that space myBoks[0] = new Boks();. I very much like the streamlining I see here from Carey and Fred, though some of the code is over my head at this point --- but I'll get there.

Kind regards --- Nick (I'll be back!)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can avoid the nulls in that array with an array initialiser.If you declare and initialise the array together you may omit
new Book[]
That may become tedious to write for a 1000000‑element array. I wonder whether you can write a Stream which will create a 1000000‑element Book array.Stream doesn't seem to have an of method which doesn't take any arguments, so let's try an int stream.There you go, a 1000000‑element array. and some pretty crappy code depending on it having the appropriate constructor. You create an IntStream with its range method (note there is another method similar to range). That creates a stream which iterates the numbers 0...999999, and I am splitting it into multiple streams because parallel execution is almost always much quicker for large streams. Each iteration takes its int and applies it to the Foo constructor.

[edit]Add a bit more: In line 3 you change the int stream for a Stream<Foo>. You can either take the int, call it i, and apply it to the Foo constructor with new Foo(i), OR, you can simply pass the Foo constructor to the method call. There isn't a keyword for constructor, so they reused an old keyword after two colons. It appears that new is the obvioius candidate for a keyword for constructors. Later on you can create an array but you must supply something which supplies a type and consumes the size of the stream. If you tell it to create a Foo‑array constructor with Foo[]::new, that will do nicely. That last call also un‑parallelises the Stream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!