选择组合框项目时如何解决错误

时间:2015-06-01 19:44:43

标签: java swing user-interface netbeans

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at my.Posterordering.PosterorderingUI.combostyleActionPerformed(PosterorderingUI.java:394)
at my.Posterordering.PosterorderingUI.access$500(PosterorderingUI.java:17)
at my.Posterordering.PosterorderingUI$6.actionPerformed(PosterorderingUI.java:201)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:586)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:834)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:498)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
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)

当我尝试运行代码时遇到问题,这就是我的代码:

package my.Posterordering;

/**
 *
 * @author 100103562
 */
import javax.swing.*;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PosterorderingUI extends javax.swing.JFrame {
/**
 * Creates new form PosterorderingUI
 */

Double total = null;
    Double size = null;
    Double quantity = null;
    Double colour = null;
    Double style = null;
    int postage = 10;



private void BtnHelpActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    JDialogExample jDialogExample = new JDialogExample();

}                                       

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

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

private void BtnHelpMouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    PosterorderingUI newFrame = new PosterorderingUI();
    newFrame.setVisible(true);
    jLabel15.setText("test");
}                                    

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


     if (comboSize.getSelectedItem().equals("Large")){
lblpstprice.setText(String.valueOf(10.00));
int size = 10;
lblTotal.setText(String.valueOf(size + postage));
     }

else if (comboSize.getSelectedItem().equals("Small")){
           lblpstprice.setText(String.valueOf(3.00));

           int size = 3;
           lblTotal.setText(String.valueOf(size + postage));
}
     else if (comboSize.getSelectedItem().equals("Medium")){
           lblpstprice.setText(String.valueOf(6.00));
           int size = 6;
           lblTotal.setText(String.valueOf(size + postage));
}
       else {
           lblpstprice.setText(String.valueOf("Please select a poster size"));
           lblTotal.setText(String.valueOf("Error Occured"));
}
    }                                         

private void combostyleActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    if (combostyle.getSelectedItem().equals("Laminated")){
        lblstyleprice.setText(String.valueOf(5.00));
        int style = 5;
        lblTotal.setText(String.valueOf(size + style + postage));
}

else if (combostyle.getSelectedItem().equals("Glossy")){
           lblstyleprice.setText(String.valueOf(10.00));
           int style = 5;
           lblTotal.setText(String.valueOf(size + style + postage));
}
else if (combostyle.getSelectedItem().equals("Uncoated")){
           lblstyleprice.setText(String.valueOf(0.00));
           int style = 0;
           lblTotal.setText(String.valueOf(size + style + postage));
}

else {
           lblstyleprice.setText(String.valueOf("Please select a poster style"));
}

}                                          

private void comboclrActionPerformed(java.awt.event.ActionEvent evt)     {                                         
    // TODO add your handling code here:
    if (comboclr.getSelectedItem().equals("Black")){
    lblcolprice.setText(String.valueOf(2.00));
    int colour = 2;
    lblTotal.setText(String.valueOf(size + style + colour + postage));
    }

    else if (comboclr.getSelectedItem().equals("Red")){
    lblcolprice.setText(String.valueOf(3.00));
    int colour = 3;
    lblTotal.setText(String.valueOf(size + style + colour + postage));
    }

    else if (comboclr.getSelectedItem().equals("Orange")){
    lblcolprice.setText(String.valueOf(3.00));
    int colour = 3;
    lblTotal.setText(String.valueOf(size + style + colour + postage));
    }

    else if (comboclr.getSelectedItem().equals("Blue")){
    lblcolprice.setText(String.valueOf(3.00));
    lblTotal.setText(String.valueOf(size + style + colour + postage));
    int colour = 3;
    }

    else if (comboclr.getSelectedItem().equals("Yellow")){
    lblcolprice.setText(String.valueOf(3.00));
    int colour = 3;
    lblTotal.setText(String.valueOf(size + style + colour + postage));
    }

else {
           lblcolprice.setText(String.valueOf("Please select a color"));
           lblTotal.setText(String.valueOf("An error has occured"));
}    

if (combostyle.getSelectedItem().equals(" ") || (comboSize.getSelectedItem().equals(" ")) || (comboclr.getSelectedItem().equals(" "))){
lblqtot.setText(String.valueOf("Please complete the form"));           
}

else if (comboquant.getSelectedItem().equals(1)){
int number = Integer.parseInt(lblTotal.getText());
int price = 1;
lblqtot.setText(String.valueOf(number * price));
lblTotal.setText(String.valueOf(size + style + colour + postage));
           }

else if (comboquant.getSelectedItem().equals(2)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 1;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

 else if (comboquant.getSelectedItem().equals(3)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 2;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(4)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 3;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(5)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 4;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(6)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 6;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(7)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 7;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(8)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 8;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(9)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 9;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(10)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 10;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(11)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 11;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(12)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 12;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(13)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 13;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));
           }

else if (comboquant.getSelectedItem().equals(14)){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 15;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));

               }

else if (comboquant.getSelectedItem().equals("15")){
int totals = Integer.parseInt(lblTotal.getText());
int quantity = 15;
lblTotal.setText(String.valueOf(size + style + colour + postage));
lblqtot.setText(String.valueOf(totals * quantity));

               }



}                      

我希望代码很容易理解(遗憾的是它有点麻烦!)当我在海报样式组合框中选择任何东西时,我遇到了这个错误。海报大小框工作正常,所以我有点难过!

2 个答案:

答案 0 :(得分:1)

您的Double size始终为空。你没有在combostyleActionPerformed()方法中设置它,但尝试使用总和

答案 1 :(得分:0)

鉴于这一大块代码,很难说清楚。如果我不得不猜测是因为这些:

else if (comboquant.getSelectedItem().equals(1)){
int number = Integer.parseInt(lblTotal.getText());
int price = 1;
lblqtot.setText(String.valueOf(number * price));
lblTotal.setText(String.valueOf(size + style + colour + postage));
           }

我认为你需要以0而不是1开头,这会导致空指针异常:else if (comboquant.getSelectedItem().equals(0)){