出现异常,帮忙看看!

yihanmushui5211314 2010-04-12 08:15:50
package src;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@SuppressWarnings("serial")
public class TestFrame extends JFrame {
JComboBox comboBox;
int index;
JToolBar toolBar = new JToolBar();//创建一个工具栏
JButton bNew = new JButton();//创建没有标签和图标的按钮
JButton bOpen = new JButton();
JButton bSave = new JButton();
JButton bbSave=new JButton();
JButton bComp = new JButton();
JButton bParse = new JButton();
JButton bTest = new JButton();
JButton bSelect = new JButton();
JTextArea content = new JTextArea();
JScrollPane jsp = new JScrollPane(content);//创建一个显示指定组件内容的 JScrollPane,只要组件的内容超过视图大小就会自动显示水平和垂直滚动条。
File curFile = null;

public TestFrame() {
bNew.setToolTipText("新建");
bNew.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/new.gif")));
bOpen.setToolTipText("打开");
bOpen.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/open.gif")));
bSave.setToolTipText("保存");
bSave.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/save.gif")));
bbSave.setToolTipText("另存为");
bbSave.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/bbsave.gif")));
bComp.setToolTipText("编译");
bComp.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/comp.gif")));
bParse.setToolTipText("解析");
bParse.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/run.gif")));
bTest.setToolTipText("测试");
bTest.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/Test.gif")));
bSelect.setToolTipText("选择");
bSelect.setIcon(new ImageIcon(TestFrame.class.getResource("/com/xyd/java/lesson12/pic/select.gif")));
toolBar.add(bNew);
toolBar.add(bOpen);
toolBar.add(bSave);
toolBar.add(bbSave);
toolBar.add(bComp);
toolBar.add(bParse);
toolBar.add(bTest);
// toolBar.add(bSelect);
//打开
bOpen.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("."); //创建一个给定目录的对话框
if(chooser.showOpenDialog(TestFrame.this) == JFileChooser.CANCEL_OPTION) {
return ;//showOpenDialog(Component)显示文件打开对话框,参数为父组件对象
}

curFile = chooser.getSelectedFile();
content.setText("");
try {
BufferedReader br = new BufferedReader(new FileReader(curFile));
String msg = br.readLine();
while(msg != null) {
content.append(msg+"\n");
msg = br.readLine();
}

} catch (IOException e1) {
JOptionPane.showMessageDialog(TestFrame.this, "打开文件失败!");

}
}

});

//解析
bParse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
StringBuffer str=new StringBuffer("");
String[]stra=new String[4];
int i=0;
try {
Class clazz=Class.forName("src.hello");
Method [] methods=clazz.getDeclaredMethods();
for(Method method: methods){
String modifier=Modifier.toString(method.getModifiers());
Class returnType=method.getReturnType();//返回类型
Class [] paramTypes=method.getParameterTypes();//参数列表
str=str.append(" "+modifier+" "+returnType+" "+method.getName()+"(");
for(int j=0;j<paramTypes.length;j++){
if(j>0){
str=str.append(",");

}
str=str.append(paramTypes[j].getName());

}
str=str.append(");");
String str1=str.toString();
stra[i]=str1; i++;
System.out.println(str1);
str=new StringBuffer("");
}
// String labels[] = { stra[0], stra[1], stra[2], stra[3]};
// JComboBox comboBox = new JComboBox(labels);
JComboBox comboBox = new JComboBox();
comboBox.insertItemAt(stra[0], 0);
comboBox.insertItemAt(stra[1], 1);
comboBox.insertItemAt(stra[2], 2);
comboBox.insertItemAt(stra[3], 3);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
index=((JComboBox)e.getSource()).getSelectedIndex() ;
System.out.println("Selected index=" + index);
}
});
toolBar.add(comboBox);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
});
//测试
bTest.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
System.out.println(comboBox.getItemAt(index));
}
});

add(toolBar,BorderLayout.NORTH);
add(jsp);
setTitle("我的单元测试工具");
setBounds(400,400,600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestFrame();
}
}
解析完了以后,选择一个方法,按下测试按钮出现这样的异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at src.TestFrame$5.actionPerformed(TestFrame.java:248)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

是不是System.out.println(comboBox.getItemAt(index)); 这句的问题,帮帮忙看看是哪的问题!谢了!~
...全文
70 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
茫茫大海 2010-04-23
  • 打赏
  • 举报
回复
我半月前做的,一切都好着呢,但是现在也出现空指针异常,是在new JFileChooser的地方,互相学些啊!
yihanmushui5211314 2010-04-12
  • 打赏
  • 举报
回复
我知道是空指针异常,但不知道具体问题,帮忙分析下
Helix333 2010-04-12
  • 打赏
  • 举报
回复
空指针异常

62,617

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧