i have this problem quite a lot -- how to get context when off in some callback somewhere. usually i arrange to pass a context in as a construction parameter and then provide that for the calls that need it. works for me!
g'luck j >Jason, thanks a lot, it turns out the context is null... See if you >can help me... > >An Alarm starts a "WakefulIntentService"( made by me, which is used to >keep a wakelock). That extends an "IntentService" (made by android). >This WakefulIntentService then writes data, passing itself as an >argument for the context. >I'm guessing IntentServices don't count as a "Context", even though >Intents and Services do count. > >Any idea how I can get a context by using a static method, or at least >get the right FileOutputStream (for the current application)? > >I appreciate your help a lot, >Brandon Nalls > >On Aug 11, 4:54 pm, Jason Proctor <[email protected]> >wrote: >> ok so if that's throwing a null pointer, then context might be null. >> check that? >> >> >> >> >I commented out a lot of code above what I pasted.... if comments >> >count as lines, line 39 is "FileOutputStream fOut = >> >context.openFileOutput(filename, Context.MODE_PRIVATE);" >> >> >Never heard of a finally block... it sounds like it isn't getting >> >called, i'll look into adding this. >> >> >I'm positive filename & contents aren't null. >> >> >On Aug 11, 4:44 pm, Jason Proctor <[email protected]> >> >wrote: >> >> what is line 39 in your source file? >> >> >> are the filename and contents non-null? >> >> >> btw, i'd recommend closing the output stream writer in a finally >> >> block. finalizers aren't guaranteed to get called. >> >> >> >I have two methods, one reads files and one writes the given string to >> >> >a file. >> >> >For some reason, the first time the write method works fine, but after >> >> >it writes the first file, any files after that makes it throw a >> >> >nullpointerexception. No idea why. The problem is in the writeData >> >> >method for sure.... why is it throwing null??? >> >> >> >Stack dump: >> >> >08-10 23:04:43.309: WARN/dalvikvm(687): threadid=17: thread exiting >> >> >with uncaught exception (group=0x4000fe70) >> >> >08-10 23:04:43.319: ERROR/AndroidRuntime(687): Uncaught handler: >> >> >thread IntentService[TheDaemon] exiting due to uncaught exception >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): >> >> >java.lang.NullPointerException >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >org.myApp.ThePreferences.writeData(ThePreferences.java:39) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >org.myApp.ThePhone.savePhoneToDisk(ThePhone.java:33) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >org.myApp.ThePhone.refreshStatus(ThePhone.java:82) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >org.myApp.TheDaemon.onHandleIntent(TheDaemon.java:62) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >android.app.IntentService$ServiceHandler.handleMessage >> >> >(IntentService.java:29) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >android.os.Handler.dispatchMessage(Handler.java:99) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >android.os.Looper.loop(Looper.java:123) >> >> >08-10 23:04:43.357: ERROR/AndroidRuntime(687): at >> >> >android.os.HandlerThread.run(HandlerThread.java:60) >> >> >> >write Method >> >> >> >public boolean writeData(String filename, String contents) >> >> > { >> >> > try { >> >> > FileOutputStream fOut = >> >> >context.openFileOutput(filename, >> >> >Context.MODE_PRIVATE); >> >> > OutputStreamWriter osw = new >>OutputStreamWriter(fOut); >> >> > osw.write(contents); >> >> > osw.flush(); >> >> > osw.close(); >> >> > } catch (IOException e) { > > >> > e.printStackTrace(); >> >> > Log.e("fmi", "fmi throws ioexception in WRITER"); > > >> > return false; >> >> > } catch (java.lang.NullPointerException e) { >> >> > e.printStackTrace(); >> >> > } >> >> > return true; >> >> > } >> >> >> >Any idea why it's throwing a null pointer exception? >> >> >> -- >> >> jason.software.particle >> >> -- >> jason.software.particle > -- jason.software.particle --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

