Reputation: 523
I'm using Java to program in Android where I have two clases: first is MainActivity.java
and the second is Tokenizer.java
. In Tokenizer
I use File, FileReader and BufferedReader to read a txt file. In this class I use the next code:
package net.try.........;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import android.content.Context;
public class Tokenizer {
BufferedReader br;
FileReader fr;
File f;
public void leerPath ( ) {
try {
f = new File("F:path...../file.txt");
fr = new FileReader(f);
br = new BufferedReader(fr);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When debugging the MainActivity "f" those have the path where the file is FileReader then dysplay that f is null, and so br is also null. Why is the reason that, when using in Java the code works but Java/Android is there something misisng?
this is the code in main activiti
public class MainActivity extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Tokenizer ob1=new Tokenizer();
ob1.leerPath();
}
}
Upvotes: 0
Views: 1715
Reputation: 1500785
The problem is that you've declared a load of instance variables, but then you've also declared a load of local variables which "hide" the instance variables. You're assigning values to the local variables, but that doesn't affect the instance variables at all.
This:
File f = new File("F:path...../file.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
should just be:
f = new File("F:path...../file.txt");
fr = new FileReader(f);
br = new BufferedReader(fr);
That's assuming you really want them to be instance variables. Why do you need all three anyway? Don't you just need the reader you're reading from?
Upvotes: 2