当前位置: 代码迷 >> Java相关 >> [求助]代码出现异常,请各位给我分析下:
  详细解决方案

[求助]代码出现异常,请各位给我分析下:

热度:179   发布时间:2007-05-06 14:36:40.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());
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

搜索更多相关的解决方案: 代码  

----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

我的代码如下:


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("学生信息输入表");
}
重复定义了


----------------解决方案--------------------------------------------------------
给你一个建议。。要定义就全部放在外面。不然就全部放在构造函数中。不然像这样的问题还会在出现的。
----------------解决方案--------------------------------------------------------
哦,谢谢你的建议哈,这个问题确实是个问题,谢谢你的提醒,我以后一定注意了!!!
----------------解决方案--------------------------------------------------------
  相关解决方案