2
I’m trying to create a validation, to know if the user is at least 12 years old.
I tried to base myself on that question: Comparison between dates.
However, I believe I’m comparing it wrong, since it gives me the following mistake:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.util.Date.getMillisOf(Date.java:958) at java.util.Date.compareTo(Date.java:978) at validacoes.IdadeMaior.testeData(IdadeMaior.java:55) at validacoes.IdadeMaior.lambda$botao$0(IdadeMaior.java:47) 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)
What should I do ?
what I tried to:
import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class IdadeMaior extends JFrame {
JLabel dataNascLabel = new JLabel("Data nascimento: ");
JDateChooser dataNasc = new JDateChooser();
JButton botao = new JButton("Calcular");
public static void main(String[] args) {
IdadeMaior idade = new IdadeMaior();
idade.setVisible(true);
}
public IdadeMaior() {
JPanel painel = new JPanel();
painel.setLayout(new FlowLayout());
painel.add(dataNascLabel);
painel.add(dataNasc);
dataNasc.setPreferredSize(new Dimension(105, 23));
painel.add(botao);
botao.setPreferredSize(new Dimension(90, 22));
botao();
add(painel);
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void botao() {
botao.addActionListener((ActionEvent e) -> {
testeData();
});
}
Date dataAtual;
private boolean testeData() {
//data não pode ser menor que 12
if (dataNasc.getDate().compareTo(dataAtual) <= 12) {
//if (dataNasc.getDate().before(dataAtual)) {
System.out.println("Data digitada: " + dataNasc.getDate());
JOptionPane.showMessageDialog(null, "Erro!");
dataNasc.requestFocus();
return false;
}
return true;
}
}
Where the error breaks?
– user28595
On the other issue I was well emphasized in saying to use the methods after and before, but I see that you are using compareTo.
– user28595
When the error occurs? What action do you take?
– user28595
@Article I edited there, I put the error + complete. Error when I click on the button that tests the dates.
– Javinha