0

I am working on a program and I am almost done. What I want is: typing a String into a JTextField, press a JButton and I want the String to come out at the other JTextField. So I have: 2 JTextField and 1 Button. Here is my code:

class Fenster extends JFrame {
JTextField inputfield;
JTextField outputfield;
JButton button;
public Fenster() {

    JTextField outputfield = new JTextField();
    outputfield.setBounds(50, 315, 400, 32);
    add(outputfield);

JTextField inputfield = new JTextField();
    inputfield.setBounds(50, 115, 400, 32);
    add(inputfield);
//The Button
JButton button = new JButton("Klick me :D");
    button.setBounds(154, 250, 92, 32);
    button.addActionListener(new buttonlistener());
    add(button);

private class buttonlistener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
String string = inputfield.getText();
outputfield.setText(string);

}
}
}

I know I didn't include the JFrame settings. Please help, cause everytime I press the button the error: "buttonlistener.actionPerformed(Fenster.java:70)" shows up :S

thanks in advance

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
fihdi
  • 145
  • 1
  • 1
  • 12
  • 2
    `buttonlistener.actionPerformed(Fenster.java:70)` There aren't 70 lines of code in the example posted. 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) Always copy/paste error and exception output (the entire stack trace, unless it is larger than the site character limit)! .. – Andrew Thompson May 23 '15 at 14:17
  • 2
    .. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 23 '15 at 14:18
  • 2
    .. Oh, and 5) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson May 23 '15 at 14:21
  • 1
    *"I am working on a program and I am almost done."* Wish I had a dollar for every time I've heard that. ;) – Andrew Thompson May 23 '15 at 14:23

3 Answers3

7

The fields outputfield & inputfield are declared as both attributes of the Fenster class and local variables of the Fenster constructor.

The ones we see on the panel are the ones declared locally, but the action listener is trying to use the ones declared as class attributes that were never initialized. Hence the NullPointerException.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

You can implement the ActionListener directly at the point you initialize it instead of having a separate function for it.

//The Button
JButton button = new JButton("Klick me :D");
button.setBounds(154, 250, 92, 32);
button.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        String string = inputfield.getText();
        outputfield.setText(string);
    }
});
add(button);
DavidN.
  • 125
  • 10
  • 1
    Java 8's lambdas make creating listeners a lot easier: `addActionListener(e -> { ... });` rather than creating an anonymous class – Vince May 23 '15 at 14:42
  • 1
    -1 : These answers, though correct per coding options in Java, very irrelevant from problem stand-point. @Andrew Thompson has given the right answer. – ring bearer May 23 '15 at 14:54
  • omg it works, dude i love youu uhm I mean, Thanks it really helped me out. – fihdi May 23 '15 at 15:06
  • @ringbearer, yes, if you want the full technical detail of why the code in the question doesn't work, then look at the other answer. If you actually want a solution, than I gave that in my answer. – DavidN. May 23 '15 at 15:13
0

I can't decypher exactly what you are asking. So I'm just going to assume your asking


"How can I get my string to output unto a JButton?"

I'm going to give you my code that I used to do just that. Now, I made a string value and you are ready to start coding!:
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.lang.Object;
import java.io.*;
import javax.imageio.*;

public class jframetest extends Object
{
    public static void main(String args[]) throws Exception  
    {

        JFrame frame = new JFrame("Not Main");

                        /* Background Image*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *       JLabel image = new JLabel(new ImageIcon("a.jpg"));        *
 *       image.setBounds(0,0, 800, 600);                           *
 *       frame.getContentPane().add(image, BorderLayout.CENTER);   *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

        Random generate = new Random();
        Random rand = new Random();

        String[] name =    {"Landry", "Azariah", "Oakley", "Lennon", "Charlie", "Skyler", "Dakota", "Armani", "Phoenix" , "Justice", "Casey", "Emory", "Remy", "Emerson", "Amari", "Roxie", "Hayden", "River", "Milan", "Tatum", "Jessie", "Finley", "Riley", "Rowan", "Sage", "Jamie", "Rory", "Harley", "Leighton", "Peyton", "Dallas", "Remington", "Quinn", "Alexis", "Sawyer", "Kamryn", "Parker", "Avery", "Eden", "Lyric", "Elliot", "Reese", "Zion", "Rylan", "Jordan", "Taylor", "Morgan", "Kendall", "Rylee", "Ryan", "Reagan", "Logan", "Hunter", "Carter"};


  int index = (int) (Math.random() * (name.length - 1));

  JButton buttun = new JButton(name[index]);
  buttun.setBounds(190,65,400,50);

  JLabel label2 = new JLabel("Customer: " + name[generate.nextInt(20)]);
  label2.setBounds(190,65,400,50); //setBounds(x,y,width,height);
  label2.setFont(new Font("Serif", Font.BOLD, 40));



  frame.setPreferredSize(new Dimension(200, 200));     
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.getContentPane().add(button); (not needed when Background Image is commented) 
  frame.setVisible(true);
  frame.pack();
  frame.add(buttun);
  frame.add(label2);   
  frame.setSize(800, 600);
  frame.setLayout(null);

   }
}
R1D1CUL3
  • 41
  • 5