0
package edu.bsu.cs121.mamurphy;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import java.awt.event.*;

public class InClass1Main extends JFrame {

    JButton button1;
    JTextField textField1;
    JTextArea textArea1;
    int buttonClicked;

    public static void main(String[] args) {
        new InClass1Main();
    }

    public InClass1Main() {

        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension dim = tk.getScreenSize();
        this.setSize((dim.width / 2), (dim.height / 2));
        int xPos = (dim.width / 2) - (this.getWidth() / 2);
        int yPos = (dim.height / 2) - (this.getHeight() / 2);

        JPanel panel = new JPanel();
        JLabel label1 = new JLabel("Translation Panel");
        JButton button1 = new JButton("Transfer");

        ListenForButton button = new ListenForButton();

        button1.addActionListener(button);

        JTextField textField1 = new JTextField();
        JTextArea textArea1 = new JTextArea(10, 50);
        JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        textField1.setColumns(50);

        textArea1.setLineWrap(true);
        textArea1.setWrapStyleWord(true);

        panel.add(label1);
        panel.add(button1);
        panel.add(textField1);
        panel.add(scrollBar1);

        this.add(panel);
        this.setLocation(xPos, yPos);
        this.setResizable(false);
        this.setTitle("Translation Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        textField1.requestFocus();

    }

    private class ListenForButton implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            textArea1.setText(textField1.getText());
        }
    }
}

I am now getting an error whenever I try to actually type in text to the text field and then put it in the text area. I can't make any sense what so ever of the error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at edu.bsu.cs121.mamurphy.InClass1Main$ListenForButton.actionPerformed(InClass1Main.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I truly have no idea what caused this error. A lot of the reasons it is giving me (as you can see above) is (Unknown Source). So any help as to why this is happening would be greatly appreciated.

  • The error originates from *"edu.bsu.cs121.mamurphy.InClass1Main$ListenForButton.actionPerformed(InClass1Main.java:66)"* because you are shadowing your variables. See ControlAltDel's answer for more specific details – MadProgrammer Nov 23 '15 at 21:54

4 Answers4

2

Remove the addActionListener statement from the body of ListenForButton

private class ListenForButton implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent ae) {
        textArea1.setText(textField1.getText());
    }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • So that did fix all of my errors, but now when I try and actually run my program to see if it actually works, I type in text and then I get a huge error in my code. I will add the error I get in an edit, as well as my updated code above. – MoeMarmalade Nov 23 '15 at 15:11
  • different issue: youre shadowing `JTextArea` and a bunch of other controls. Replace `JTextArea textArea1 = new JTextArea(...)` with `textArea1 = new JTextArea(...)`, etc... – Reimeus Nov 23 '15 at 15:16
1

You've got a problem in your constructor in these lines:

    JTextField textField1 = new JTextField();
    JTextArea textArea1 = new JTextArea(10, 50);

What you probably want is

    textField1 = new JTextField();
    textArea1 = new JTextArea(10, 50);

This will set your object members, rather than putting them in method local variables.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Here is you complete solution...

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class InClass1Main extends JFrame{
        JButton button1;
        public static JTextField textField1;
        public static JTextArea textArea1;
        int buttonClicked;

        public static void main(String[] args) {
            new InClass1Main();
        }

        public InClass1Main() {

            Toolkit tk = Toolkit.getDefaultToolkit();

            Dimension dim = tk.getScreenSize();
            this.setSize((dim.width / 2), (dim.height / 2));
            int xPos = (dim.width / 2) - (this.getWidth() / 2);
            int yPos = (dim.height / 2) - (this.getHeight() / 2);

            JPanel panel = new JPanel();
            JLabel label1 = new JLabel("Translation Panel");
            JButton button1 = new JButton("Transfer");

            ListenForButton button = new ListenForButton();

            button1.addActionListener(button);

            textField1 = new JTextField();
            textArea1 = new JTextArea(10, 50);
            JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            textField1.setColumns(50);

            textArea1.setLineWrap(true);
            textArea1.setWrapStyleWord(true);

            panel.add(label1);
            panel.add(button1);
            panel.add(textField1);
            panel.add(scrollBar1);

            this.add(panel);
            this.setLocation(xPos, yPos);
            this.setResizable(false);
            this.setTitle("Translation Frame");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);

            textField1.requestFocus();

        }


}

class ListenForButton implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Hello");
        System.out.println(InClass1Main.textField1.getText());
        System.out.println(InClass1Main.textArea1.getText());
        InClass1Main.textArea1.setText(InClass1Main.textField1.getText());
    }
}
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
-1

This will solve the issues in your code :

    public class InClass1Main extends JFrame {

    JTextField textField1;
    JTextArea textArea1;
    int buttonClicked;

    public static void main(String[] args) {
        new InClass1Main();
    }

    public InClass1Main() {

        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension dim = tk.getScreenSize();
        this.setSize((dim.width / 2), (dim.height / 2));
        int xPos = (dim.width / 2) - (this.getWidth() / 2);
        int yPos = (dim.height / 2) - (this.getHeight() / 2);

        JPanel panel = new JPanel();
        JLabel label1 = new JLabel("Translation Panel");
        JButton button1 = new JButton("Transfer");

        ListenForButton button = new ListenForButton();

        button1.addActionListener(button);

        textField1 = new JTextField();//no need to declare any other JTextField object just initialize the above crated 
        textArea1 = new JTextArea(10, 50);//same here
        JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        textField1.setColumns(50);

        int numOfLines = textArea1.getLineCount();

        textArea1.setLineWrap(true);
        textArea1.setWrapStyleWord(true);
        textArea1.append(" Number of lines: " + numOfLines);

        panel.add(label1);
        panel.add(button1);
        panel.add(textField1);
        panel.add(scrollBar1);

        this.add(panel);
        this.setLocation(xPos, yPos);
        this.setResizable(false);
        this.setTitle("Translation Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        textField1.requestFocus();

    }

    private class ListenForButton implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
        new InClass1Main().textArea1.setText(textField1.getText());
        new InClass1Main().textField1.setText(null);
        }

    }
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • Thanks a lot. This actually solves all of the problems I was having. – MoeMarmalade Nov 23 '15 at 15:16
  • I would like to ask though. How can I make it so that the text in the text field goes away, and the text in the text area stay? So for example, you type in `This is a test.` So that goes in the text field. Then you type in `This is the next text.` and that text also goes in the text field, but doesn't overwrite what was put in previously? – MoeMarmalade Nov 23 '15 at 15:19
  • I updated the code .. check it. – Madushan Perera Nov 23 '15 at 15:20
  • how textArea1 as well as textField1 is accessible while it is totally out of scope. It's not Valid code. – Vishal Gajera Nov 23 '15 at 15:21