Problem NaN error or infinity error

1

Hi, I have a problem with a textfield when I try to show the result of a calculation. I tried converting it to String and everything but it says NaN error or infinity error. I better show you some images to help me. the first image shows the jframe code. the second image shows the code of the method that performs the calculation. The problem is that when I enter the data and I give it to calculate, Infinity appears instead of the result in the textfield. I can not find the problem for about 3 days, I would appreciate a solution. I am new here, greetings. Sorry I put images and not the code I did not realize, I do not know very well what is the problem so I do not put the specific question, know apologize. Thanks again.

THIS IS THE SOURCE OF THE NETBEANS JFRAME WHERE THE CODE OF THE JFIELDTEXT AND THE BOTTON IS CALCULATING. moisture calculation package;

public class calculohumedad extends javax.swing.JFrame {
    double sueloSeco, sueloHumedo, porcentajeHumedad;
    int humedad1;
    String humedad;

    public calculohumedad() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtsuelohumedo = new javax.swing.JTextField();
        jtsueloseco = new javax.swing.JTextField();
        jthumedad = new javax.swing.JTextField();
        calcular = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("     Suelo Humedo");

        jLabel2.setText("        Suelo Seco");

        jLabel3.setText("         Humedad");

        jtsuelohumedo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtsuelohumedoActionPerformed(evt);
            }
        });

        jtsueloseco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtsuelosecoActionPerformed(evt);
            }
        });

        calcular.setText("calcular");
        calcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtsuelohumedo)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(jtsueloseco))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(jthumedad))))
                .addGap(0, 16, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtsuelohumedo, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                    .addComponent(jtsueloseco)
                    .addComponent(jthumedad))
                .addGap(29, 29, 29)
                .addComponent(calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jtsuelohumedoActionPerformed(java.awt.event.ActionEvent evt) {                                              
        sueloHumedo = Double.parseDouble(this.jtsuelohumedo.getText());
    }                                             

    private void jtsuelosecoActionPerformed(java.awt.event.ActionEvent evt) {                                            
        sueloSeco = Double.parseDouble(this.jtsueloseco.getText());
    }                                           

    private void calcularActionPerformed(java.awt.event.ActionEvent evt) {                                         
        calculo micalculo = new calculo();
        humedad = String.valueOf(micalculo.calcular(sueloHumedo, sueloSeco));

        jthumedad.setText(humedad);
        System.out.print(humedad);
    }                                        

    /**
     * @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(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(calculohumedad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton calcular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jthumedad;
    private javax.swing.JTextField jtsuelohumedo;
    private javax.swing.JTextField jtsueloseco;
    // End of variables declaration                   
}

AND THIS IS THE CODE OR METHOD OF THE Q CALCULATION IS IN ANOTHER CLASS BUT THE SAME PACKAGE,

package calculationHumidity;

public class calculo {

    double sueloHumedo, sueloSeco, porcentajeHumedad;

    public double calcular(double sueloHumedo, double sueloSeco){
    porcentajeHumedad = ((sueloHumedo-sueloSeco) * 100) / sueloSeco;
    return porcentajeHumedad;
}
}

    
asked by Andres 27.09.2017 в 21:33
source

1 answer

1

I roll up a bit, but it's for you to see the process to debug you in the future.

A value of Infinity indicates, among other cases, a division of a number by 0 .

Naturally, that means that you either assign values to the attributes and keep the default value (0), or assign them a value of 0.

The code of jtsuelohumedoActionPerformed and jtsuelosecoActionPerformed is simple enough to rule out the second possibility, so the question is whether those methods are called.

So it seems that the actionPerformed is not being invoked 1 , so I look for when this method is invoked in JTextField and encounter , which basically explains that the actionPerformed of the listener is executed when you press Intro within the JTextField .

So, simply, the event is not being launched and the attributes are not initialized. You can try to play with other listeners like DocumentListener or FocusListener , but the simplest thing is to put all the logic in calcularActionPerformed , since anyway you do not use any of the data before invoking that method.

1 Adding a couple of log lines to those methods you could have detected and investigated that.     
answered by 27.09.2017 / 23:27
source