File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Having trouble appending text to a textarea, need helpful advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Having trouble appending text to a textarea, need helpful advice" Watch "Having trouble appending text to a textarea, need helpful advice" New topic
Author

Having trouble appending text to a textarea, need helpful advice

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
I keep getting a NullPointerException when trying to append text to my JTextArea.
I've read just about everything I could onine and in multiple books but can't find
a solution. If you find yourself an accomplished Java guru, please read the following
and save me from knocking my head against the wall or scaping this as a java project entirely.

In my buildPanel method I declare the JTextArea:


Then in my buildTextArea method I initialize the JTextArea and add the ScrollPane:


Then in my actionPerformed method I trigger the event of opening my JFileChooser:


Then I open the JFileChooser to browse for a file:


Then I read in the file chosen line by line into an ArrayList<String> followed by calling my method to
append the contents of the ArrayList to the JTextArea:


Here's where I keep hitting walls on solving the issue.
the data from the file is being read and successfully captured into the ArrayList,
and even prints out (for test purposes) from the the following method which
is where it should append the lines to the JTextArea. Instead the line where I
append the text to the JTextArea gives me a NullPointerException:


Here's the console print out:

Browse button was pressed!
size of arraylist: 298
<CustomerCapture>
NullPointerException in jtextarea append-> null
<Customer>
NullPointerException in jtextarea append-> null
<CustomerDate>10/29/2012</CustomerDate>
NullPointerException in jtextarea append-> null
<CustomerTime>2:38:58 PM</CustomerTime>


Any help would be greatly appreciated.
Much thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Have you confirmed that the text area is not null?

I shall move this discussion to our GUI forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
How do you know that buildTextArea method is called before the button is pressed?
Why have you got that method rather than an initGUI method or initialising that exte area in the constructor?
Why are you using _ in variable names?
Why are you using that dreadful design of actionPerformed method?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It might be easier to diagnose if you create an SSCCE (<- link).

But I think the problem is here:

which is part of your readFileIn() method. It creates a new MainPanel and sets the text for the text area contained therein. This is probably not the same panel/text area you created and initialized previously. Instead of creating a new MainPanel, you probably need to get a reference to the one you already have made.


Steve
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi all, thanks for your advice, but I have already solved this delima.
It turned out to be where I was appending to the JTextArea at.
Turns out it wouldn't allow me to run the following code:

anywhere but within the actionPerformed method.
(even though I was calling the method which does this from the actionPerform method.)
wierd huh?

But Anyways...
Thanks for being there just in case.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Joe McTigue wrote:anywhere but within the actionPerformed method.
(even though I was calling the method which does this from the actionPerform method.)
wierd huh?

'Weird' usually just means you don't quite understand the problem. The reason you need to limit the assignment to actionPerformed is probably because you have direct access to the Object which had been initialized. Either way, you should understand the difference between what does work and what does not work, and not just attribute it to 'weirdness' because weird will bite you later on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having trouble appending text to a textarea, need helpful advice