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.