0

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();
}
}
JUAN
  • 523
  • 4
  • 10
  • 27

1 Answers1

2

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • need to locate the file and the read line by line in order to use numbers from my text file, i already changed the code but still "fr" is still null and so is "br" – JUAN Oct 05 '11 at 18:46
  • @JUAN: Then perhaps the file was not found? After all, you're just printing the stack trace and carrying on as if nothing happened... – Jon Skeet Oct 05 '11 at 18:48
  • the file is in the correct path i use this code in normal java and it shows the number, but when using java and android it show null, have any thoughts or some code that works, already try many diferets codes with the same results – JUAN Oct 05 '11 at 18:54
  • @JUAN: Well how about you stop catching the exception, and then you'll be able to tell more easily what's going on. You *are* calling the method, right? – Jon Skeet Oct 05 '11 at 19:05
  • just update my code, where in maiactivity call the obj tokenizer still in br result is null – JUAN Oct 06 '11 at 14:07
  • @JUAN: If you really called `leerPath`, the `FileNotFoundException` was *not* thrown, and you really *have* removed the local variable declarations (which you still haven't done in the question), then `br` *cannot be null*. What are the values of the other variables? – Jon Skeet Oct 06 '11 at 14:09
  • for the " f " and fr there nothing when debug f has the path at the momente when is doing fr=new FileReader(f) f still has the path when it gos to br =new bufferedReader(fr) f is empty and so is fr – JUAN Oct 06 '11 at 16:52
  • @JUAN: If `f` becomes null for no reason, then it sounds like you're looking at a separate instance of the class. There's nothing in the code you've shown that will set `f` back to null. I don't know whether the problem is that you haven't changed the code as suggested or whether it's with your diagnostics... have you tried adding log statements after each line of code to see what's going on, rather than using the debugger? – Jon Skeet Oct 06 '11 at 16:55
  • checking how to do the log statements thaks for all the help jon – JUAN Oct 06 '11 at 17:13
  • @Jon Skeet : Hi, sorry for Hacking the other thread, but here I have a strange problem like when I execute command runtime from my test-project it always return null, the same thing running on android activity. and this is the link for it [why process give null inputstream in test runner class?](http://stackoverflow.com/questions/7683642/why-process-give-null-inputstream-in-test-runner-class). Thanks. – user370305 Oct 07 '11 at 08:06