iSynthesis iSynthesis - 6 months ago 9
Java Question

java.lang.ArithmeticException can't see why

Im working on a calculator and am using NetBeans GUI Generator so it may be a bit unclear and confusing.
(Full Code http://pastebin.com/ZLcM74ZQ , as it was too long)

package rechnerfinal;

/**
*
* @author Fredrik
*/
public class RechnerJFrame extends javax.swing.JFrame {

/**
* Creates new form RechnerJFrame
*/
private int value1;
private int value2;
private int Rechenoperation;
public RechnerJFrame() {
initComponents();
}

//Generated Code....

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//7
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(7);
} else {
labeltextNeu = labeltext + "7";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
//รท
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 4;
jLabel1.setText("0");
}

private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//1
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(1);
} else {
labeltextNeu = labeltext + "0";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//1
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(1);
} else {
labeltextNeu = labeltext + "1";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//8
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(8);
} else {
labeltextNeu = labeltext + "8";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//9
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(9);
} else {
labeltextNeu = labeltext + "9";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//4
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(4);
} else {
labeltextNeu = labeltext + "4";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//5
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(5);
} else {
labeltextNeu = labeltext + "5";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//6
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(6);
} else {
labeltextNeu = labeltext + "6";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//2
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(2);
} else {
labeltextNeu = labeltext + "2";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//3
String labeltextNeu = null;
String labeltext = jLabel1.getText();
if(labeltext == "0") {
setlabeltext(3);
} else {
labeltextNeu = labeltext + "3";
setlabeltext(Integer.valueOf(labeltextNeu));
}
}

private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
//+
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 1;
jLabel1.setText("0");
}

private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {
//=
int Ergebnis;
switch(Rechenoperation) {
case 1: //+
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 + value2;
setlabeltext(Ergebnis);
case 2: //-
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 - value2;
setlabeltext(Ergebnis);
case 3: //x
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 * value2;
setlabeltext(Ergebnis);
case 4: //:
value2 = Integer.valueOf(jLabel1.getText());
Ergebnis = value1 / value2;
setlabeltext(Ergebnis);
}
Ergebnis = 0;
}

private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) {
//AC
jLabel1.setText("0");
value1 = 0;
value2 = 0;
Rechenoperation = 0;
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
//-
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 2;
jLabel1.setText("0");
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
//*
value1 = Integer.valueOf(jLabel1.getText());
Rechenoperation = 3;
jLabel1.setText("0");
}

private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}


public void setlabeltext(int labelwert) {
jLabel1.setText(Integer.toString(labelwert));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RechnerJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new RechnerJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton23;
private javax.swing.JButton jButton24;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}


This is the error I'm getting:

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
at rechnerfinal.RechnerJFrame.jButton24ActionPerformed(RechnerJFrame.java:451)
at rechnerfinal.RechnerJFrame.access$1700(RechnerJFrame.java:8)
at rechnerfinal.RechnerJFrame$18.actionPerformed(RechnerJFrame.java:212)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


Also, the
jLabel
is not displaying the outcome of the calculation although when testing with
System.out.println
, it gave me the correct solution. I'm completly out of ideas as to what could cause this.

Answer

You have a couple of issues in your switch statement:

switch(Rechenoperation) {
            case 1: //+
                value2 = Integer.valueOf(jLabel1.getText());
                Ergebnis = value1 + value2;
                setlabeltext(Ergebnis);
                break;
            case 2: //-
                value2 = Integer.valueOf(jLabel1.getText());
                Ergebnis = value1 - value2;
                setlabeltext(Ergebnis);
                break;
            case 3: //x
                value2 = Integer.valueOf(jLabel1.getText());
                Ergebnis = value1 * value2;
                setlabeltext(Ergebnis);
                break;
            case 4: //:
                value2 = Integer.valueOf(jLabel1.getText());
                if(value2 != 0){ //Add a safeguard if value2 is '0'
                    Ergebnis = value1 / value2;
                    setlabeltext(Ergebnis);
                } else {
                    setlabeltext("NaN");
                }
                break;
        }

You need to break after the logic in each case. This makes things more efficient and is good practice. In addition, check to see whether value2 is zero before dividing.

As FredK pointed out, you can setlabeltext after the switch statement and get the value2 prior to the switch statement. Regardless, it wont matter with efficiency as at most, these will be executed once each because of your break statements, but it is still better structured that way for maintenance.