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

  • Home
  • SEARCH
  • 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 6211563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:21:40+00:00 2026-05-24T06:21:40+00:00

I’ve searched around and haven’t been able to coem up with a solution to

  • 0

I’ve searched around and haven’t been able to coem up with a solution to this one..
my (first) problem is that i’m getting NPE on FileInputStream instream = context.getApplicationContext().openFileInput(textname);in the getLengthOfText(); method. I’ve debugged and the correct filename appears to be passed to this method. I’ve been stuck on this for weeks and really want it to work (newb).

this method is being called by another class. Also, the files are there in data/data and everything else is as it should be. please help!!
-Tricknology

/***** this class takes a text file and loads it into an array **/

public class loadArray {

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
     this.context = cxt;
     textwriter = new textWriter (cxt);
     context = cxt;
}

    FileInputStream instream;
    textWriter tw = new textWriter(context);
    String[] choiceAry, dummyAry;
    P p = new P(); // shared prefs helper
    String Choice;
    String comp = "notnull";

    int addChoiceCount, length;

    // Context context = this.getApplicationContext();

    public void buildDummies(int length) {
        // addChoiceCount = p.gisI("addChoiceCount");
        choiceAry = new String[length];
        p.pisI("length", length, context);

        // dummyAry = new String[100];

    }

    public Integer getLengthOfText(String textname)
            throws FileNotFoundException {// counts how many lines of text
        // DataInputStream dis = new DataInputStream(openFileInput("file.dat"));
        int length = 0;
        instream = context.getApplicationContext().openFileInput(textname);
        InputStreamReader inputreader = new InputStreamReader(instream);
        BufferedReader buffreader = new BufferedReader(inputreader);

        try {
            // while (!comp.equals(null)){
            while (buffreader.ready()) {
                String temp = buffreader.readLine();
                length++;
            }
            buffreader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return length;
    }

    public String[] laft(String textname) throws FileNotFoundException {
        // loads a text file to an array...load array from text........
        length = getLengthOfText(textname);
        buildDummies(length);
        try {
            instream = context.getApplicationContext().openFileInput(textname);
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);
            // load array from text
            for (int i = 0; i < (length); i++) {
                try {
                    Choice = buffreader.readLine();
                    choiceAry[i] = Choice;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                buffreader.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return choiceAry;

    }

    public String getOrderName() {
        // TODO Auto-generated method stub
        return null;
    }

}

I found the answer thanks to Leeds and billiard from #android-dev

what I was doing is also calling loadArry from another class that did not extend activity.. so it was activity >calling> class that doesnt extend activity >calling> class that doesnt extend activity. somehow in there the context was lost. I basically applied similar lines at the top starting with

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
     this.context = cxt;
     textwriter = new textWriter (cxt);
     context = cxt;

hope this saves someone a lot of time in the future.

  • 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-24T06:21:41+00:00Added an answer on May 24, 2026 at 6:21 am

    You have made things much too complicated for yourself in the code. In the end, complicated code = harder debugging.

    I load text files into arrays all the time.

    Consider using the java.util.StringTokenizer class or the String split() method in class java.lang.String.

    TenFour4 brings up another good point.

    This is what the code should look like…

    Context context;
    textWriter textwriter;
    public loadArray (Context cxt){
         this.context = cxt;
         textwriter = new textWriter (cxt);
    }
    

    –UPDATE–

    A common mistake is that the file you are trying to read from has not been closed properly, therefore you are getting a NullPointerException whenever you try to access the still open file.

    e.g.

    PrintWriter pw = new PrintWriter (new FileWriter (fileName));
    pw.println ("This is output data: " + data);
    //new loadArray ().getLengthOfText (fileName); //ERROR Throws NPE
    pw.close(); //Make Sure you Close Before trying to read the file again!
    new loadArray ().getLengthOfText (fileName); //Works like a charm :)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

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.