You tried to call it from inside the inner class but outside any method!
Because its outside any method the compiler is confused and thinks you are trying to define a new method.

Ah, sorry I didn't know that calls from outside methods were not allowed. Good to know for next time then.
Now i kind of works in the sense that I managed to get something onto the document, but there are a few problems:
1) the format isn't the one I want. It's pretty much the same issue we had before, in the sense that everything runs together in one line (but this time we have the system-specific separator added fileWriter.append(stringInput + separator);) and every input is followed by "null". An interesting thing is that no matter which application I open the file with, everything still runs in one long line, which makes me think that it's not the same issue we had before. Here is a screenshot:
sentenceFile.jpg
Also, I'm thinking to rewrite the ClearButtonHandling in such a way that it calls a function that clear the text area because if I type one sentence, press submit and then type another one, the first sentence is still there and gets added again, so I think that after you press submit I need to clear the text area anyway.

I didn't know that calls from outside methods were not allowed

You know that you can't put any kind of executeable code just lying around in a class. It has to be in a method or an initialiser. Method calls are no different than any other executable code in that respect.

Your separator is null because you never initialise it. Where you think you are initialising it you are actually initialising a local variable. Not the first time you have made this mistake (and probably not the last either :)

Um, yes I knew about executable code, but I thought methods were different...eh eh.

Not the first time you have made this mistake (and probably not the last either :)

Yes, I understand now, I'm creating another separator variable rather than initialising it. And yes, alas, I've done that before. However I went back to the console version of this application and I've notice I've made the very same mistake there, only I haven't noticed it and for whatever reason, in the console application it didn't cause any problem. So since that version was fine it didn't even occur to me that I could have made such a mistake: it's not trying to justify it of course, I should've checked, and I'll try not to make the same mistake again, but it's quite a subtle one, I'm sure that with practice I'll learn not to make it again.
It works now, so what I want to do is to changes a few things slightly and make sure that clearing the text in the textarea sits in its own function so I can call it also as soon as I click on the submit button.
One question: here at home I have Ubuntu and I tried to run the application, it compiles OK but when I press submit in the application I get a FileNotFoundException

catch(FileNotFoundException fileNotFoundException){
                System.err.println("Error opening or creating the file");
                System.exit(1);
            }

and get the error message. It's odd in a way because I get the working directory

workingDir = System.getProperty("user.dir");
...
filePath = workingDir + "\\sample.txt";

Do you reckon that the issue is with the slash - from memory in unix the slashes are the other way around, aren't they? It's not hugely important, I'm just curious

Don't have linux here so I can't say.
But why not use the File constructor that goes
public File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.... the parent pathname string is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file.

that way you don't need to input a separator at all.

that's interesting actually. I've looked that up in the API, but I'm not sure I understand it correctly, meaning I don't undesrtand what it means by "each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.". So, this application resides in a usb key, in a folder called "gui", here is the full path (in linux):
/media/KINGSTON/JAVA/GUI/2015/createFrames/files/gui
Windows (can't quite remeber, I think it's G:/KINGSTON/JAVA/GUI/2015/createFrames/files/gui):
So, if I use the new file constructor I could, perhaps, do the following:
file = new File(String gui, String sample.txt );
and that will create the sample.txt file inside the gui directory?

Right idea, wrong syntax for a method call. It goes like this:

String myDir = ..... (maybe from system properties)
String fileName = .....
File f = new File(myDir, fileName); // creates a File object with that file in that dir

Will try that, but, realistically, what difference is there gonna be between this code (which is the one I have now ):

workingDir = System.getProperty("user.dir");
...
filePath = workingDir + "\\sample.txt";         
file = new File(filePath);

And this (providing I understood correctly your suggestion, when you say getting the directory from system property):

myDir = System.getProperty("user.dir");
fileName = "sample.txt";
File f = new File(myDir, fileName); // creates a File object with that file in that dir

The two should result in the same platform-independent path, shouoldn't they?

It just avoids the problem of you having to get the right OS-dependent separator.

yes sorry, when I asked that I completely forgot about the separator variable I was using. Still, the variable should have done its job of getting the correct separator, the problem, I suspect, was with another variable:
`filePath = workingDir + "\sample.txt";
because it's using the window separator. In any case, the change has worked, I've just compiled and run the application on Ubuntu, it works :-)!
Thanks, as usual, for all your help with this.
Here is the latest version of the source code (2 files) if anybody is interested.

/*SentenceRecorder.java
takes a sentence or word as input and saves it on text file. Every new word/sentence is appended at the end of the file
*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.*;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.lang.SecurityException;//if no permission to write to file
import java.io.FileNotFoundException;//if the file can't be created
import java.io.IOException;
import java.util.Properties;//to get working directory
import java.io.BufferedWriter;
public class SentenceRecorder extends JFrame{
    private JLabel instructions;
    private JButton submit;
    private GridBagLayout gbLayout;
    private JButton clear;
    private JTextArea input;    
    private String stringInput;
    private String filePath;
    private String fileName;
    private File file;
    private BufferedWriter fileWriter;
    private String workingDir;
    private String separator;
    private Scanner scannerInput;
    //private Properties workingDir;
    /* private String string1;
    private String string2; */
    public SentenceRecorder(){
        super("List of sentences to remember");
        separator = System.getProperty("line.separator");//getting the system-dependent separator       
        workingDir = System.getProperty("user.dir");
        instructions = new JLabel("Enter your sentence.");
        input = new JTextArea(10,12);//holds the text input
        submit = new JButton("Submit");//submit button
        clear = new JButton("Clear");//clear button
        stringInput = "";//initialize string to empty
        //scannerInput = new Scanner(stringInput);
        gbLayout = new GridBagLayout();
        //System.out.println("workingDir is " + workingDir);
        //input = new Scanner(System.in);       
        fileName = "sample.txt";
        /* filePath = workingDir + "\\sample.txt";          
        file = new File(filePath);   */ 
        file = new File(workingDir, fileName);
        setLayout(gbLayout);//set layout of jframe
        add(instructions, new GridBagConstraints(0,0,2,1,0,0,CENTER,HORIZONTAL, new Insets(10,15,10,15),0,0));
        add(new JScrollPane(input), new GridBagConstraints(0,1,2,1,0.5,0.5,CENTER,BOTH, new Insets(10,15,10,15),10,10));
        add(submit, new GridBagConstraints(0,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
        add(clear, new GridBagConstraints(1,2,1,1,1.0,1.0,CENTER,HORIZONTAL,new Insets(10,15,10,15),1,1));
        //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");

        ProcessButtonHandling handler1 = new ProcessButtonHandling();
        ClearButtonHandling handler2 = new ClearButtonHandling();
        submit.addActionListener(handler1);
        clear.addActionListener(handler2);  
    }//end of constructor
    //inner class for event handlings
    private class ProcessButtonHandling implements ActionListener{
        public void actionPerformed(ActionEvent event){
            stringInput = input.getText();//copy text from textArea to string
            scannerInput = new Scanner(stringInput);            
            try{    
                fileWriter = new BufferedWriter( new FileWriter(file,true));
            }           
            catch(SecurityException securityException){//if you don't have write access
                System.err.println("You don't have write access to this file");
                System.exit(1);
            }
            catch(FileNotFoundException fileNotFoundException){
                System.err.println("Error opening or creating the file");
                System.exit(1);
            }
            catch(IOException ioexception){
                System.err.println("General Error with IO");
                System.exit(1);
            }
            while(scannerInput.hasNext()){
                stringInput = scannerInput.nextLine();
                try{
                    fileWriter.append(stringInput + separator);
                }
                catch(IOException ioexception){
                    System.err.println("General Error with IO");
                    ioexception.printStackTrace();
                    System.exit(1);
                }
                //System.out.printf("Enter your sentence or end of file - ctrl+z or Enter+ctrl+d\n");
            }
            CloseFile();
            Clear();
        }//end of actionPerformed

    }//end of inner class
    private class ClearButtonHandling implements ActionListener{
        public void actionPerformed(ActionEvent event){         
            Clear();
        }//end of actionPerformed       
    }
    private void Clear(){
        stringInput = "";
        input.setText("");
        CloseFile();        
    }
    public void CloseFile(){
        try{
            fileWriter.close();
        }
        catch(IOException ioexception){
                System.err.println("General Error with IO");
                ioexception.printStackTrace();
                System.exit(1);

            }
    }//closeFile
}//end of SentenceRecorder




/*SentenceRecorderTest.java*/
import javax.swing.JFrame;
public class SentenceRecorderTest{
    public static void main(String[] args){ 
        SentenceRecorder sentenceRecorder = new SentenceRecorder();
        sentenceRecorder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        //sentenceRecorder.CloseFile();
        sentenceRecorder.pack();
        //sentenceRecorder.setSize(900,800);
        sentenceRecorder.setVisible(true);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.