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.