| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 369 人关注过本帖
标题:[求助]代码出现异常,请各位给我分析下:
只看楼主 加入收藏
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
 问题点数:0 回复次数:4 
[求助]代码出现异常,请各位给我分析下:

我的代码如下:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class lab3 implements ActionListener,ItemListener
{
JFrame f=null;
JTextField name,num,age;
JRadioButton r1,r2;
int op=0;
String sex1;
public lab3(){
JFrame f = new JFrame("学生信息输入表");
Container contentPane = f.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
JLabel L1 = new JLabel("姓名:");
name = new JTextField();
JLabel L2 = new JLabel("学号:");
num = new JTextField();
JLabel L4 = new JLabel("年龄:");
age = new JTextField();
p1.add(L1);
p1.add(name);
p1.add(L2);
p1.add(num);
p1.add(L4);
p1.add(age);

contentPane.add(p1, BorderLayout.NORTH);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
JLabel L3 = new JLabel("性别:");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
p3.add(L3);
p3.add(r1);
p3.add(r2);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
r1.addItemListener(this);
r2.addItemListener(this);
contentPane.add(p3, BorderLayout.CENTER);


JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2));
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
p2.add(b1);
p2.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
contentPane.add(p2, BorderLayout.SOUTH);
f.setSize(210, 200);
//f.pack();//对组件进行排列
f.show();//显示
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void itemStateChanged(ItemEvent e){//单选按钮被单击时触发

if(e.getSource()==r1)op=1;
if(e.getSource()==r2)op=2;
}

public void actionPerformed(ActionEvent e){//确定,取消按钮被单击时触发
String cmd=e.getActionCommand();
if(cmd.equals("确定")){
String name1=name.getText();
System.out.println(name1);
//int number=Integer.parseInt(num.getText());
//int age1=Integer.parseInt(age.getText());
String number=num.getText();
String age1=age.getText();
switch(op){
case 1:
sex1="男";
break;
case 2:
sex1="女";
break;
}
//写文件
String filename="F:\\students.txt";
try{
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
out.write(name1);
out.newLine();
out.write(number);
out.newLine();
out.write(sex1);
out.newLine();
out.write(age1);
out.newLine();
out.close();
}
catch (IOException iox)
{
System.out.println("写文件有错误!"+filename);
}
//读文件
String filename2 ="F:\\students.txt";
String line;
try{
BufferedReader in =new BufferedReader(new FileReader(filename2));
line=in.readLine();
while (line !=null)
{
System.out.println(line);
line=in.readLine();
}
in.close();
}
catch (IOException iox)
{
System.out.println("读文件有错误!"+filename2);
}

f.setVisible(false);
return;
}

else if(cmd.equals("取消"))System.exit(0);

}
public static void main(String args[])
{
new lab3();
}


}



然后出现的结果是这样的:


C:\Borland\JBuilder2005\jdk1.4\bin\javaw -classpath "F:\shiyan3\classes;C:\Borland\JBuilder2005\jdk1.4\jre\bin\SHOCK.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\indicim.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\thaiim.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\dnsns.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\sunjce_provider.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\ldapsec.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\localedata.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\jsse.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\plugin.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\sunrsasign.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\charsets.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\rt.jar;C:\Borland\JBuilder2005\jdk1.4\jre\lib\jce.jar;C:\Borland\JBuilder2005\jdk1.4\jre\javaws\javaws.jar;C:\Borland\JBuilder2005\jdk1.4\lib\tools.jar;C:\Borland\JBuilder2005\jdk1.4\lib\dt.jar;C:\Borland\JBuilder2005\jdk1.4\lib\htmlconverter.jar" lab3
java.lang.NullPointerException

at lab3.actionPerformed(lab3.java:122)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)

at java.awt.Component.processMouseEvent(Component.java:5100)

at java.awt.Component.processEvent(Component.java:4897)

at java.awt.Container.processEvent(Container.java:1569)

at java.awt.Component.dispatchEventImpl(Component.java:3615)

at java.awt.Container.dispatchEventImpl(Container.java:1627)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)

at java.awt.Container.dispatchEventImpl(Container.java:1613)

at java.awt.Window.dispatchEventImpl(Window.java:1606)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

李四

李四

123456

20

搜索更多相关主题的帖子: 代码 
2007-05-06 14:36
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

空指针异常,在你的程序第122行


可惜不是你,陪我到最后
2007-05-07 09:20
食恶不色
Rank: 2
等 级:新手上路
威 望:3
帖 子:632
专家分:5
注 册:2006-11-8
收藏
得分:0 

我的代码如下:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class lab3 implements ActionListener,ItemListener
{
JFrame f=null;
JTextField name,num,age;
JRadioButton r1,r2;
int op=0;
String sex1;
public lab3(){
JFrame f = new JFrame("学生信息输入表");
}
重复定义了


做人要厚道,看帖要回帖!回帖是尊重,回帖是美德! 美德要发扬,我们要顶帖!顶帖是好事,千万莫灌水!
2007-05-07 10:59
食恶不色
Rank: 2
等 级:新手上路
威 望:3
帖 子:632
专家分:5
注 册:2006-11-8
收藏
得分:0 
给你一个建议。。要定义就全部放在外面。不然就全部放在构造函数中。不然像这样的问题还会在出现的。

做人要厚道,看帖要回帖!回帖是尊重,回帖是美德! 美德要发扬,我们要顶帖!顶帖是好事,千万莫灌水!
2007-05-07 11:01
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 
哦,谢谢你的建议哈,这个问题确实是个问题,谢谢你的提醒,我以后一定注意了!!!
2007-05-07 12:45
快速回复:[求助]代码出现异常,请各位给我分析下:
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030091 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved