这个问题太怪了,请帮帮我吧.
这个问题太怪了,请帮帮我吧.
是一个选择框的程序:编译通过了,运行时出现:
Exception in thread "main" java.lang.NullPointerException
at combobox.combobox(ll.java:18)
at ll.main(ll.java:56)
源程序如下,请帮帮我啊.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class combobox implements ItemListener
{JFrame frame;
JComboBox choice1,choice2;
JTextField text1,text2;
JPanel panel1,panel2;
void combobox()
{frame=new JFrame("jcomboBox演示窗口");
panel1=new JPanel();
panel2=new JPanel();
choice1=new JComboBox();
choice1=new JComboBox();
text1=new JTextField(10);
text2=new JTextField(10);
choice1.addItem("音乐");choice2.addItem("重庆");
choice1.addItem("新闻");choice2.addItem("北京");
choice1.addItem("体育");choice2.addItem("上海");
choice1.addItem("生活");choice2.addItem("天津");
choice1.addItem("教育");choice2.addItem("四川");
Container con=frame.getContentPane();
con.setLayout(new BoxLayout(con,BoxLayout.Y_AXIS));
panel1.add(choice1);panel1.add(choice2);
panel2.add(text1);panel2.add(text2);
con.add(panel1);con.add(panel2);
choice1.addItemListener(this);
choice2.addItemListener(this);
frame.setSize(600,600);
frame.setVisible(true);frame.show();
}
public void itemStateChanged(ItemEvent e)
{if(e.getItemSelectable()==choice1)
{if(choice1.getSelectedIndex()==0)
{text1.setText("音乐");}
else if(choice1.getSelectedIndex()==1)
{text1.setText("新闻");}
else if(choice1.getSelectedIndex()==2){text1.setText("体育");}
else if(choice1.getSelectedIndex()==3){text1.setText("生活");}
else if(choice1.getSelectedIndex()==4){text1.setText("教育");}
}
else if(e.getItemSelectable()==choice2)
{
if(choice2.getSelectedIndex()==0)
{text1.setText("重庆");}
else if(choice2.getSelectedIndex()==1){text1.setText("北京");}
else if(choice2.getSelectedIndex()==2){text1.setText("上海");}
else if(choice2.getSelectedIndex()==3){text1.setText("天津");}
else if(choice2.getSelectedIndex()==4){text1.setText("四川");}
}
}}
public class ll{
public static void main(String args[])
{combobox kk=new combobox();
kk.combobox();}
}