NewDTinStackoverflow
NewDTinStackoverflow

Reputation: 543

Preference class

I am now trying to use Preference class using

Preferences pfrOfThis = Preferences.userNodeForPackage(this)

It gets an error:

"method userNodeForPackage in class java.util.prefs.Preferences cannot be applied to given types; required: java.lang.Class found: myPac.MainApp reason: actual argument myPac.MainApp cannot be converted to java.lang.Class by method invocation conversion

Leaking this in constructor"

MainApp here extends JFrame. How comes it is not a Class and could not be used here?

Upvotes: 1

Views: 988

Answers (3)

Spike Gronim
Spike Gronim

Reputation: 6182

The reference "this" is to an object. Try "this.getClass()".

Upvotes: 3

Dave Newton
Dave Newton

Reputation: 160201

"this" isn't a class, it's an instance. Try .getClass(), or use MainApp.class.

Upvotes: 3

DejanLekic
DejanLekic

Reputation: 19797

D T, if you take a look at userNodeForPackage() doc you will see that signature of that method is is: public static Preferences userNodeForPackage(Class<?> c) which means it expect a Class object as argument. I do not know what your program does, but judging from the exception you mentioned you should try: Preferences pfrOfThis = Preferences.userNodeForPackage(MainApp.class);

Upvotes: 1

Related Questions