JTextArea

Discussion in 'Java' started by Battler, Aug 15, 2004.

  1. Battler

    Battler Guest

    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
     
    Battler, Aug 15, 2004
    #1
    1. Advertising

  2. On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:

    > 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>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 15, 2004
    #2
    1. Advertising

  3. On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:

    > 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.) ] ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 15, 2004
    #3
  4. Battler

    Battler Guest

    > On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:
    >
    > > 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.

    >
    > 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
     
    Battler, Aug 15, 2004
    #4
  5. Battler

    Battler Guest

    "Andrew Thompson" <> wrote in message
    news:...
    > On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:
    >
    > > 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) {
    > ....
    > }


    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
     
    Battler, Aug 15, 2004
    #5
  6. On Sun, 15 Aug 2004 15:09:38 GMT, Battler wrote:

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

    >
    > 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>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 15, 2004
    #6
  7. On Sun, 15 Aug 2004 15:09:38 GMT, Battler wrote:
    > "Andrew Thompson" <> wrote in message
    >> On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:
    >>
    >>> Is there a way of checking if a JTextArea

    ....
    > 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

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 15, 2004
    #7
  8. "Battler" <> schrieb im Newsbeitrag
    news:MrHTc.58722$...
    > 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
     
    Sebastian Scheid, Aug 16, 2004
    #8
  9. Battler

    Battler Guest

    > > "Andrew Thompson" <> wrote in message
    > >> On Sun, 15 Aug 2004 11:05:16 GMT, Battler wrote:
    > >>
    > >>> Is there a way of checking if a JTextArea

    > ...
    > > 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.




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
     
    Battler, Aug 16, 2004
    #9
  10. On Mon, 16 Aug 2004 02:51:29 GMT, Battler wrote:
    (A.T.)
    >> 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.


    ....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.. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 16, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa

    Tab key on JTextArea

    Ahmed Moustafa, Jun 28, 2003, in forum: Java
    Replies:
    1
    Views:
    2,824
    xarax
    Jun 28, 2003
  2. bad_knee
    Replies:
    0
    Views:
    538
    bad_knee
    Jul 13, 2003
  3. bad_knee
    Replies:
    2
    Views:
    562
    bad_knee
    Jul 14, 2003
  4. Thomas Weidenfeller

    Re: JTextArea text is double-spaced?

    Thomas Weidenfeller, Jul 16, 2003, in forum: Java
    Replies:
    2
    Views:
    459
    Thomas Weidenfeller
    Jul 17, 2003
  5. Brian J. Sayatovic
    Replies:
    0
    Views:
    2,092
    Brian J. Sayatovic
    Sep 7, 2003
Loading...

Share This Page