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

我的程序是要实现一个从输入界面输入用户信息,然后写入一个文件当中,再从文件中读出来.我写的这个程序,编译执行就没问题,当我点击用户界面的"确定"后就出现了错误,哪位帮忙解决一下哈.先谢谢了!!!



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("姓名:");
JTextField name = new JTextField();
JLabel L2 = new JLabel("学号:");
JTextField num = new JTextField();
JLabel L4 = new JLabel("年龄:");
JTextField 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());
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();
}


}


搜索更多相关主题的帖子: public import color null 
2007-05-05 16:42
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 

请问是出现什么错误呢?


我的msn: myfend@
2007-05-05 16:47
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 
执行过后出现的是异常!!!!
2007-05-06 08:45
fengwei
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-12-19
收藏
得分:0 
成员变量:JTextField name,num,age;


局部变量
JTextField name = new JTextField();
JTextField num = new JTextField();
JTextField age = new JTextField();

name num age 重复定义
成员变量 name,num,age 没有初使化
2007-05-06 13:55
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 
楼上的说的那个成员变量重复定义,我现在删了重复定义后,出现了结果,但是前面还是有错,还是报抛出异常,还有就是我的数字转换不行呢!!!
2007-05-06 14:20
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 

这个是我出现结果后的错误报告:




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:120)

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)

lhj

lhj

数字部分没显示出来,我的转换类型是不是有错啊?

2007-05-06 14:23
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分: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("学生信息输入表");
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());
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();
}


}

2007-05-06 14:25
fengwei
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-12-19
收藏
得分:0 

JFrame f=null;

JFrame f = new JFrame("学生信息输入表");


还是老问题 重复定义了
2007-05-06 14:54
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 
那我再看看呢!!!
2007-05-06 15:23
lhjlhj123123
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-4-25
收藏
得分:0 
我的问题解决了,原来是这样子做的哦!!
看来不能重复定义了!!
2007-05-06 15:34
快速回复:我的程序出现不懂的错误????
数据加载中...
 
   



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

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