ActionListener issues

Soldato
Joined
23 Oct 2002
Posts
4,098
Location
_
Hi guys, wondering if you could solve this issue for me. Basically, I've got a void init() method that's initialising all my GUI items, packing them and setting visible to true. It's called when my main method instantiates itself and calls that method. I've added an actionlistener to a button within the innit method like so:

Code:
JButton button = new JButton("Post");
button.addActionListener(new ActionListener () {
	public void actionPerformed(ActionEvent ev) {

	}
});

Trouble is, I can't do anything with it!

It'll do a basic System.out.println(""), but it won't do what I want it to do, which is to call a method that'll do some stuff. It won't even reference any global variables.

It just says:

Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at multicasting.Client.doStuff(Client.java:136)
	at multicasting.Client$1.actionPerformed(Client.java:39)
	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)

Any tips on how I can get this method to just call something?

I've bunged in the relevant code below. Any help's extremely appreciated.

Code:
	public void init () {
		JButton button = new JButton("Post");
		button.addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent ev) {
				doStuff();
			}
		});
				
		JTextArea textArea = new JTextArea();
		JTextField textField = new JTextField();
		JScrollPane scroll = new JScrollPane(textArea);
		JPanel mainPanel = new JPanel();
		JPanel secPanel = new JPanel();
		JPanel triPanel = new JPanel();
		JFrame f = new JFrame();
		
		textArea.setColumns(35);
		textArea.append("Login first");
		textArea.setRows(5);
		textArea.setWrapStyleWord(true);
		
		textField.setColumns(35);		
				
		mainPanel.add(scroll, BorderLayout.NORTH);
		secPanel.add(textField, java.awt.BorderLayout.SOUTH);
		triPanel.add(button,BorderLayout.SOUTH);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(triPanel, java.awt.BorderLayout.SOUTH);
		f.getContentPane().add(mainPanel, java.awt.BorderLayout.NORTH);
		f.getContentPane().add(secPanel, BorderLayout.CENTER);

		f.setSize(400,200);
		f.setVisible(true);
	}

	public static void main(String args[]) {
		Client c = new Client();
		c.init();
		c.multicasting();
	}

	public void doStuff() {
		String userInput = textField.getText();
		
		if (!loggedIn) {
			msgFlag = 2;
		}
		else {
			msgFlag = 3;
		}
		
		if (!message.equals("")){
			multicastSend(constructByte(message, msgFlag, '0', 1), myMulticast);
		}
		else {
			textArea.setText("No text entered.");
		}
	}
}
 
Back
Top Bottom