JTextArea

B

Battler

Hi

Is there a way of checking if a JTextArea holds any text without using the
getText() method, which throws a null pointer exception if the text area is
null.

I do not want to catch any exceptions for checking the text area because
this will interrupt the flow of the program.

Battler
 
A

Andrew Thompson

Is there a way of checking if a JTextArea holds any text without using the
getText() method, which throws a null pointer exception if the text area is
null.

Whoever told you that, and did you test it?

A TextArea can itself be null, after it
is declared, but before it is instantiated,
but the default constructor "Constructs a new
text area with the empty string as text."

Note there is a better group for GUI questions
<http://www.physci.org/codes/javafaq.jsp#cljg>
or perhaps, with your seeming inexperience with
JavaDocs and basic testing, you might be better at..
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
A

Andrew Thompson

Is there a way of checking if a JTextArea

Or rather, since I was talking about 'TextArea'

simply do..
JTextArea ta = new JTextArea();

.....

if (ta.getText()!=null) {
....
}

[ Another reason you should be looking to the
GUI group, ..the GUI Guru's know off by heart
that the default constructors of TextArea and
JTextArea differ.. (Strides off ..whistling
loudly.) ] ;-)
 
B

Battler

A TextArea can itself be null, after it
is declared, but before it is instantiated,
but the default constructor "Constructs a new
text area with the empty string as text."

Well I meant the underlying document. Sometimes you need to read between the
lines.

Let me rephrase:
Is there a way of checking if a JTextArea holds any text without using the
getText() method, which throws a null pointer exception if the text area
holds no text?
getText
public String getText()
Returns the text contained in this TextComponent. If the underlying
document is null, will give a NullPointerException.

Returns:
the text
Throws:
NullPointerException - if the document is null
See Also:
setText(java.lang.String)


Did you actually answer the question? hmm... you seem to have lost the plot.

Thanks anyway.

Battler
 
B

Battler

Andrew Thompson said:
Or rather, since I was talking about 'TextArea'

simply do..
JTextArea ta = new JTextArea();

....

if (ta.getText()!=null) {
....
}

But i want to test when it is null without having to catch the null pointer
exception.

I guess I could use what you have and add an else statement to capture what
I want. It should work I think.

Thanks

Battler
 
A

Andrew Thompson

But i want to test when it is null without having to catch the null pointer
exception.

<sscce>
import javax.swing.*;

public class TestTextArea {

public static void main(String args[]) {
JFrame f = new JFrame();
JTextArea ta = new JTextArea();
f.getContentPane().add(ta);
f.validate();
f.setSize(f.getPreferredSize());
f.setVisible(true);

if ( ta.getText()!=null ) {
System.out.println("TextArea is null!");
} else {
System.out.println("TextArea is '" + ta.getText() + "'" );
}
}
}
</sscce>
 
A

Andrew Thompson

....
But i want to test when it is null without having to catch the null pointer
exception.

I think we both went wrong on this one, I
whipped up an example to check some things,..
(ahem..) ignore the earlier one.
<sscce>
import javax.swing.*;

public class TestTextArea {

public static void main(String args[]) {
JFrame f = new JFrame();
String s = null;
JTextArea ta = new JTextArea(s);
f.getContentPane().add(ta);
f.validate();
f.setSize(f.getPreferredSize());
f.setVisible(true);

if ( ta.getText()==null ) {
System.out.println("TextArea is null!");
} else {
System.out.println("TextArea is '" + ta.getText() + "'" );
}

ta.setText(null);
if ( ta.getText()==null ) {
System.out.println("TextArea is null!");
} else {
System.out.println("TextArea is '" + ta.getText() + "'" );
}
}
}
</sscce>

Even when instantiated with a null string, then
set to a bull string, the values returned were ..
TextArea is ''
TextArea is ''
both times, using Java 1.5.

So *I* delved back into the JDocs...

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextArea.html#JTextArea()>
"Constructs a new TextArea. A default model is set,
the initial string is null, and rows/columns are set to 0. "

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#getText()>
"If the underlying document is null, will give a NullPointerException."

Note that it says if the underlying *document* is 'null',
that corresponds to the *model*, not the text itself..

Apparently, Sun has decided that even if the String
contained in the JTextArea is null, it will return
a 0 length String.

That is as far as I understand it at the moment.

BTW. To demonstrate teting for null..
<sscce>
public class TestNull {

public static void main(String args[]) {

String s = null;

if ( s==null ) {
System.out.println("String is null!");
} else {
System.out.println("String is '" + s + "'" );
}
}
}
</sscce>

No exception needs to be thrown, and nobody
needs to get nailed to anything.

HTH
 
S

Sebastian Scheid

Battler said:
Hi

Is there a way of checking if a JTextArea holds any text without using the
getText() method, which throws a null pointer exception if the text area is
null.

You mean, if the variable which shoulds hold the texarea is null? Then the
textarea doesn't exist! So it cannot contain any text!
I do not want to catch any exceptions for checking the text area because
this will interrupt the flow of the program.

??? Explain that please.

Sebastian
 
B

Battler

Andrew Thompson said:
...
But i want to test when it is null without having to catch the null pointer
exception.

I think we both went wrong on this one, I
whipped up an example to check some things,..
(ahem..) ignore the earlier one.
<sscce>
import javax.swing.*;

public class TestTextArea {

public static void main(String args[]) {
JFrame f = new JFrame();
String s = null;
JTextArea ta = new JTextArea(s);
f.getContentPane().add(ta);
f.validate();
f.setSize(f.getPreferredSize());
f.setVisible(true);
if ( ta.getText()==null ) {
System.out.println("TextArea is null!");
} else {
System.out.println("TextArea is '" + ta.getText() + "'" );
}

ta.setText(null);
if ( ta.getText()==null ) {
System.out.println("TextArea is null!");
} else {
System.out.println("TextArea is '" + ta.getText() + "'" );
}
}
}
</sscce>

Even when instantiated with a null string, then
set to a bull string, the values returned were ..
TextArea is ''
TextArea is ''
both times, using Java 1.5.

So *I* delved back into the JDocs...

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextArea.html#JTextAre
a()>
"Constructs a new TextArea. A default model is set,
the initial string is null, and rows/columns are set to 0. "

<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.htm
l#getText()>
"If the underlying document is null, will give a NullPointerException."

Note that it says if the underlying *document* is 'null',
that corresponds to the *model*, not the text itself..

I thought underyling document referred to the text inside the textarea.

It doesnt make sense that a getText() method does not refer to the text
inside the textarea, but the modal? I wonder if its even possible to throw
such an exception.
Apparently, Sun has decided that even if the String
contained in the JTextArea is null, it will return
a 0 length String.

getText() will return 0 if it is null or a null reference? you didn't seem
to be clear on that.

I guess I can saftely assume no nullpointerexception is thrown.
 
A

Andrew Thompson

On Mon, 16 Aug 2004 02:51:29 GMT, Battler wrote:
(A.T.)
getText() will return 0 if it is null or a null reference? you didn't seem
to be clear on that.

....try the code sample I supplied at the top
of the message, my report is based on that.
I guess I can saftely assume no nullpointerexception is thrown.

'assume'?? That is what threw me at first*,
why are you assuming anything, given it is relatively
easy to test these sorts of questions with code?

* That's my excuse and I'm sticking to it.. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top