Results 1 to 3 of 3
  1. #1
    bl00dshooter is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default Null Pointer Execption

    Hello,

    I'm making a calculator in java :)
    I'm using NetBeans as my IDE.

    Whener I run it, it gives me this error.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Calculadora.<init>(Calculadora.java:354)
    at Calculadora$7.run(Calculadora.java:318)
    at java.awt.event.InvocationEvent.dispatch(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)
    Here is my code.


    /*
    * Calculadora.java
    *
    * Created on 23/08/2009, 20:04:49
    */

    /**
    *
    * @author Eduardo
    */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.net.URI;

    public class Calculadora extends javax.swing.JFrame {
    public int resultadoparcial;
    public int segundonumero;
    public int resultadototal;
    public String PrimeiroNumero;



    public Calculadora() {
    initComponents();
    }


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

    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel2 = new javax.swing.JPanel();
    label1 = new java.awt.Label();
    label2 = new java.awt.Label();
    textField1 = new java.awt.TextField();
    textField2 = new java.awt.TextField();
    label3 = new java.awt.Label();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    jRadioButton3 = new javax.swing.JRadioButton();
    jButton7 = new javax.swing.JButton();
    label4 = new java.awt.Label();
    jTextField4 = new javax.swing.JTextField();
    jPanel1 = new javax.swing.JPanel();
    jTextField1 = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextPane1 = new javax.swing.JTextPane();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    label5 = new java.awt.Label();
    label6 = new java.awt.Label();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    label1.setText("Altura");

    label2.setText("Largura");

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

    label3.setText("Calcular a área de um:");

    jRadioButton1.setText("Quadrado");

    jRadioButton2.setText("Retângulo");

    jRadioButton3.setText("Triângulo");

    jButton7.setText("Calcular!");

    label4.setText("A área é:");

    jTextField4.setEditable(false);
    jTextField4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField4ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(45, 45, 45)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jButton7)
    .addComponent(jRadioButton3)
    .addComponent(jRadioButton2)
    .addComponent(jRadioButton1)
    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)))))
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(19, 19, 19)
    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(65, Short.MAX_VALUE))
    );

    jPanel2Layout.linkSize(javax.swing.SwingConstants. HORIZONTAL, new java.awt.Component[] {jRadioButton1, jRadioButton2, jRadioButton3});

    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(46, 46, 46)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(23, 23, 23)
    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jRadioButton1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jRadioButton2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jRadioButton3)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jButton7)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(12, Short.MAX_VALUE))
    );

    jPanel2Layout.linkSize(javax.swing.SwingConstants. VERTICAL, new java.awt.Component[] {jRadioButton1, jRadioButton2, jRadioButton3});

    jTabbedPane1.addTab("Áreas", jPanel2);

    jTextField1.setEditable(false);

    jTextPane1.setEditable(false);
    jTextPane1.setText("Total");
    jScrollPane1.setViewportView(jTextPane1);

    jButton1.setText("Limpar");

    jButton2.setText("Calcular");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

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

    jTextField3.setText("0");
    jTextField3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField3ActionPerformed(evt);
    }
    });

    jButton4.setText("-");

    jButton5.setText("/");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton5ActionPerformed(evt);
    }
    });

    jButton6.setText("*");
    jButton6.setActionCommand("jButton6");

    jButton3.setText("+");

    label5.setText("1° Número");

    label6.setText("2° Número");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(jButton2)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jButton1))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addGap(20, 20, 20))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(50, 50, 50)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jButton3)
    .addComponent(jButton5))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jButton4)
    .addComponent(jButton6))
    .addGap(35, 35, 35))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addGap(40, 40, 40))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton1)
    .addComponent(jButton2))
    .addGap(30, 30, 30)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton4)
    .addComponent(jButton3))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jButton5)
    .addComponent(jButton6))
    .addContainerGap(71, Short.MAX_VALUE))
    );

    jTabbedPane1.addTab("Calculadora", jPanel1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
    );

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

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void textField2ActionPerformed(java.awt.event.ActionEve nt evt) {

    }

    private void jTextField4ActionPerformed(java.awt.event.ActionEv ent evt) {

    }

    private void jTextField3ActionPerformed(java.awt.event.ActionEv ent evt) {

    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEv ent evt) {

    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


    }


    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Calculadora().setVisible(true);
    }
    }
    );

    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    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.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextPane jTextPane1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.Label label6;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration
    String PRimeiroNumero = jTextField3.getText();
    public int primeironumero = Integer.parseInt(PRimeiroNumero);
    }
    I would really like if someone could help me :)

    thanks,

    bl00dshooter.

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    if you don't understand the problem with your 2 added lines here,
    you should start again with the java tutorials

    Java Code:
      Variables declaration - do not modify
      ...
      private javax.swing.JTextField jTextField3;
      ...
      // End of variables declaration
      String PRimeiroNumero = jTextField3.getText();
      public int primeironumero = Integer.parseInt(PRimeiroNumero);
    }
    also, you are mixing awt components and swing components,
    so you are going to have painting problems

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    All global declarations are executed before any code.

    So at the time of execution of
    String PRimeiroNumero = jTextField3.getText();
    jTextField3 is still null. The assignment to PRimeiroNumero needs to be inside the method
    jTextField3ActionPerformed

    One "tutorial" that demonstrates the order of initialization is
    java2s.com/Tutorial/Java/0100__Class-Definition/Initializationorder.htm

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  3. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  4. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •