The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by dmullens]

Hi,

I was getting a null pointer exception on this section of code (Page 76):

if (children.size() == 0) {
out.write("/>"smilie;
out.flush();
return;
}

I checked and the variable children was null. I changed it to:

if (children == null || children.size() == 0) {
out.write("/>"smilie;
out.flush();
return;
}

Which seems to work, but I didn't know if children should be null there or
not...I might have typed something else in wrong.

I was using the example from the book under "testing the server" which doesn't
seem to contain any children.

- Dave
import-bot (20211) [Avatar] Offline
#2
Re: writeXML method
[Originally posted by iainshigeoka]

> I was getting a null pointer exception on this section of code (Page 76):
>
> if (children.size() == 0) {
> out.write("/>"smilie;
> out.flush();
> return;
> }

Hi,

I think something must have been typed in wrong. children should always be
non-null. It is a class member variable that is initialized with a LinkedList
in the class initializer (where it is declared). children may be empty
(children.size() == 0) but it should never be null. To save space, some of
the code in the book doesn't contain the entire class (mainly where I cover a
class that has already been shown previously and I just include the changed
methods).

If you haven't already, try downloading the source code from the book website
at manning.com and compare what you've typed in with what I have there. I'm
guessing some minor difference so a "diff" tool may help. Or you can just use
my version.

let me know if you have any other problems.

-iain
import-bot (20211) [Avatar] Offline
#3
Re: writeXML method
[Originally posted by dmullens]

I have downloaded the source code, but some of it is different...for example,
this is from the Session class from the downloaded source (for chapter 3):

Reader in;
public Reader getReader() throws IOException {
if (in == null){
in = new XercesReader(sock.getInputStream());
}
return in;
}

This is from the Session class in chapter 3:

public Reader getReader() throws IOException {
if (_in == null) {
_in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
}
return _in;
}

I have already written a _very_ crude client before I got the book....My main
trouble was getting the packets parsed right, so your book has been a big help
to me already! (Thanks) Right now I want to make sure I understand the way
you have things setup in the book, however, I do look at the downloaded source
too.

I'll take a look at where that variable is declared. I bet I didn't set it to
anything.

Thanks for the response and the work you put into the book!

Dave.


> > I was getting a null pointer exception on this section of code (Page 76):
> >
> > if (children.size() == 0) {
> > out.write("/>"smilie;
> > out.flush();
> > return;
> > }
>
> Hi,
>
> I think something must have been typed in wrong. children should always be
> non-null. It is a class member variable that is initialized with a LinkedList
> in the class initializer (where it is declared). children may be empty
> (children.size() == 0) but it should never be null. To save space, some of
> the code in the book doesn't contain the entire class (mainly where I cover a
> class that has already been shown previously and I just include the changed
> methods).
>
> If you haven't already, try downloading the source code from the book website
> at manning.com and compare what you've typed in with what I have there. I'm
> guessing some minor difference so a "diff" tool may help. Or you can just use
> my version.
>
> let me know if you have any other problems.
>
> -iain
import-bot (20211) [Avatar] Offline
#4
Re: writeXML method
[Originally posted by iainshigeoka]

> I have downloaded the source code, but some of it is different...for example,
> this is from the Session class from the downloaded source (for chapter 3):
>
> Reader in;
> public Reader getReader() throws IOException {
> if (in == null){
> in = new XercesReader(sock.getInputStream());
> }
> return in;
> }
>
> This is from the Session class in chapter 3:
>
> public Reader getReader() throws IOException {
> if (_in == null) {
> _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
> }
> return _in;
> }

Yes. The online source has been modified in several ways. Primarily the
changes were bug fixes and other small changes that crept in between the time
we froze the book text and the time I posted the source to the website. We
were in a tight deadline to get the book completed for Java One so I was still
creating code while writing the book. And some of it worked fine in my
mini-tests but didn't work when integrated into the whole source base... ;(

I was also struggling with balancing the book source format so it was as clear
as possible (as an educational tool) vs. making it semi-realistic and
functional. In many cases, I sided on more pseudo code style Java in the book
to emphasize what was being discussed. These simplified code snippets were
beefed up a bit in the online source so it would work a little more robustly
(although the code is still largely untested and 'brittle').

The most notable online source code feature not in the book is forcing the
Xerces parser to handle streaming XML using a modified stream class (the code
is adding extra white space into the XML stream).

The most brittle code is the roster code which was hastily ported from a
version that relied on a database to the book version that uses hashtables
(and I think still has some lingering bugs in it).

> I have already written a _very_ crude client before I got the book....My main
> trouble was getting the packets parsed right, so your book has been a big help
> to me already! (Thanks) Right now I want to make sure I understand the way
> you have things setup in the book, however, I do look at the downloaded source
> too.

I'm glad it has helped you out. Happy Jabbering!

-iain