尽管能够访问数组,Java List Listener仍会抛出Null指针错误

时间:2014-04-22 00:49:03

标签: java if-statement compare awt

这是列表组件的侦听器。我从人们选择它时得到一个空指针错误,但我仍然能够从数组中获取所需的信息。问题是每次单击列表项时编译器仍会抛出空指针事件。我怎么能正确访问该数组,但它仍然会抛出此错误? class facebookListListener实现ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            moodSetter.setEnabled(true);
            String name = thePeople[facebookList.getSelectedIndex()].getName();
            String mood = thePeople[facebookList.getSelectedIndex()].getMood();
            Color lightGrey = new Color(160,160,160);
            if(mood == null){
                thePersonsMood.setText("default_mood");
                thePersonsMood.setBackground(lightGrey);

            }
            if(mood.equalsIgnoreCase("happy")){
                thePersonsMood.setText(name+"'s mood is " + mood);
                thePersonsMood.setBackground(Color.RED);
            }else{
                thePersonsMood.setText(name+"'s mood is " + mood);
                thePersonsMood.setBackground(lightGrey);
            }
            }
        }

1 个答案:

答案 0 :(得分:3)

啊,改变一下:

}
if(mood.equalsIgnoreCase("happy")){

到此:

} else // **** note the else!!!
if(mood.equalsIgnoreCase("happy")){

否则,即使if (mood == null)测试为真,您也会进入if块。