0

I want to use a properties file to read out some config data in my android app. When I place the properties file within the class folder of the handler everything works fine, but I want to place the properties file in the project root. I already tried it with relative paths (../../../ etc.), but it didn't work.

Is there an "easy" way to do this (/config.properties also didn't work)?

Here is my code:

    private Properties prop;

    public PropertiesHandler() {
        this.prop = new Properties();
        InputStream input = null;

        try {
            input = getClass().getResourceAsStream("config.properties");
            //input = new FileInputStream("config.properties");
            prop.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

Any help would be greatly appreciated.

EDIT:

Unfortunately I got this Exception when applying the suggested solution:

10-08 15:10:05.903: E/AndroidRuntime(967): FATAL EXCEPTION: main
10-08 15:10:05.903: E/AndroidRuntime(967): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.os.Looper.loop(Looper.java:137)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 15:10:05.903: E/AndroidRuntime(967):  at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967):  at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:10:05.903: E/AndroidRuntime(967):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 15:10:05.903: E/AndroidRuntime(967):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 15:10:05.903: E/AndroidRuntime(967):  at dalvik.system.NativeStart.main(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967): Caused by: java.lang.NullPointerException
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
10-08 15:10:05.903: E/AndroidRuntime(967):  at com.dev.app1234.Register.<init>(Register.java:68)
10-08 15:10:05.903: E/AndroidRuntime(967):  at java.lang.Class.newInstanceImpl(Native Method)
10-08 15:10:05.903: E/AndroidRuntime(967):  at java.lang.Class.newInstance(Class.java:1319)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 15:10:05.903: E/AndroidRuntime(967):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 15:10:05.903: E/AndroidRuntime(967):  ... 11 more

Here is my code:

public PropertiesHandler(Context context) {
        this.prop = new Properties();
        InputStream input = null;

        try {
            input = context.getAssets().open("config.properties");

And I call it like this:

private final String URL = new PropertiesHandler(this.getApplicationContext()).getUrl()
                + "url.php";

EDIT 2:

Code:

private final String URL = new PropertiesHandler(Register.this).getUrl()
            + "url.php";

Stack Trace:

10-08 20:45:14.421: E/AndroidRuntime(1024): FATAL EXCEPTION: main
10-08 20:45:14.421: E/AndroidRuntime(1024): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.app1234/com.dev.app1234.Register}: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.os.Looper.loop(Looper.java:137)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at java.lang.reflect.Method.invoke(Method.java:511)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at dalvik.system.NativeStart.main(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024): Caused by: java.lang.NullPointerException
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at com.dev.app1234.model.PropertiesHandler.<init>(PropertiesHandler.java:25)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at com.dev.app1234.Register.<init>(Register.java:68)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at java.lang.Class.newInstanceImpl(Native Method)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at java.lang.Class.newInstance(Class.java:1319)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-08 20:45:14.421: E/AndroidRuntime(1024):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-08 20:45:14.421: E/AndroidRuntime(1024):     ... 11 more
6
  • Where is your config.properties file ? Commented Oct 7, 2014 at 17:47
  • In the project root. Commented Oct 7, 2014 at 17:48
  • About your edit : try without using the application context. private final String URL = new PropertiesHandler(AnActivity.this).getUrl() + "url.php"; For instance, if an activity is reachable. Commented Oct 8, 2014 at 19:06
  • Thanks for the tip, but I got the same exception. Activity should be reachable -> public class Register extends Activity Commented Oct 8, 2014 at 19:45
  • 1
    Could you try initializing URL in the OnCreate method (remove the final for test purpose), maybe the inline init of the private variable causes the issue Commented Oct 8, 2014 at 20:51

2 Answers 2

1

This call:

private final String URL = new PropertiesHandler(Register.this).getUrl() + "url.php";

needs to be placed inside the activity's onCreate() method. Like so:

class Register extends Activity {

    private String URL;

    // ...

    protected void onCreate(Bundle savedInstanceState) {
        // ...
        URL = new PropertiesHandler(Register.this).getUrl() + "url.php";
        // ...
    }
}

By creating your custom class before the activity has been created, you are effectively passing null in this code: new PropertiesHandler(Register.this), hence the null pointer error that appears in the stack trace.

Errors like this one occur often because so many useful system calls in Android require a Context. A good clue that this is happening is seeing in the stack trace a reference to the <init> virtual method, which refers to all the variable initializers in a class that are not enclosed in some other method. Like this line in the stack trace given:

10-08 20:45:14.421: E/AndroidRuntime(1024): at com.dev.app1234.Register.<init>(Register.java:68)
Sign up to request clarification or add additional context in comments.

Comments

1

If i were you, i would put my config file in the asset folder and get an InputStream on it like this :

InputStream is = context.getAssets().open("config.properties");

Where context is your activity for instance.

6 Comments

Thanks for your help. How do I call it from my activity?
If you call within the activity you can directly write : InputStream is = getAssets().open("config.properties");
No, I don't call it directly within the activity. I tried it likes this: new PropertiesHandler(this).getPropertyURL() but I got a NullPointerException in this line: InputStream is = context.getAssets().open("config.properties");.
In the class where you made the call, you must have a reference to a Context, Activity or Application for instance.
Thanks for the tip. But new PropertiesHandler(this.getApplicationContext()).getPropertyURL() also gave me a NullPointerException. Any ideas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.