How do I catch the input from JTextfield?

I want to be able to input delaytime for the buffer, but all I get is a lot of red text in my console window. The "actionPerformed" is at the bottom and I added a listener in

JButton bufferButton = new JButton("Set buffer-time");

bufferButton.addActionListener(this);

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.WindowAdapter;

import java.util.Scanner;

import javax.swing.*;

import javax.swing.Timer;

import java.util.*;

import java.awt.event.*;

import javax.swing.border.*;

/**

* @author

*

*/

publicclass Guiextends JFrameimplements ActionListener

{

public Consumer c1;

public Consumer c2;

public Consumer c3;

public Producer p1;

public Producer p2;

public Producer p3;

public Buffer buffer;

private JPanel buttonPanel, producerPanel, consumerPanel, bufferPanel,

consumerButtonpanel, producerButtonpanel, bufferButtonpanel;

private Container container;

private JLabel producerLabel =new JLabel("Producer is not producing.");

private JLabel consumerLabel =new JLabel("Consumer is not consuming.");

private JLabel bufferLabel =new JLabel("Buffer is not working.");

private JLabel c1Label =new JLabel("Consumer 1");

private JLabel c2Label =new JLabel("Consumer 2");

private JLabel c3Label =new JLabel("Consumer 3");

private JLabel p1Label =new JLabel("Producer 1");

private JLabel p2Label =new JLabel("Producer 2");

private JLabel p3Label =new JLabel("Producer 3");

private JLabel bLabel =new JLabel("Buffer contains");

private JLabel fillOut1 =new JLabel(" ");

private JLabel fillOut2=new JLabel(" ");

private JLabel fillOut3 =new JLabel(" ");

public JTextField getBufferTime;

/**

*

*/

public Gui(Buffer buffer, Consumer c1, Consumer c2, Consumer c3, Producer p1, Producer p2, Producer p3)

{

this.buffer = buffer;

this.c1 = c1;

this.c2 = c2;

this.c3 = c3;

this.p1 = p1;

this.p2 = p2;

this.p3 = p3;

setLayout(new FlowLayout());

getContentPane().setBackground(Color.lightGray);

container = getContentPane();

container.setLayout(new GridLayout(1,3,1,2));

JTextField consumer1 =new JTextField(2);

JTextField consumer2 =new JTextField(2);

JTextField consumer3 =new JTextField(2);

JTextField bufferTextfield =new JTextField(2);

JTextField producer1 =new JTextField(2);

JTextField producer2 =new JTextField(2);

JTextField producer3 =new JTextField(2);

JTextField getBufferTime =new JTextField(2);

JTextField getConsumerTime =new JTextField(2);

JTextField getProducerTime =new JTextField(2);

JButton consumeButton =new JButton("Set consume-time");

JButton produceButton =new JButton("Set produce-time");

JButton bufferButton =new JButton("Set buffer-time");

bufferButton.addActionListener(this);

producerPanel=new JPanel(new GridLayout(3,0));

consumerPanel=new JPanel(new GridLayout(3,0));

bufferPanel=new JPanel(new GridLayout(2,0));

consumerButtonpanel =new JPanel(new GridLayout());

producerButtonpanel =new JPanel(new GridLayout());

bufferButtonpanel =new JPanel(new GridLayout());

producerPanel.setPreferredSize(new Dimension(300,400));

consumerPanel.setPreferredSize(new Dimension(300,500));

bufferPanel.setPreferredSize(new Dimension(100,100));

consumerButtonpanel.setPreferredSize(new Dimension(50,50));

producerButtonpanel.setPreferredSize(new Dimension(50,50));

bufferButtonpanel.setPreferredSize(new Dimension(50,50));

container.add(producerPanel,BorderLayout.WEST);

container.add(bufferPanel,BorderLayout.CENTER);

container.add(consumerPanel,BorderLayout.EAST);

consumerPanel.add(consumerButtonpanel);

producerPanel.add(producerButtonpanel);

bufferPanel.add(bufferButtonpanel);

consumerPanel.setLayout(new BoxLayout(consumerPanel,BoxLayout.Y_AXIS));

consumerPanel.setBorder(new LineBorder(Color.BLACK,2));

consumerPanel.add(consumerLabel);

consumerPanel.add(fillOut1);

consumerPanel.add(c1Label);

consumerPanel.add(consumer1);

consumerPanel.add(c2Label);

consumerPanel.add(consumer2);

consumerPanel.add(c3Label);

consumerPanel.add(consumer3);

consumerPanel.setBackground(Color.LIGHT_GRAY);

consumerPanel.add(consumeButton);

bufferPanel.setLayout(new BoxLayout(bufferPanel,BoxLayout.Y_AXIS));

bufferPanel.setBorder(new LineBorder(Color.BLACK,2));

bufferPanel.add(bufferLabel);

bufferPanel.add(fillOut2);

bufferPanel.add(bLabel);

bufferPanel.add(bufferTextfield);

bufferPanel.setBackground(Color.LIGHT_GRAY);

bufferPanel.add(bufferButton);

producerPanel.setLayout(new BoxLayout(producerPanel,BoxLayout.Y_AXIS));

producerPanel.setBorder(new LineBorder(Color.BLACK,2));

producerPanel.add(producerLabel);

producerPanel.add(fillOut3);

producerPanel.add(p1Label);

producerPanel.add(producer1);

producerPanel.add(p2Label);

producerPanel.add(producer2);

producerPanel.add(p3Label);

producerPanel.add(producer3);

producerPanel.add(produceButton);

producerPanel.setBackground(Color.LIGHT_GRAY);

consumerButtonpanel.add(consumeButton);

consumerButtonpanel.add(getConsumerTime);

bufferButtonpanel.add(bufferButton);

bufferButtonpanel.add(getBufferTime);

producerButtonpanel.add(produceButton);

producerButtonpanel.add(getProducerTime);

pack();

setResizable(false);

setVisible(true);

setSize(910, 500);

setLocation(100, 100);

setTitle("Producer-Consumer");

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

//public void actionPerformed(ActionEvent e) {

//Scanner sc = new Scanner(getBufferTime.getSelectedText());

//long temp = (sc.nextLong());

//}

publicvoid actionPerformed(ActionEvent e){

int delay = Integer.decode(getBufferTime.getText());

System.out.println(delay);

}

}

[10343 byte] By [_Robb_] at [2007-11-26 12:17:47]
# 1
Hey there,Well, try to use Integer.parseInt(JTextfield.getText())instead of "Integer.decode()"
Thunder_Blast at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 2
decode should also work
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 3
@Op. Can you post the error message and stacktrace?
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 4
Both methods (parseInt and decode) throw a NumberFormatException for unparseable data, such as the empty String "" ... So wrap the parsing line in a try-catch.
quitte at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 5

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at Gui.actionPerformed(Gui.java:183)

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.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

_Robb_ at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 6
getBufferTime is null.
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 7

..or, something is very fishy do you get that when your actionPerformed looks like this:

public void actionPerformed(ActionEvent e) {

int delay = Integer.decode(getBufferTime.getText());

System.out.println(delay);

}

The only thing which can get nullpointer there is if getBufferTime is null, but getBufferTime is set in the constructor.

kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 8
In the constructor, getBufferTime is a local variable, so the initialisation does not refer to the textfield attribute having the same name. The latter one is still null when actionPerformed tries to access it ...JTextField getBufferTime = new JTextField(2);
quitte at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 9
What's on line 183?
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 10
line 183: int delay = Integer.decode(getBufferTime.getText());
_Robb_ at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 11
You have this in your constructor:JTextField getBufferTime = new JTextField(2);It should only be:getBufferTime = new JTextField(2);since getBufferTime is an attribute. It will work after you have made that change.Kaj
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 12
(The problem was that you both declared it to be an attribute and a local variable)
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 13

I've tried with both of theese decode/parseInt

public void actionPerformed(ActionEvent e) {

int delay = Integer.decode(getBufferTime.getText());

//int delay = Integer.parseInt(getBufferTime.getText());

System.out.println(delay);

changed to getBufferTime = new JTextField(2);

and bufferPanel.add(bufferLabel);

Now the bufferwindow is huge, the textfield below has dissapeared. And I also get

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.valueOf(Unknown Source)

at java.lang.Integer.decode(Unknown Source)

at Gui.actionPerformed(Gui.java:183)

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.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

_Robb_ at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 14
That's because you are trying to parse an empty string. An empty string is not a number. Kaj
kajbj at 2007-7-7 14:55:46 > top of Java-index,Archived Forums,Socket Programming...
# 15

Do this:

public void actionPerformed(ActionEvent e) {

String data = getBufferTime.getText().trim();

if (data.length() == 0) {

//Handle empty string, or just return;

return;

}

int delay = Integer.decode();

System.out.println(delay);

}

Kaj

kajbj at 2007-7-7 14:55:48 > top of Java-index,Archived Forums,Socket Programming...
# 16
Ok, my bad. I changed the wrong one. Now it works!!Thank you all!
_Robb_ at 2007-7-7 14:55:48 > top of Java-index,Archived Forums,Socket Programming...