Frage

This error is all over the internet and yet none of the "explanations" ever explain anything, nor do they fix my problem.

public Properties getProperties(String propertiesFileName)
{
    Properties prop;
    prop = new Properties();
    InputStream in = new FileInputStream(propertiesFileName);
    prop.load(in);
    in.close();
    return prop;
    IOException localIOException;
    localIOException; //THIS LINE THROWS THE ERROR
    localIOException.printStackTrace();
    return null;
}

The line that says "localIOException;" is the cause of the error. Please tell me how I can fix this and/or why it's happening in the first place. Thanks.

EDIT:

Look at these 3 lines:

IOException localIOException;
localIOException;
localIOException.printStackTrace();

If I remove the middle line I get errors all over the place. For reasons I don't understand that middle line has to be there, but that's the line that causes my error.

War es hilfreich?

Lösung 2

This worked:

public Properties getProperties(String propertiesFileName)
{
    try {
        Properties prop;
        prop = new Properties();
        InputStream in = new FileInputStream(propertiesFileName);
        prop.load(in);
        in.close();
        return prop;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Andere Tipps

The Java Language Specification forbids a line that does not conform to what it considers an expression. You don't call a method on the object or assign it to a variable, which is what the compiler is telling you.

If you're accesing a file and you want to check for possible errors (using java exceptions), you ought to be using try/catch blocks:

For your code:

public Properties getProperties(String propertiesFileName)
{
    Properties prop;
    prop = new Properties();

    try
    {
        InputStream in = new FileInputStream(propertiesFileName);
        prop.load(in);
        in.close();
        return prop;
    }
    catch (IOException localIOException)
    {
        localIOException.printStackTrace();
        return null;
    }
}

The code you're writing is equivalent to write:

int x;     // IOException localIOException;
x;         // localIOException; //THIS LINE THROWS THE ERROR

That's what the error means, a variable's name by itself is not an expression for Java.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top