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.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
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.
All things are lawful, but not all things are profitable.
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)
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.
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, but that objects must then be created to fill that space myBoks = 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.
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.
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.