user3551966
user3551966

Reputation: 11

Java GUI Write In Text Area

I have a project that is due tonight that I have mostly completed but I am having a hard time putting a GUI on it, specifically getting the file printed onto the GUI rather than in the terminal window.

 private JFrame frame;
 private JTextArea area;
 private Font font;
 private Directory directory;
 private String name;
 private String firstName;
 private String lastName;

public GUI()
{
    makeFrame();
    directory = new Directory();
    directory.FileRead();
    String name = (firstName + lastName);
    directory.findPerson(name);
    directory.listDirectory();
    writeTextArea(name);
    font = new Font("Verdana", Font.PLAIN, 10);
 }

private void writeTextArea(String instr)
  {
    area.append(instr);
  }

I keep getting a NullPointerException for area.append(instr);. How do I fix this?

Upvotes: 1

Views: 644

Answers (1)

camickr
camickr

Reputation: 324078

private JTextArea area;

Your text area is null. You need to create an instance of it if you want to use it:

private JTextArea area = new JTextArea(5, 30);

and you also have to add it to the frame it you want to see the text.

I suggest you start with the Swing basics by reading the Swing tutorial. The section on Using Text Components has working examples.

Upvotes: 2

Related Questions