java - Accessing JTextField in a different class and adding it to a String -
i'm making program java , have problem want write file in 1 class. in class want overwrite file, if name of file exsists. first class uses jtextfield in can enter number. number added filename. works perfect, in second class won't. used association, gave me nullpointer exception. can 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 , field 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 opgeslagen!", "succes", joptionpane.plain_message); dispose(); } catch (ioexception e1) { system.out.println("exception "); } // no filled in } else if (!allesgevuld()) { joptionpane.showmessagedialog(null, "niet alle velden zijn ingevuld!", "mislukt", joptionpane.plain_message); } // 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 number should overwritten file planning = new file(filename + ".txt"); // delete file 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 "); } } }
what problem?
if cannot call tf1.gettext() second class because tf1 private in first class -> make public or add public static variable hold file name can refer second class.
Comments
Post a Comment