Olá, pessoal!
Quando calcular e clique abrir outra janela sobre forma de pagamento e apareceu o erro veja mensagem de erro em baixo e depois te mostro os códigos que fiz…
Mensagem de erro
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Exe4.pagamento(Exe4.java:88)
at Exe4.actionPerformed(Exe4.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Código que eu fiz
public class Exe4 implements ActionListener {
JFrame frame, frame1;
JPanel panel, panel1,panelLbl,panelRadio;
JTextField text;
JButton button,button1,butttonRadio;
JLabel lbl,lblSoma;
JRadioButton rb1,rb2,rb3;
ButtonGroup grupo;
static double soma=0;
public static void main(String[] args) {
Exe4 ex = new Exe4();
ex.criaJanela();
}
public void criaJanela(){
frame = new JFrame("Teste");
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3,1));
panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
button = new JButton("soma");
button1 = new JButton("Ok");
lbl = new JLabel("Coloca o valor:");
text = new JTextField(10);
panel.add(lbl);
panel.add(text);
panelLbl = new JPanel();
lblSoma = new JLabel(String.valueOf(Exe4.soma));
panelLbl.add(lblSoma);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1,2));
panel1.add(button);
button.addActionListener(this);
panel1.add(button1);
button1.addActionListener(this);
frame.add(panel);
frame.add(panelLbl);
frame.add(panel1);
frame.setVisible(true);
}
public void pagamento(){
frame1 = new JFrame("Forma do pagamento");
frame1.setSize(200,200);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLayout(new GridLayout(2,1));
panelRadio = new JPanel();
rb1 = new JRadioButton("Dinheiro");
rb2 = new JRadioButton("Crédito");
rb3 = new JRadioButton("Cheque");
panelRadio.add(rb1);
panelRadio.add(rb2);
panelRadio.add(rb3);
grupo.add(rb1);
grupo.add(rb2);
grupo.add(rb3);
butttonRadio = new JButton();
butttonRadio.addActionListener(this);
frame1.add(panelRadio);
frame1.add(butttonRadio);
frame1.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button){
Exe4.soma+=Double.parseDouble(text.getText());
text.setText(null);
lblSoma.setText(String.valueOf(Exe4.soma));
} else if(e.getSource() == button1){
Exe4 exe = new Exe4();
exe.pagamento();
} else if(e.getSource() == butttonRadio){
if(rb1.isSelected()){
JOptionPane.showMessageDialog(null,"O pagamento do Dinheiro: \nTotal: "+Exe4.soma);
} else if (rb2.isSelected()){
double total = Exe4.soma * (1 + 0.05);
JOptionPane.showMessageDialog(null,"Pagamento do debito: \nAcréscimo:5% \n Total: "+total);
} else if(rb3.isSelected()){
double total2 = Exe4.soma * (1+ 0.10);
JOptionPane.showMessageDialog(null,"Pagamento do Cheque: \nAcréscimo: 10% \n Total: "+total2);
}
}
}
}
Espero que vocês me ajudem…