Jump to content
Divine-X

Sending text to JTextArea from outside of GUI help

Recommended Posts

Okay, my problem is that I have GUI and I want to have a method that I can use that'll update the JTextArea with the new string.

But I keep getting this error

[code]
Exception in thread "main" java.lang.NullPointerException
at text.gui.GUI.updateTextArea(GUI.java:138)
at text.game.Test.intro(Test.java:10)
at text.gui.GUI.main(GUI.java:50)
[/code]

Here's all the code I have that involves the text area, am I missing something, I don't know what's wrong.

[code]private JTextArea textArea;[/code]
[code] public static void main(String[] args) {
/*try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
GUI frame = new GUI();
Test.intro();
}
});*/
GUI frame = new GUI();
frame.setVisible(true);
Test.intro();
}[/code]

[code] final JTextArea textArea = new JTextArea();
textArea.setBackground(new Color(176, 224, 230));
scrollPane.setViewportView(textArea);
textArea.setEditable(false);[/code]
[code] textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
inputString = textField.getText();
textArea.append(inputString+"\n");
textField.setText("");
}
}
});[/code]
[code] public void updateTextArea(String message) {
textArea.append(textArea.getText() + message + "\n");
textArea.selectAll();
int x = textArea.getSelectionEnd();
textArea.select(x, x);
}[/code]

[code]public class Test {

static GUI frame = new GUI();

public static void intro() {
frame.updateTextArea("Testing");
}

}[/code]

:confused:

Share this post


Link to post
Share on other sites
Not sure what your 'selection' part is. But this works and you don't need to use KeyListener for this. ActionListener works just the same.
[CODE]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Example
{

private static JFrame frame;

private JTextArea textArea;

private JScrollPane scrollPane;

private JTextField textField;

public static void main(String[] args)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Example().initializeFrame();
frame.setVisible(true);
}

private void initializeFrame()
{
// Set the frame size & resize options
frame.setSize(400, 400);
frame.setResizable(true);
// Initialize Text Area
textArea = new JTextArea();
textArea.setEditable(false);
// Set Background Color of Text Area
textArea.setBackground(new Color(176, 234, 230));
// Initialize Scrolling Pane
scrollPane = new JScrollPane(textArea);
// Add Scroll Pane to Frame
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
// Initialize Text Field
textField = new JTextField();
// Add Text Field to Frame
frame.getContentPane().add(textField, BorderLayout.SOUTH);
// Listen for when Text Field has an Action (ENTER PRESSED)
textField.addActionListener(new ActionListener()
{
[MENTION=15855]Over[/MENTION]ride
public void actionPerformed(ActionEvent e)
{
textArea.append(textField.getText() + "\n");
// Reset Text Field Text
textField.setText("");
}
});
}
}
[/CODE]

[IMG]http://i.imgur.com/rIXUps4.png[/IMG]

Share this post


Link to post
Share on other sites
[quote name='Emily']Not sure what your 'selection' part is. But this works and you don't need to use KeyListener for this. ActionListener works just the same.
[CODE]
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Example
{

private static JFrame frame;

private JTextArea textArea;

private JScrollPane scrollPane;

private JTextField textField;

public static void main(String[] args)
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Example().initializeFrame();
frame.setVisible(true);
}

private void initializeFrame()
{
// Set the frame size & resize options
frame.setSize(400, 400);
frame.setResizable(true);
// Initialize Text Area
textArea = new JTextArea();
textArea.setEditable(false);
// Set Background Color of Text Area
textArea.setBackground(new Color(176, 234, 230));
// Initialize Scrolling Pane
scrollPane = new JScrollPane(textArea);
// Add Scroll Pane to Frame
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
// Initialize Text Field
textField = new JTextField();
// Add Text Field to Frame
frame.getContentPane().add(textField, BorderLayout.SOUTH);
// Listen for when Text Field has an Action (ENTER PRESSED)
textField.addActionListener(new ActionListener()
{
[MENTION=15855]Over[/MENTION]ride
public void actionPerformed(ActionEvent e)
{
textArea.append(textField.getText() + "\n");
// Reset Text Field Text
textField.setText("");
}
});
}
}
[/CODE]

[IMG]http://i.imgur.com/rIXUps4.png[/IMG][/QUOTE]

I'll try this out once I get home, thanks, but it seems to be like my current system right now. I want to send text to the text area by using a method.

Like let's say this
Class A is the GUI and where the method is located, if I try to use my "updateTextArea" method I would get the error above.
And if I try to use the method again but let's say Class B, I would still get the error above.


Sent from my iPod touch using [URL=http://tapatalk.com/m?id=1]Tapatalk[/URL]

Share this post


Link to post
Share on other sites
Your test is called before the text area is created. This is however just a guess as I cannot see the entire code. Edited by Arix

Share this post


Link to post
Share on other sites
[quote name='Arix']Your test is called before the text area is created. This is however just a guess as I cannot see the entire code.[/QUOTE]

I'm activating the GUI first though?


Sent from my iPod touch using [URL=http://tapatalk.com/m?id=1]Tapatalk[/URL]

Share this post


Link to post
Share on other sites
[quote name='Divine-X']I'm activating the GUI first though?


Sent from my iPod touch using [URL=http://tapatalk.com/m?id=1]Tapatalk[/URL][/QUOTE]
Oh yeah I see now, my bad. Why do you create two different GUI objects? It could be that the static GUI frame in Test is still loading? Just reference the GUI from your main method via parameters
[code]
public static void main(String[] args) {
/*try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
GUI frame = new GUI();
Test.intro();
}
});*/
GUI frame = new GUI();
frame.setVisible(true);
Test.intro(frame);
}
[/code]

[code]
public class Test {

public static void intro(GUI frame) {
frame.updateTextArea("Testing");
}

}
[/code]

I highly discourage the use of static if not absolutely necessary.

Share this post


Link to post
Share on other sites
I solved the problem! I don't know what I did but I did! Thanks for your help everyone!

Share this post


Link to post
Share on other sites
Nvm, I thought I fixed it but I didn't. I'm just going to assume that JTextArea's cannot be updated by methods?

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Nvm, I thought I fixed it but I didn't. I'm just going to assume that JTextArea's cannot be updated by methods?[/QUOTE]

Yes they can..your error is stating a NullPointerException. Meaning it isn't/wasn't initialized before the call of the method. You need to make sure you are setting the variable for the text area, as well as calling your update method after the fact. Just post your whole file, I doubt anyone is going to steal your code...

I'll check back later today, I have some Christmas shopping still to do. Look at what I posted as a reference. You can pull this code:
[CODE]
textArea.append(textField.getText() + "\n");
// Reset Text Field Text
textField.setText("");
[/CODE]

Out as a method and replace the function call in the code.

[CODE]
public void setText(String text)
{
textArea.append(text + "\n");
// Reset Text Field Text
textField.setText("");
}
[/CODE]

[CODE]
textField.addActionListener(new ActionListener()
{
[MENTION=15855]Over[/MENTION]ride
public void actionPerformed(ActionEvent e)
{
setText(textField.getText());
}
});
[/CODE]

Share this post


Link to post
Share on other sites
[MENTION=1045]Emily[/MENTION]

If it's because of not being initialized, then all he needs is..

[code]
fieldName = new JTextArea("Words if needed");
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×