Problem calling a method on a jlabel

Asked

Viewed 40 times

-1

Good am trying to make an application in 2 screens the first to set the values and the second screen to exit the values the first inserir a descrição da imagem aqui

is working normal Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculadoraimc;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 *
 * @author SpiriT
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        TextField_Peso = new javax.swing.JTextField();
        TextField_Altura = new javax.swing.JTextField();
        Peso_Label = new javax.swing.JLabel();
        Altura_Label = new javax.swing.JLabel();
        TextField_Nome = new javax.swing.JTextField();
        Nome_Label = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        JFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculadora de IMC");
        setBackground(new java.awt.Color(102, 102, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMaximumSize(new java.awt.Dimension(600, 600));
        setPreferredSize(new java.awt.Dimension(600, 600));
        getContentPane().setLayout(null);

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N
        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(190, 490, 240, 60);

        TextField_Peso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextField_PesoActionPerformed(evt);
            }
        });
        getContentPane().add(TextField_Peso);
        TextField_Peso.setBounds(260, 290, 180, 30);
        getContentPane().add(TextField_Altura);
        TextField_Altura.setBounds(260, 330, 180, 30);

        Peso_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Peso_Label.setForeground(new java.awt.Color(255, 255, 255));
        Peso_Label.setText("Peso");
        getContentPane().add(Peso_Label);
        Peso_Label.setBounds(170, 290, 110, 30);

        Altura_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Altura_Label.setForeground(new java.awt.Color(255, 255, 255));
        Altura_Label.setText("Altura");
        getContentPane().add(Altura_Label);
        Altura_Label.setBounds(160, 330, 110, 30);
        getContentPane().add(TextField_Nome);
        TextField_Nome.setBounds(260, 370, 180, 30);

        Nome_Label.setFont(new java.awt.Font("Arial Black", 2, 24)); // NOI18N
        Nome_Label.setForeground(new java.awt.Color(255, 255, 255));
        Nome_Label.setText("Nome");
        getContentPane().add(Nome_Label);
        Nome_Label.setBounds(160, 370, 110, 30);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/ftc.png"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(130, 90, 410, 150);

        JFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/fd.jpg"))); // NOI18N
        JFundo.setText("jLabel1");
        getContentPane().add(JFundo);
        JFundo.setBounds(0, 0, 612, 644);

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String nome = TextField_Nome.getText();
    double peso = Double.parseDouble(TextField_Peso.getText());
    double altura = Double.parseDouble(TextField_Altura.getText()); 
    IMC i = new IMC(nome, peso, altura);

     TextField_Nome.setText(" ");
     TextField_Peso.setText(" ");
     TextField_Altura.setText(" ");

     new NewJFrame1().setVisible(true);
     dispose();
    }                                        

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

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

            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Altura_Label;
    private javax.swing.JLabel JFundo;
    private javax.swing.JLabel Nome_Label;
    private javax.swing.JLabel Peso_Label;
    private javax.swing.JTextField TextField_Altura;
    private javax.swing.JTextField TextField_Nome;
    private javax.swing.JTextField TextField_Peso;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

the second that show the gets is giving an error:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculadoraimc;

/**
 *
 * @author SpiriT
 */
public class NewJFrame1 extends javax.swing.JFrame {
private IMC i;
    /**
     * Creates new form NewJFrame1
     */
    public NewJFrame1() {
        initComponents();
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        JFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel2.setText(i.getNome());
        getContentPane().add(jLabel2);
        jLabel2.setBounds(210, 280, 250, 14);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/ftc.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(130, 60, 420, 210);

        JFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/calculadoraimc/fd.jpg"))); // NOI18N
        JFundo.setText("jLabel1");
        getContentPane().add(JFundo);
        JFundo.setBounds(0, 0, 625, 644);

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

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

    // Variables declaration - do not modify                     
    private javax.swing.JLabel JFundo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

error code:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at calculadoraimc.NewJFrame1.initComponents(NewJFrame1.java:41)
    at calculadoraimc.NewJFrame1.<init>(NewJFrame1.java:18)
    at calculadoraimc.NewJFrame.jButton1ActionPerformed(NewJFrame.java:116)
    at calculadoraimc.NewJFrame.access$000(NewJFrame.java:14)
    at calculadoraimc.NewJFrame$1.actionPerformed(NewJFrame.java:58)
    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:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    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:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at calculadoraimc.NewJFrame.jButton1ActionPerformed(NewJFrame.java:108)
    at calculadoraimc.NewJFrame.access$000(NewJFrame.java:14)
    at calculadoraimc.NewJFrame$1.actionPerformed(NewJFrame.java:58)
    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:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    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:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)
CONSTRUÍDO COM SUCESSO (tempo total: 12 segundos)

1 answer

0

Nullpointerexception a very common error in Java. This error occurs when we are working with null values, and we want to reference them to do something. An example, suppose we have a class person, like this:

 public class Pessoa {

    private String nome;

    public void andar(){
        System.out.println("ANDANDO");
    }
}

When Performing the Person Instance with:

Pessoa p = new Pessoa();

We can access the public methods of the Pessoa object, since the compiler knows the type of the object.

Pessoa p = new Pessoa();
p.andar();

However, when we do not instantiate the person object, and declare only the object as.

Person p; p floor.();

A Nullpointerexception will be released, since the compiler does not know the walk() method, because it does not know which instance of the object.

This is just an exeplo of Nullpointer and that I think is happening with your code, there are other situations, but even probable.

Surigo que debug seu código, in order to find where these null objects are.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.