1

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.

tricknology
  • 1,092
  • 1
  • 15
  • 27
  • Are you sure `textWriter` is a class? If it is, it doesn't follow proper java naming conventions. – Jimmy Huch Aug 01 '11 at 22:28
  • Jimmy, textWriter is a class I made myself; that would also describe the error in naming conventions. it contains methods write, reWrite. it writes and rewrites just fine. – tricknology Aug 01 '11 at 23:27

3 Answers3

1

As it seems to me, the only reason you may get NPE in the line you said you get it is that context is somehow NULL (openFileInput doesn't throw NPE) make sure that context is not NULL.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

Your textWriter instance is getting instantiated while context is still null. Any definitions that occur outside of methods will happen before the constructor is called.

By the way, class names should start with a capital letter and not start with a verb, by convention. It will help you avoid errors and be easier for other people to understand.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
1

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 :)
Jimmy Huch
  • 4,400
  • 7
  • 29
  • 34
  • Also there's no reason to iterate through the entire file to find the length of the file. Use an ArrayList and then call toArray() at the end. – Jason Aug 01 '11 at 22:53
  • Jason, I'll give Jimmy's suggestion of using delimiters a go, looks like it would be a lot easier than iterating. however, i still can't get the darn file to read even one line. – tricknology Aug 01 '11 at 23:28
  • thanks Jimmy, it is closed in the P.class, one i didnt post. the issue was with context. – tricknology Aug 02 '11 at 05:24