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 6790397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:43:14+00:00 2026-05-26T17:43:14+00:00

Hello I am trying to call a method (fileReader) that is using openFileInput(filename) from

  • 0

Hello I am trying to call a method (fileReader) that is using openFileInput(filename) from another class.
I am doing some processing in a class which extends BroadcastReceiver, and I need to read a file, I can’t call openFileInput() inside of this class, so I have created a helper class which extends Activity or IntentService (tried both) so that I can instantiate it and call it like that:

    HelperClass hc = new HelperClass();
    hc.fileReader();

I get a NullPointerException on the line in fileReader where openFileInput is Called.
In my opinion it has to do something with the context or teh constructor not being properly initialized, its just HelperClass(){super();} .

How can I over come this?

  • 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-26T17:43:15+00:00Added an answer on May 26, 2026 at 5:43 pm

    When you are calling openFileInput you are trying to open a file associated with the given context. Since you are instantiating your HelperClass without supplying it a Context, it is trying to open your file using a null context (Hence your null pointer exception). What you can do is make your method fileReader() accept a Context object and then call openFileInput using the provider context.

    Ex.

    public void fileReader(Context context) //change the return type if you need
    {
      //your code
      //..
    
      FileInputStream fos = context.openFileInput(filename);
    }
    

    To call it

    hc.fileReader(context); //get the context using "this" or getContext() depending on where you're calling it
    

    Alternatively, you can make fileReader a static method so you do not need to create an instance of HelperClass just to use it.

    public static void fileReader(Context context) //change the return type if you need
    {
          //your code
          //..
    
          FileInputStream fos = context.openFileInput(filename);   
    }
    

    Then call it using

    HelperClass.fileReader(context);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a method inside class, from another class. namespace Crystal.Utilities
I'm trying to use call_user_func to call a method from another method of the
I'm trying to call a method of another class but this causes my app
Hello everyone I'm trying to improve my Java skills by solving some problems from
I'm trying to get a simple Java method call from C++ while Java calls
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
Hello I am trying to setup a Padrino project using DataMapper and MySQL on
**Hello, I'm trying to create an archiver in java. This means that I am
Thank you all for your answers and time! Hello, I've been trying to call
I am trying to call OpenEvent of kernel32.dll using JNA and it fails with

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.