Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:15:47+00:00 2026-05-23T11:15:47+00:00

As a novice Android developer, I’ve faced a bit strange problem. I want to

  • 0

As a novice Android developer, I’ve faced a bit strange problem. I want to create a class, which methods other classes-activities-whatever could use for working with files in some special way. Let`s say for simplicity we would be logging some stuff. If I do following within an activity (in OnClick listener for example), everything works just fine:

FileOutputStream fOut = openFileOutput("somefile", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("Very important foobar");
osw.flush();
osw.close();

But when I try to enclose that into some class and create singleton like that:

public class Logger extends BaseActivity {
//BaseActivity is the "init" class which extends Activity

public static final Logger INSTANCE = new Logger();
private Logger() { 
// singleton
}

public boolean doLog (String whatToLog) {
 try {
     FileOutputStream fOut = openFileOutput("somefile", MODE_PRIVATE);
 OutputStreamWriter osw = new OutputStreamWriter(fOut);
 osw.write(whatToLog);
 osw.flush();
 osw.close(); }
     catch (IOException ioe) { ioe.printStackTrace(); }  
     return true; }

and call it from other activity like that

Logger.INSTANCE.doLog("foobar");  

app chrashes with NullPointerException (at line with openFileOutput). I suppose it`s because of improper use of singleton/activity here and now rewriting the code to run as a service. But maybe there are some better ideas to solve an issue? Or some workarounds?

Thanks for your contributions in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:15:47+00:00Added an answer on May 23, 2026 at 11:15 am

    You based your singleton on an Activity that you didn’t start as an Activity. Therefore, it doesn’t have a valid Context, which is necessary for the IO calls. See Blundell’s answer for better singleton, with one change: As per android.app.Application javadoc, your singleton should get the application context from the given context via Context.getApplicationContext(). See why does AndroidTestCase.getContext().getApplicationContext() return null for another example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice Android developer. I used the code given at Google's Android developer
I'm novice in android. I'm writing an android application which requires the data like
I am a novice in android and trying to create a small app. my
As a novice developer who is getting into the rhythm of my first professional
I am a AS3 novice learning PureMVC and want to write code following best
I am developing an Android application. I am novice in Touch enabled Android Application.
The Problem I've been developing for Android for quite some time now. One of
I'm a novice Android programmer, and to help me understand how Intents really work,
Hi I'm a real novice at android and really getting desperate at this apparently
I have a strange one (or at least it's beyond me as a novice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.