• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

null pointer problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
______________________________________________________



_______________________________________________________

 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 8327
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4393
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 73334
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 12994
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 8327
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 732
10
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4393
38
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yet another way
 
Sheriff
Posts: 8012
563
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway, why your Boks fields are public?
 
Campbell Ritchie
Marshal
Posts: 73334
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73334
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic