-3

I am getting a NullPointerException when running the following example:

private void contractExpiryActionPerformed(java.awt.event.ActionEvent evt) {                                               
   String expireDate = contractExpiry.getSelectedItem().toString();
   System.out.println(expireDate);

}  

Just want to printout the date to test if it is working, But I keep getting this error no matter what I do.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at tetrapro.IncomeSolution.contractExpiryActionPerformed(IncomeSolution.java:865)
    at tetrapro.IncomeSolution.lambda$initComponents$6(IncomeSolution.java:492)
    at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
    at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1352)
    at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
    at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174)
    at javax.swing.JComboBox.removeAllItems(JComboBox.java:771)
    at tetrapro.IncomeSolution.setExpirationDates(IncomeSolution.java:250)
    at tetrapro.IncomeSolution.(IncomeSolution.java:61)
    at tetrapro.IncomeSolution.lambda$main$11(IncomeSolution.java:1007)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    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$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    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)
Viorel Florian
  • 594
  • 9
  • 29
  • 1
    what is `contractExpiry`? – gschambial Oct 22 '16 at 14:15
  • contractExpiry is a JcomboBox. I know what a nullPointerException is. I have no idea why it is showing up in my code. It doesn't make any sense. I have the eventListener in the constructor but I always get this error when I want to get the item selected. – Philip Caputo Oct 22 '16 at 16:27

2 Answers2

1

Most likely contractExpiry is null getSelectedItem() returns null. Use your IDE's debugging option and palce a breakpoint on the 3rd line of your sample code

OR

private void contractExpiryActionPerformed(java.awt.event.ActionEvent evt) {
   System.out.println("contractExpiry = " + contractExpiry);
   System.out.println("contractExpiry.getSelectedItem() = " + contractExpiry.getSelectedItem());

   String expireDate = contractExpiry.getSelectedItem().toString();
   System.out.println(expireDate);

}

This actually shows you where the NullPointerException occurs:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tetrapro.IncomeSolution.contractExpiryActionPerformed(IncomeSolution.java:865)
Viorel Florian
  • 594
  • 9
  • 29
1
 private void displayOptions(String symbol){
    int count = contractExpiry.getItemCount();

    if(count != 0){
        String expire = contractExpiry.getSelectedItem().toString();        
        x = expire.substring(expire.length()-5, expire.length());
        days = data.getDaysToExpire(symbol, x);

        displayNumDays.setText(days);

        data.getSelectedData_F(symbol, x);
    }

    String last = data.getFuturePrice(contractBox.getSelectedIndex());
    displayPrice.setText(last);

I had to test if the jcombobox was empty or not. when it was empty it was giving me the error. Now, if the item count is 0 it will be skip. Hence, no more error. Thanks everyone for your help.