Olha soh, galera ta dando um erro de NullpointerException mesmo eu tratando ele, usando uma entrada velha, nao consigo achar onde esta um erro se alguem puder me ajudar agradeco ta ai meu codigo.
[code]/*
- TelaCarros.java
- Created on 18 de Outubro de 2007, 21:28
*/
package visao;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.HashMap;
import modelo.Carro;
/**
*
-
@author Nuno
/
public class TelaCarros extends javax.swing.JFrame {
HashMap<String, Carro> carros;
/* Creates new form TelaCarros */
public TelaCarros() {
initComponents();
HashMap<String,Carro> carros = new HashMap<String,Carro>();
}/** 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.
*/
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
txtPlaca = new javax.swing.JTextField();
txtAno = new javax.swing.JTextField();
txtModelo = new javax.swing.JTextField();
txtCor = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font(“Tahoma”, 1, 12));
jLabel1.setText(“Aluguel De Carros”);jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
jLabel2.setFont(new java.awt.Font(“Tahoma”, 1, 11));
jLabel2.setText(“Placa”);jLabel3.setFont(new java.awt.Font(“Tahoma”, 1, 11));
jLabel3.setText(“Ano”);jLabel4.setFont(new java.awt.Font(“Tahoma”, 1, 11));
jLabel4.setText(“Modelo”);jLabel5.setFont(new java.awt.Font(“Tahoma”, 1, 11));
jLabel5.setText(“Cor”);jButton1.setText(“Cadastrar”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(42, 42, 42)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(layout.createSequentialGroup()
.add(txtPlaca, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
.add(txtAno)
.add(org.jdesktop.layout.GroupLayout.LEADING, txtModelo)
.add(org.jdesktop.layout.GroupLayout.LEADING, txtCor)
.add(org.jdesktop.layout.GroupLayout.LEADING, jButton1)))
.add(layout.createSequentialGroup()
.add(61, 61, 61)
.add(jLabel4))
.add(layout.createSequentialGroup()
.add(69, 69, 69)
.add(jLabel5))
.add(layout.createSequentialGroup()
.add(66, 66, 66)
.add(jLabel2))
.add(layout.createSequentialGroup()
.add(71, 71, 71)
.add(jLabel3)))
.add(93, 93, 93)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(150, 150, 150)
.add(jLabel1)))
.addContainerGap(171, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel1)
.add(7, 7, 7)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(41, 41, 41)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtPlaca, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(9, 9, 9)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtAno, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtModelo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtCor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(21, 21, 21)
.add(jButton1)
.addContainerGap(34, Short.MAX_VALUE))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// não deixa entrar com os campos em branco
if(txtPlaca.getText().equals("")){
JOptionPane.showMessageDialog(this, “Informe a placa”);
txtPlaca.requestFocus();
return;
}// verifica se é uma placa válida String placa = txtPlaca.getText().toUpperCase(); if(!placa.matches("[a-zA-z]{3}[0-9]{4}")){ JOptionPane.showMessageDialog(this, "Use formato AAA9999"); txtPlaca.requestFocus(); return; } // verifica se já existe if(carros.containsKey(placa)){ JOptionPane.showMessageDialog(this, "Placa já cadastrada."); txtPlaca.requestFocus(); return; } // verifica campos em branco if(txtAno.getText().equals("")){ JOptionPane.showMessageDialog(this, "Informe o ano"); txtAno.requestFocus(); return; } // tenta converter o ano em inteiro int ano; try { ano = Integer.parseInt(txtAno.getText()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Informe o ano"); txtAno.requestFocus(); return; } catch (NullPointerException ex) { JOptionPane.showMessageDialog(this, "Informe o ano"); txtAno.requestFocus(); return; } if(txtCor.getText().equals("")){ JOptionPane.showMessageDialog(this, "Informe a cor"); txtCor.requestFocus(); return; } // cria um carro novo String modelo = txtModelo.getText(); String cor = txtCor.getText(); Carro car = new Carro(placa, modelo, ano, cor); //guarda o carro no HASHMAP carros.put(placa, car); // avisa e limpa JOptionPane.showMessageDialog(this, "Carro cadastrado"); txtPlaca.requestFocus();
}
/**
-
@param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaCarros().setVisible(true);
}
});
}
// Declaração de variáveis - não modifique
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField txtAno;
private javax.swing.JTextField txtCor;
private javax.swing.JTextField txtModelo;
private javax.swing.JTextField txtPlaca;
// Fim da declaração de variáveis -
}[/code]