4 Replies - 216 Views - Last Post: 14 May 2012 - 05:23 PM Rate Topic: -----

#1 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

GUI for test Sum (4 + 4) producing error which I can't pinpoint

Posted 14 May 2012 - 05:07 PM

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

public class SumGui extends JFrame{
	
	private int sum;
	private JLabel resultLabel;
	private JButton button;
	
	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 300;
	
	
	public SumGui()
	{
		createComponents();
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
	}

	

	class AddAdditionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			sum = 4+4;
			resultLabel.setText("Sum of four plus four :" + sum);
		}
		
	}
		private void createComponents()
		{
			button = new JButton("Click for 4 + 4!");
			ActionListener listener = new AddAdditionListener();
			button.addActionListener(listener);
			
			JLabel resultLabel  =new JLabel("Result");
			JPanel panel = new JPanel();
			panel.add(button);
			panel.add(resultLabel);
			add(panel);
		}
	}



import javax.swing.JFrame;

public class ButtonViewer {

	public static void main(String[] args) {
	
		JFrame frame = new ButtonFrame2();
		frame.setTitle("Button Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}



I created this GUI to produce the output of a Sum method (4+4) in the ActionListener class. But there is an exception at runtime when I actually click the JButton ("Click 4 +4"). I can't pinpoint what is happening because I don't understand this error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SumGui$AddAdditionListener.actionPerformed(SumGui.java:31)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6375)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6140)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4737)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.window.dispatchEventImpl(window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:643)
at java.awt.EventQueue$1.run(EventQueue.java:641)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:657)
at java.awt.EventQueue$2.run(EventQueue.java:655)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Any ideas guys?

Is This A Good Question/Topic? 0
  • +

Replies To: GUI for test Sum (4 + 4) producing error which I can't pinpoint

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: GUI for test Sum (4 + 4) producing error which I can't pinpoint

Posted 14 May 2012 - 05:12 PM

In your createComponents() method, you have this line: JLabel resultLabel =new JLabel("Result");. This creates a new local variable, rather than instantiating the instance variable. Remove the JLabel type to the left of the assignment operator to fix the problem.
Was This Post Helpful? 1
  • +
  • -

#3 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: GUI for test Sum (4 + 4) producing error which I can't pinpoint

Posted 14 May 2012 - 05:13 PM

This is the line in the error you should be looking at:at SumGui$AddAdditionListener.actionPerformed(SumGui.java:31), but I can't figure out why its throwing a NullPionterException
Edit: Look at the guy above me

This post has been edited by natecat: 14 May 2012 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: GUI for test Sum (4 + 4) producing error which I can't pinpoint

Posted 14 May 2012 - 05:20 PM

View Postmacosxnerd101, on 14 May 2012 - 05:12 PM, said:

In your createComponents() method, you have this line: JLabel resultLabel =new JLabel("Result");. This creates a new local variable, rather than instantiating the instance variable. Remove the JLabel type to the left of the assignment operator to fix the problem.


Thank you very much. It works now. I was going to ask a question on the need for the instance variables in this code , but do I need to start a whole new topic for that? Thank you as well to the last chap in this post for helping.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,686
  • Joined: 27-December 08

Re: GUI for test Sum (4 + 4) producing error which I can't pinpoint

Posted 14 May 2012 - 05:23 PM

Yes, you do need an instance variable. Otherwise, the ActionListener won't be able to access the JLabel.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1