我正在用Java创建一个程序,我遇到的问题是我想在一个类中编写一个文件。 在另一个类中,我想覆盖此文件,如果该文件的名称已经存在。 但是,第一个类使用JTextField,您可以在其中输入数字。 该数字将添加到文件名中。 这样做很完美,但在第二节课中则不然。 我使用了一个关联,但它给了我一个nullpointer异常。 有人可以帮忙吗?
public void actionPerformed(ActionEvent e) { //make file String fileName = "Week " + tf1.getText(); File planning = new File(fileName + ".txt"); // save file if (e.getSource() == b2) { // file does't exists yet and all field are filled out if (!planning.exists() && allesGevuld()) { FileWriter fw; try { fw = new FileWriter(planning, false); BufferedWriter bw = new BufferedWriter(fw); bw.write(toString()); bw.close(); fw.close(); JOptionPane.showMessageDialog(null, "De weekplanning is opgeslagen!", "Succes", JOptionPane.PLAIN_MESSAGE); dispose(); } catch (IOException e1) { System.out.println("Exception "); } // no everything is filled in } else if (!allesGevuld()) { JOptionPane.showMessageDialog(null, "Niet alle velden zijn ingevuld!", "Mislukt", JOptionPane.PLAIN_MESSAGE); } // to overwriting frame else if (planning.exists() && allesGevuld()) { OverschrijvenFrame ov = new OverschrijvenFrame(planningFrame); ov.setVisible(true); } } }第二课:
if (e.getSource() == overschrijf) { // overschrijven van file String fileName = "Week"+ ;//this is where the number should be overwritten File planning = new File(fileName + ".txt"); // delete the file which will be overwritten planning.delete(); // new file File nieuw = new File(fileName + ".txt"); FileWriter fw; try { fw = new FileWriter(nieuw); BufferedWriter bw = new BufferedWriter(fw); bw.write(planningFrame.toString()); bw.close(); fw.close(); dispose(); } catch (IOException e1) { System.out.println("Exception "); } } }I'm making a program with Java and I have the problem that I want to write a file in one class. In another class I want to overwrite this file, if the name of the file already exsists. However the first class uses a JTextField in which you can enter a number. That number is than added to the filename. That works perfect, but in the second class it won't. I used an association, but it gave me a nullpointer exception. Can someone help?
public void actionPerformed(ActionEvent e) { //make file String fileName = "Week " + tf1.getText(); File planning = new File(fileName + ".txt"); // save file if (e.getSource() == b2) { // file does't exists yet and all field are filled out if (!planning.exists() && allesGevuld()) { FileWriter fw; try { fw = new FileWriter(planning, false); BufferedWriter bw = new BufferedWriter(fw); bw.write(toString()); bw.close(); fw.close(); JOptionPane.showMessageDialog(null, "De weekplanning is opgeslagen!", "Succes", JOptionPane.PLAIN_MESSAGE); dispose(); } catch (IOException e1) { System.out.println("Exception "); } // no everything is filled in } else if (!allesGevuld()) { JOptionPane.showMessageDialog(null, "Niet alle velden zijn ingevuld!", "Mislukt", JOptionPane.PLAIN_MESSAGE); } // to overwriting frame else if (planning.exists() && allesGevuld()) { OverschrijvenFrame ov = new OverschrijvenFrame(planningFrame); ov.setVisible(true); } } }Second class:
if (e.getSource() == overschrijf) { // overschrijven van file String fileName = "Week"+ ;//this is where the number should be overwritten File planning = new File(fileName + ".txt"); // delete the file which will be overwritten planning.delete(); // new file File nieuw = new File(fileName + ".txt"); FileWriter fw; try { fw = new FileWriter(nieuw); BufferedWriter bw = new BufferedWriter(fw); bw.write(planningFrame.toString()); bw.close(); fw.close(); dispose(); } catch (IOException e1) { System.out.println("Exception "); } } }最满意答案
你有什么问题呢?
如果你不能从第二个类调用tf1.getText(),因为tf1在第一个类中是私有的 - >使它成为公共或添加公共静态变量来保存可以从第二个类引用的文件名。
What your problem?
If you cannot call tf1.getText() from second class because tf1 is private in first class -> make it public or add public static variable to hold the file name that can refer from second class.
更多推荐
发布评论