• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having trouble appending text to a textarea, need helpful advice

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you confirmed that the text area is not null?

I shall move this discussion to our GUI forum.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe McTigue
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic