🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #771

Re: making sense of runtime error

#771
From: Paul Furbacher [TeamB]
Date: Wed, 22 Dec 2004 14:15
54 lines
2569 bytes
Alan Shiers wrote:

> I'm working on a project where I'm trying to save to file all the objects
> contained in a class that extends JPanel and implements Serializable.  When
> I attempt the save using the following code:

Have you stepped through that code in the debugger?
Where does it go off the tracks?

You are trying to serialize an object which
may not exist: is "docPanel" null?

> FileOutputStream fStream = null;
> ObjectOutputStream stream = null;
> [...]
> catch(Exception ex)
> {
>     ex.printStackTrace();
>     try{stream.close();fStream.close();}

In general, check variable for whether they
are not null before invoking methods on them:

     try {
      if ( stream != null )
        stream.close();
      ....

This motif is very commonly used in books
and demos.

A minor point, but you are closing the streams
in two places.

Finally, it's more proper to close them in a
*finally* block.  Again, this is an oft-recurring
motif of a type that should be picked up by
extensive reading of books and demo code.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Technical Details
Message-ID: <41c98187$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41c887e9$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,a48625165a64e13d
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Wed, 22 Dec 2004 09:15:36 -0500
From: "Paul Furbacher [TeamB]" <pfurbacher@mac.com>
Organization: Reverse Alchemy, Inc.
User-Agent: Mozilla Thunderbird 0.8 (Macintosh/20040913)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: making sense of runtime error
References: <41c887e9$1@newsgroups.borland.com>
In-Reply-To: <41c887e9$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.142.222.65
Message-ID: <41c98187$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1103724935 69.142.222.65 (22 Dec 2004 06:15:35 -0800)
Lines: 54
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail