Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Simple calculator issue

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Simple calculator issue

    Hello,
    I am a total begginer in JAVA and I could use your help. I am reading a book " Java Programming 24-Hour Trainer by Yakov Fain
    " and I have a problem with creating a calculator.

    here is my class calculator that is only for User Interface:

    package com.PracticalJava.Lesson9;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    import java.awt.*;
     
    public class Calculator {
     
    	private JButton button0;
    	private JButton buttonPoint;
    	private JButton buttonEqual;
    	private JButton buttonSlash;
    	private JButton button7;
    	private JButton button8;
    	private JButton button9;
    	private JButton buttonMultiply;
    	private JButton buttonMinus;
    	private JButton button6;
    	private JButton button5;
    	private JButton button4;
    	private JButton buttonPlus;
    	private JButton button3;
    	private JButton button2;
    	private JButton button1;
    	private JTextField display;
    	JPanel windowContent;
     
    	Calculator ()
    	{
    		windowContent = new JPanel ();
    		GridBagLayout gb = new GridBagLayout ();
    		windowContent.setLayout (gb);
     
    		//RED 0
    		GridBagConstraints red0 = new GridBagConstraints ();
     
    		red0 .gridx=0;
    		red0 .gridy=0;
     
    		red0 .gridheight=1;
    		red0 .gridwidth=4;
     
    		red0 .fill=red0.BOTH;
     
    		red0 .weightx=1.0;
    		red0 .weighty=1.0;
     
    		display = new JTextField ();
     
    		gb.setConstraints(display,red0);
     
    		windowContent.add(display);
     
    		GridBagConstraints red1 = new GridBagConstraints ();
     
    		red1 .gridx=0;
    		red1 .gridy=1;
     
    		red1 .gridheight=1;
    		red1 .gridwidth=1;
     
    		red1 .fill=red1.BOTH;
    		red1 .anchor = red1.CENTER;
     
    		red1 .weightx=1.0;
    		red1 .weighty=1.0;
     
    		button1 = new JButton ("1");
    		button2 = new JButton ("2");
    		button3 = new JButton ("3");
    		buttonPlus = new JButton ("+");
     
    		JPanel p1 = new JPanel ();
    		GridLayout g1 = new GridLayout (1,4);
    		p1.setLayout(g1);
     
    		p1.add(button1);
    		p1.add(button2);
    		p1.add(button3);
    		p1.add(buttonPlus);
     
    		gb.setConstraints(p1, red1);
    		windowContent.add(p1);
     
    		GridBagConstraints red2 = new GridBagConstraints ();
     
    		red2 .gridx=0;
    		red2 .gridy=2;
     
    		red2 .gridheight=1;
    		red2 .gridwidth=1;
     
    		red2 .fill = red2 .BOTH;
    		red2 .anchor = red2.CENTER;
     
    		red2 .weightx=1.0;
    		red2 .weighty=1.0;
     
    		button4 = new JButton ("4");
    		button5 = new JButton ("5");
    		button6 = new JButton ("6");
    		buttonMinus = new JButton ("-");
     
    		JPanel p2 = new JPanel ();
    		p2.setLayout(g1);
     
    		p2 .add(button4);
    		p2 .add(button5);
    		p2 .add(button6);
    		p2 .add(buttonMinus);
     
    		gb.setConstraints (p2,red2);
    		windowContent.add(p2);
     
    		GridBagConstraints red3 = new GridBagConstraints ();
     
    		red3 .gridx=0;
    		red3 .gridy=3;
     
    		red3 .gridheight=1;
    		red3 .gridwidth=1;
     
    		red3 .fill=red3 .BOTH;
    		red3 .anchor = red3.CENTER;
     
    		red3 .weightx=1.0;
    		red3 .weighty=1.0;
     
    		button7 = new JButton ("7");
    		button8 = new JButton ("8");
    		button9 = new JButton ("9");
    		buttonMultiply = new JButton ("*");
     
    		JPanel p3 = new JPanel ();
    		p3.setLayout(g1);
    		p3.add(button7);
    		p3.add(button8);
    		p3.add(button9);
    		p3.add(buttonMultiply);
     
    		gb.setConstraints(p3,red3);
    		windowContent .add(p3);
     
    		GridBagConstraints red4 = new GridBagConstraints ();
     
    		red4 .gridx=0;
    		red4 .gridy=4;
     
    		red4 .gridheight=1;
    		red4 .gridwidth=1;
     
    		red4 .fill = red4.BOTH;
    		red4 .anchor = red4.CENTER;
     
    		red4 .weightx=1.0;
    		red4 .weighty=1.0;
     
    		button0 = new JButton ("0");
    		buttonPoint = new JButton (".");
    		buttonEqual = new JButton ("=");
    		buttonSlash = new JButton ("/");
     
    		JPanel p4 = new JPanel ();
    		p4.setLayout(g1);
     
    		p4.add(button0);
    		p4.add(buttonPoint);
    		p4.add(buttonEqual);
    		p4.add(buttonSlash);
     
    		gb.setConstraints (p4,red4);
    		windowContent .add(p4);
     
    		JFrame frame = new JFrame("Calculator");
    		frame.setContentPane(windowContent);
    		frame.pack();
    		frame.setVisible(true);
     
    		CalculatorEngine calcEngine = new CalculatorEngine ();
    		button0.addActionListener(calcEngine);
     
    		}
     
    	public void setDisplay (String x){
    		display.setText(x);
    	}
     
    	public String getDisplay (){
    		return display.getText();
    	}
     
    	public static void main (String [] args){
    		new Calculator ();
    	}
    }

    and here is the code for performing actions

    package com.PracticalJava.Lesson9;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
     
    public class CalculatorEngine implements ActionListener {
     
    	Calculator referenca;
     
    	CalculatorEngine (){
     
    	}
    	CalculatorEngine (Calculator referenca)
    	{
    		this.referenca=referenca;
    	}
     
    	public void actionPerformed (ActionEvent događaj){
     
    		{
    			JButton clickedButton = (JButton) događaj.getSource();
     
    			String displayText = referenca.getDisplay();
     
    			String clickedButtonLabel = clickedButton.getText ();
     
    			referenca.setDisplay(clickedButtonLabel);
    		}
     
    	}
     
     
    }

    There are no compliation errors. When I press button0 My program should display 0 in text field but it doesn`t. Nothing happens and in CONSOLE field in eclipse it says.

    "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.PracticalJava.Lesson9.CalculatorEngine.actionP erformed(CalculatorEngine.java:25)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.mouseRe leased(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(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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$000(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$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)"

    Can anyone help me how to solve this problem? I know that some variables are in Croatian and I am sorry for that.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple calculator issue

    If you want help, you'll have to provide an SSCCE that demonstrates the problem.

    Alternatively, you should step through this with a debugger or at the very least add some print statements to figure out what's going on. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ikocijan (June 20th, 2012)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Simple calculator issue

    referenca is null. When you created your CalculatorEngine object, you didn't use the constructor that passes it the Calculator.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    ikocijan (June 20th, 2012)

  6. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple calculator issue

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.PracticalJava.Lesson9.CalculatorEngine.actionP erformed(CalculatorEngine.java:25)
    There is a variable on line 25 in CalculatorEngine that has a null value. Look at that line, find the variable that is null and then backtrack in the code to see why it does not have a non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    ikocijan (June 20th, 2012)

  8. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Simple calculator issue

    Quote Originally Posted by Norm View Post
    There is a variable on line 25 in CalculatorEngine that has a null value. Look at that line, find the variable that is null and then backtrack in the code to see why it does not have a non-null value.
    Thanks for your answer, and I figured out where is the problem. It`s in this line
    String displayText = referenca.getDisplay();
    I think it means that I can not reach method(or function, do not know how you call it) getDisplay in Calculator class. I think that I need to foward reference from Calculator to CalculatorEngine but do not know how.

  9. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    16
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: Simple calculator issue

    Quote Originally Posted by aussiemcgr View Post
    referenca is null. When you created your CalculatorEngine object, you didn't use the constructor that passes it the Calculator.
    I thought I did. Isn`t this what you are talking about
     
            Calculator referenca;
     
    	CalculatorEngine (Calculator referenca)
    	{
    		this.referenca=referenca;
    	}

    I realized what I did wrong. It was this line:
    CalculatorEngine calcEngine = new CalculatorEngine ([B]this[/B]);
    Now I know what you where saying when you said that I didn`t use a constructor. I used an empty constructor insted. Thanks!!
    Last edited by ikocijan; June 20th, 2012 at 03:39 AM.

Similar Threads

  1. Need help building a simple calculator
    By zigma in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 01:13 AM
  2. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM
  3. [SOLVED] Java Network simple issue
    By Budlee in forum Java Networking
    Replies: 2
    Last Post: April 7th, 2011, 12:49 PM
  4. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  5. Mortgage Calculator Issue
    By coyboss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 5th, 2011, 09:12 AM