java-告诉addItemListener()时,JComboBox抛出NullPointerException

当我尝试运行程序时,在第27行(listOfWindTurbines.addItemListener(new dropDownListener());)上收到NullPointerException.请帮忙!

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()]));
        }

    }



}
}

解决方法:

您需要初始化listOfWindTurbines变量,例如:

JComboBox listOfWindTurbines = new JComboBox();
上一篇:在GPS上运行的Android应用程序中的NullPointerException


下一篇:1、基于MFC的OpenGL程序