java swing nullpointerexception jcombobox

java - JComboBox está lanzando una NullPointerException cuando se le dice addItemListener()



swing (1)

listOfWindTurbines inicializar la variable listOfWindTurbines , por ejemplo:

JComboBox listOfWindTurbines = new JComboBox();

listOfWindTurbines.addItemListener(new dropDownListener()); una NullPointerException en la línea 27 ( listOfWindTurbines.addItemListener(new dropDownListener()); ) cuando intento ejecutar mi programa. ¡Por favor ayuda!

import java.awt.event.*; import java.awt.*; import javax.swing.*; public class PlannerMain { JFrame frame; JButton makeMap; JPanel panel; JLabel outcome; JComboBox listOfWindTurbines; String[] windTurbineSpace = new String[10]; Integer[] windTurbineLengths = new Integer[10]; Integer[] windTurbineWidths = new Integer[10]; JTextField lengthOfRoom, widthOfRoom, widthObjectNeeds, lengthObjectNeeds; int lengthOfRoomInt, widthOfRoomInt, widthObjectNeedsInt, lengthObjectNeedsInt, largerObjectMeasurement, numberOfItems, numberOfItemsShort; public static void main(String[] args){ PlannerMain p = new PlannerMain(); } public PlannerMain(){ windTurbineLengths[0] = 1; windTurbineWidths[0] = 1; for(int i = 0;i<=9;i++){ int wNum = i + 1; windTurbineSpace[i] = "Windturbine "+ wNum; } listOfWindTurbines.addItemListener(new dropDownListener()); frame = new JFrame("Minecraft Land Planner"); outcome = new JLabel(); panel = new JPanel(); makeMap = new JButton("Make Map"); lengthOfRoom = new JTextField("Length of Room"); widthOfRoom = new JTextField("Width of Room"); widthObjectNeeds = new JTextField("Width Object Needs"); lengthObjectNeeds = new JTextField("Length Object Needs"); listOfWindTurbines = new JComboBox(windTurbineSpace); makeMap.addActionListener(new makeMapListener()); frame.setSize(580,550); frame.add(panel); panel.add(makeMap); panel.add(lengthOfRoom); panel.add(widthOfRoom); panel.add(lengthObjectNeeds); panel.add(widthObjectNeeds); panel.add(listOfWindTurbines); panel.add(outcome); frame.setVisible(true); } class makeMapListener implements ActionListener{ public void actionPerformed(ActionEvent e) { lengthOfRoomInt = Integer.parseInt(lengthOfRoom.getText()); widthOfRoomInt = Integer.parseInt(widthOfRoom.getText()); lengthObjectNeedsInt = Integer.parseInt(lengthObjectNeeds.getText()); widthObjectNeedsInt = Integer.parseInt(widthObjectNeeds.getText()); if(lengthObjectNeedsInt<=widthObjectNeedsInt){ largerObjectMeasurement = widthObjectNeedsInt; } if(widthObjectNeedsInt<=lengthObjectNeedsInt){ largerObjectMeasurement = lengthObjectNeedsInt; } numberOfItems = (lengthOfRoomInt/lengthObjectNeedsInt)*(widthOfRoomInt/widthObjectNeedsInt); outcome.setText(String.valueOf(numberOfItems)); lengthOfRoom.setSize(30, 20); widthOfRoom.setSize(30, 20); widthObjectNeeds.setSize(30, 10); lengthObjectNeeds.setSize(100, 20); } } class dropDownListener implements ItemListener{ public void itemStateChanged(ItemEvent event) { if(event.getStateChange() == ItemEvent.SELECTED){ lengthObjectNeeds.setText(Integer.toString(windTurbineLengths[listOfWindTurbines.getSelectedIndex()])); widthObjectNeeds.setText(Integer.toString(windTurbineLengths[listOfWindTurbines.getSelectedIndex()])); } } } }