1

I'm trying to use openFileOutput from a class which is not Activity class. When I'm writing something following, it gives me null pointer exception-

try {
            Context con = null;
            fosCAM = con.openFileOutput(camFile, Context.MODE_PRIVATE);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }    

Can anyone help me please?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pow
  • 1,377
  • 10
  • 32
  • 56

3 Answers3

4

try this if you are using it in non Activity Class:

in your Activity Class try to create a Context and then pass it to your Class Constructor

in your class get the context in class constructor and in your function (which is going to save the file ) get an extra parameter which is Context . now use yourContext.openFileOutput , the same as this :

public void SaveFileIntoStorage(String xml,Context cn) throws IOException

now it should be ok :)

Mahdi Giveie
  • 630
  • 1
  • 8
  • 25
3

You're receiving a null pointer exception because you're setting the Context variable con to null and then referencing it with con.openFileOutput.

Where are you using this code, in an activity?

If this code is in your Activity, just remove the Context variable and call openFileOutput. You can do this because Activity derives from Context. If the code is in another class you should pass a context into the class and use it.

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • Thanks for the response. No, I'm not using it in Activity. It's a different class in a different package. I have set it null cause it was asking me to initialize the value of con. As I'm not using it in Activity, it's giving me trouble :(. – Pow Mar 25 '11 at 03:47
  • 3
    Pass the Context into your class. I would call getApplicationContext() from your Activity which will return the app context. Context ctx = getApplicationContext(); Then pass ctx to your class and use it. ctx.openFileOutput(...). – Ryan Reeves Mar 25 '11 at 04:02
  • Or you can also pass the activity context in the constructor of the class and can use it in the function of yours. – mudit Mar 25 '11 at 05:24
  • Thanks for the responses again. Now in my other class if I write something like this - Context ctx = Main.this.getApplicationContext(); it gives me the error - "No enclosing instance of the type Main is accessible in scope". I cannot make the Main class static. Any suggestion please? – Pow Mar 25 '11 at 07:05
0

If you are starting your second class from an Activity, you can pass it your context.

new SecondClass(getBaseContext()).start();

getBaseContext() will return your context, but you should call it from an Activity or equal class.

vendor
  • 1,854
  • 13
  • 8
  • I'm not starting my second class from Activity. It's a complete different one. – Pow Mar 25 '11 at 18:28
  • But you should start your application from an activity. You should catch the context and forward it to your other classes (objects). – vendor Mar 25 '11 at 21:38