Persisting a properties object across the whole of my program.
Hello all,
As per usual, sorry for any idiocy on my part and thanks for any guidance.
I have written a wrapper class for the Properties class.
I have an MDI which creates a properties object and loads it at start up, and I have tried to use the same object again (to save any property changes) when the app terminates. However, I get a nullPointerException when I try and use it when the program closes. Below is what I hope are the relevant pieces of code. I don't include it all as it a couple of hundred lines long - but can easily do so if you think it will help. Many thanks once again:
Here is the load properties method:
privatevoid yLoadProperties(){
String workingDirectory = System.getProperty("user.dir");
File yFile =new File(workingDirectory +"\\KTProperties.ini");
YPorperties yProperties =new YPorperties(yFile);
yProperties.yLoadProperties();
// Retrieve application name - no conversion required
yApplicationName = yProperties.yGetProperty("ApplicationName");
}
and here is the exit program method with the call the the save properties method
privatevoid yExitApplication(){
// Save properties back to disk
yProperties.ySaveProperties();
// Exit
System.exit(0);
}
I have also declared the yProperties variable thus:
private YPorperties yProperties;
I was hoping to use the yProperties object anywhere within my program to adjust properties as required. Any help, gratefully received.
Steve
> This line is the problem:YPorperties
> yProperties = new YPorperties(yFile);
It
> declares a local (i.e. method-scoped) reference
> variable that hides your private (instance-scoped)
> variable, so the latter remains null. Change the line
> to:yProperties = new YPorperties(yFile);
That's great. I tried it and it worked. But can I just ask one further question?
How is it that the line:
YPorperties
> yProperties = new YPorperties(yFile);
declares yProperties as local? In my mind, when I explicitly declared yProperties, thus:
private YPorperties yProperties;
I was making it available to the whole program.
There must be something crucial in the fact that I included YProperties in the line
YPorperties
> yProperties = new YPorperties(yFile);
but I thought I was simply assigning a type to yProperties. Could you explain a little further please.
Many, many thanks,
Steve
That's just the way Java works. I'm sure someone is able to quote chapter and verse of the Java Language Specification that spells it out, but the basic distinction is this:
A variable declaration has the form: <Type> <variableName> [= <value expression>];.
A variable assignment (which assigns a value to an existing variable) has the form: <variableName> = <value expression>;
When you specify the type of a variable, Java assumes you are declaring a new variable in the current scope, regardless of whether the name of the variable already existing in a enveloping scope. When you don't specify the type, Java will look for an existing variable with that name in the current scope and any enveloping scopes.
> In my mind, when I explicitly declared yProperties, thus:> private YPorperties yProperties;
> I was making it available to the whole program.
With that line you declared a member variable of type YPorperties (shouldn't that be YProperties, by the way?), which means a separate yProperties variable is available as a member of each instance of your class. Note that you already specify the type of the variable yProperties in that line, so there is no need to specify that type again when using the variable further on in your application.
But yes, you are correct in saying that this variable would be available anywhere inside that one instance of your class. However, it is possible in Java to declare a variable with the same name in a different, narrower scope (as you have seen).
Java looks for variables from the narrowest scope outward, so in this case it sees the local variable yProperties before it sees the member variable yProperties.
(BTW for future reference, note that a class or an instance of a class is not a "program" as such. So declaring a private member variable does not make it available to your "program", it makes it available to an instance of a class. The fact that you use that class to start your application is irrelevant)