问题描述
packagecom.lovo.book;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Properties;publicclassIO{publicvoidoutput(Propertiesprop){FileOutputStreamfos=null;try{fos=newFileOutputStream("1.properties");try{prop.store(fos,null);fos.flush();}catch(IOExceptione2){//TODO自动生成catch块e2.printStackTrace();}}catch(FileNotFoundExceptione1){//TODO自动生成catch块e1.printStackTrace();}finally{if(fos==null){try{fos.close();}catch(IOExceptione){//TODO自动生成catch块e.printStackTrace();}}}}publicvoidinput(){Propertiesprop=newProperties();FileInputStreamfli=null;try{fli=newFileInputStream("1.properties");prop.load(fli);}catch(FileNotFoundExceptione){//TODO自动生成catch块e.printStackTrace();}catch(IOExceptione){//TODO自动生成catch块e.printStackTrace();}finally{if(fli==null){try{fli.close();}catch(IOExceptione){//TODO自动生成catch块e.printStackTrace();}}}}packagecom.lovo.book;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Properties;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;publicclassPhoneGUIextendsJFrameimplementsActionListener{privateJTextFieldjtf1=newJTextField();privateJTextFieldjtf2=newJTextField();privateJTextFieldjtf3=newJTextField();privateJTextFieldjtf4=newJTextField();privateIOio=newIO();privatePropertiesprop;FileOutputStreamfos;privateJButtonjbt1=newJButton("储存");privateJButtonjbt2=newJButton("删除");publicPhoneGUI(){this.setLayout(null);//创建标签组件JLabeljlb1=newJLabel("姓名:");jlb1.setBounds(40,50,40,25);JLabeljlb2=newJLabel("性别:");jlb2.setBounds(220,50,40,25);JLabeljlb3=newJLabel("电话:");jlb3.setBounds(40,100,40,25);JLabeljlb4=newJLabel("QQ:");jlb4.setBounds(220,100,40,25);//创建单行文本框jtf1.setBounds(80,50,100,25);jtf2.setBounds(260,50,100,25);jtf3.setBounds(80,100,100,25);jtf4.setBounds(260,100,100,25);//创建按钮jbt1.setBounds(50,150,80,25);jbt1.addActionListener(this);jbt2.setBounds(150,150,80,25);jbt1.addActionListener(this);//把组件加入到容器this.add(jlb1);this.add(jlb2);this.add(jlb3);this.add(jlb4);this.add(jtf1);this.add(jtf2);this.add(jtf3);this.add(jtf4);this.add(jbt1);this.add(jbt2);this.setTitle("通讯录");this.setResizable(false);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);this.setSize(400,250);this.setVisible(true);}publicstaticvoidmain(String[]args){PhoneGUIpho=newPhoneGUI();}publicvoidactionPerformed(ActionEvente){//TODO自动生成方法存根if(e.getActionCommand().equals("储存")){《这是空指针》“Stringstr1=jtf1.getText();Stringstr2=jtf2.getText();Stringstr3=jtf3.getText();Stringstr4=jtf4.getText();”try{fos.write(str1.getBytes());fos.write(str2.getBytes());fos.write(str3.getBytes());fos.write(str4.getBytes());io.output(prop);}catch(IOExceptione1){//TODO自动生成catch块e1.printStackTrace();}}}}
解决方案
解决方案二:
你的fos,prop都没有初始化。这是我改的publicvoidactionPerformed(ActionEvente){//TODO自动生成方法存根if(e.getActionCommand().equals("储存")){//《这是空指针》Stringstr1=jtf1.getText();Stringstr2=jtf2.getText();Stringstr3=jtf3.getText();Stringstr4=jtf4.getText();try{prop=newProperties();//createObjectprop.put("name",str1);prop.put("sex",str2);prop.put("phone",str3);prop.put("qq",str4);io.output(prop);}catch(Exceptione1){//TODO自动生成catch块e1.printStackTrace();}}}