> Can someone tell me what I'm doing wrong or point me to help towards a
> solution. Thanks.
> //Declare an array "doc"
> DefaultStyledDocument[] doc;
>
> //Instantiate and array "doc"
> DefaultStyledDocument[] doc = new DefaultStyledDocument[5];
You have declared the variable doc twice. The following is correct:
//Declare and instantiate an array "doc"
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];
or also correct:
//Declare an array "doc"
DefaultStyledDocument[] doc;
//Instantiate the array "doc"
doc = new DefaultStyledDocument[5];
I think (the error message of the compiler is a bit puzzling) this will
solve your problem.
Cheers,
Peter
Ed - 12 Jun 2006 19:08 GMT
Thanks. Peter - I understood that in addition to declaring the array I had
to initialize each element also. In an earlier version of my code when I had
only the code you suggest the compiler objected, saying that the array "may
not be initialized" That is why I added the five lines referring to each
element.
>> Can someone tell me what I'm doing wrong or point me to help towards a
>> solution. Thanks.
[quoted text clipped - 23 lines]
> Cheers,
> Peter
Oliver Wong - 12 Jun 2006 19:16 GMT
> Thanks. Peter - I understood that in addition to declaring the array I had
> to initialize each element also. In an earlier version of my code when I
> had only the code you suggest the compiler objected, saying that the array
> "may not be initialized" That is why I added the five lines referring to
> each element.
That's not the chance that Peter is talking about. Given explicitly,
here's what your code should look like:
<code>
class PublishStats extends JFrame
{
JFrame f = new JFrame("BJ");
StyleContext sc = new StyleContext();
//Declare an array "doc" and
//Instantiate it
DefaultStyledDocument[] doc = new DefaultStyledDocument[5];
//Initialize each element of array "doc"
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
</code>
- Oliver
> Using Java 5.0
> I'm trying to prepare a DefaultStyledDocument array so that I can then
[quoted text clipped - 24 lines]
> doc[3] = new DefaultStyledDocument(sc);
> doc[4] = new DefaultStyledDocument(sc);
Besides the double declaration of doc that Peter mentioned, you're also placing the
individual assignments incorrectly, causing the compiler error. Assignment statements
(versus declaration statements) must be placed inside a method or an initializer. You
can specify an instance initializer by placing braces around the last sequence ...
//Initialize each element of array "doc"
{
doc[0] = new DefaultStyledDocument(sc);
doc[1] = new DefaultStyledDocument(sc);
doc[2] = new DefaultStyledDocument(sc);
doc[3] = new DefaultStyledDocument(sc);
doc[4] = new DefaultStyledDocument(sc);
}
... or in a constructor, or you could do it in the declaration, like so:
DefaultStyledDocument[] doc =
{
//Initialize each element of array "doc"
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
new DefaultStyledDocument(sc),
};

Signature
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
Peter Van Weert - 12 Jun 2006 22:23 GMT
Oops, can't believe I missed that one :-)
Well spotted Lee!
Lee Fesperman schreef:
>> Using Java 5.0
>> I'm trying to prepare a DefaultStyledDocument array so that I can then
[quoted text clipped - 50 lines]
> new DefaultStyledDocument(sc),
> };
Ed - 13 Jun 2006 14:08 GMT
Either putting it inside of curly brackets or in a method works. That was
the problem all along. Thanks to all - you are very helpful.
> Oops, can't believe I missed that one :-)
> Well spotted Lee!
[quoted text clipped - 56 lines]
>> new DefaultStyledDocument(sc),
>> };