I want to read a text file and parse the string until eof..here is a code snippet.. programs.txt is under assets directory
public void insert_programs() throws FileNotFoundException {
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new
InputStreamReader(getAssets().open("programs.txt"))); // <-- NPE occurs on that line
} catch (IOException e) {
e.printStackTrace();
}
Scanner pgm = new Scanner(bfr);
pgm.useDelimiter("*{3}");
while (pgm.hasNext()) {
String str = pgm.next();
process(str);
}
}
Logs
03-14 18:05:34.936: E/AndroidRuntime(467): at dalvik.system.NativeStart.main(Native Method)
03-14 18:05:34.936: E/AndroidRuntime(467): Caused by: java.lang.NullPointerException
03-14 18:05:34.936: E/AndroidRuntime(467): at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
03-14 18:05:34.936: E/AndroidRuntime(467): at c.theworld.com.nikhil.Database.insert_programs(Database.java:40)
The method
ContextWrapper.getAssets()is throwing theNullPointerException. You are extendingContextWrapper(or another class that extends it) and it looks like that has not been initialized properly. The baseContextwithin theContextWrapperis null.