当前位置: 代码迷 >> Java相关 >> 这个问题太怪了,请帮帮我吧.
  详细解决方案

这个问题太怪了,请帮帮我吧.

热度:298   发布时间:2005-09-23 22:01:00.0
这个问题太怪了,请帮帮我吧.
这个问题太怪了,请帮帮我吧. 是一个选择框的程序:编译通过了,运行时出现: 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();} }
----------------解决方案--------------------------------------------------------
这可能是路径的问题
----------------解决方案--------------------------------------------------------
构造器没有返回值这一说,所以不能写成void combobox()
----------------解决方案--------------------------------------------------------
对,构件无返回值
----------------解决方案--------------------------------------------------------

哈哈.兄弟你有福了,我给你问了好多人才知道结果是哪里错了,自己看看吧 还请以后把代码写规范点,要不很难看,哈哈 import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; class combobox extends JFrame implements ItemListener { JFrame frame; JComboBox choice1,choice2; JTextField text1,text2; JPanel panel1,panel2; combobox(){ frame=new JFrame("jcomboBox演示窗口"); panel1=new JPanel(); panel2=new JPanel(); choice1=new JComboBox(); choice2=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){text2.setText("北京");} else if(choice2.getSelectedIndex()==2){text2.setText("上海");} else if(choice2.getSelectedIndex()==3){text2.setText("天津");} else if(choice2.getSelectedIndex()==4){text2.setText("四川");} } } public static void main(String args[]){ new combobox(); } }


----------------解决方案--------------------------------------------------------
  相关解决方案