-2

In my Android application, there is a code that is used in the service and have to read information from a file into a string. I took the code from here https://stackoverflow.com/a/9095665/2448623

   public String readSavedData ( ) {
    String datax = "" ;
    String FILENAME = "TeleportSAASPass.txt";
    if(datax != null){
    try {
        FileInputStream fIn = openFileInput ( FILENAME ) ;
        InputStreamReader isr = new InputStreamReader ( fIn ) ;
        BufferedReader buffreader = new BufferedReader ( isr ) ;

        String readString = buffreader.readLine ( ) ;
        while ( readString != null ) {
            datax = datax + readString ;
            readString = buffreader.readLine ( ) ;
        }

        isr.close ( ) ;
    } catch ( IOException ioe ) {
        ioe.printStackTrace ( ) ;
    }
    }
    return datax ;
}

I use this value to display information to the user in the form of Toast MainActivity.java

String UserInfo = gps.readSavedData();
Toast.makeText(getApplicationContext(), UserInfo, Toast.LENGTH_LONG).show();

But when I try to execute this function I get the value of

java.lang.NullPointerException
com.teleport.saas.GPSTracker.readSavedData(GPSTracker.java:294)

variable is defined in MainActivity.java

// GPSTracker class
GPSTracker gps;

How can I fix this error?

Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
Alex
  • 338
  • 2
  • 12
  • 3
    You show a declaration of a `GPSTracker`, fine; but where is it initialized? – fge Jun 05 '13 at 09:24
  • it is defined but initiated? please post the whole MainActivity – s_bei Jun 05 '13 at 09:24
  • 1
    I think you might have rushed into android programming. It's not a good idea to start with android by copy-pasting before you know basic java. I'd suggest that you at least never copy-paste, and try to learn from others' code instead. Also, NPE's are almost never a good reason to post here on SO. You should learn how to debug them. – keyser Jun 05 '13 at 09:29
  • The only line i see where i would expect NPE is the one where you open your FileInputStream - are you sure the file is where you expect it to be? – kutschkem Jun 05 '13 at 09:33
  • @kutschkem you are wrong. onpenFileInput will throw a FileNotFoundException if the file does not exists – Blackbelt Jun 05 '13 at 09:45
  • The file is in the system. I do not just copy-paste the code, I have an example based on what I've been doing my code. I also tried to solve the problem using this issue http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception – Alex Jun 05 '13 at 10:45
  • 294 line - FileInputStream fIn = openFileInput ( FILENAME ) ; – Alex Jun 05 '13 at 11:35
  • @blackbelt see? line 294 is openFileInput(). I can't be that wrong after all. @ user2448623 : openFileInput() is a function you wrote, right? Can you post the code for that function? – kutschkem Jun 05 '13 at 11:42
  • I have no other code besides the one described above my question. I found a similar question but unfortunately it does not solve my problem http://stackoverflow.com/q/16022916/2448623 – Alex Jun 05 '13 at 11:49
  • This code work perfect in MainActivity but I need use him in Service – Alex Jun 05 '13 at 12:56

2 Answers2

0

Ok so gps is an object of the GPSTracker class. You need to initialize it using new.

GPSTracker gps = new GPSTracker();

I am assuming that GPSTracker.java is a file you have created. So technically you should have defined the readSavedDate() method inside the class. Also remember to add the ACCESS_FINE_LOCATION and other necessary permissions in the manifest.

Check out this tutorial for details: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

Hope this helps. Cheers!

rtindru
  • 4,150
  • 7
  • 32
  • 49
  • I initialize GPSTracker gps = new GPSTracker(MainActivity.this); but i have same error :( – Alex Jun 05 '13 at 11:19
  • Well you should really check out the tutorial. Also share the code in the GPSTracker.java. – rtindru Jun 05 '13 at 11:24
  • checked design challenge other treatments - everything works. Gives some reason, an error in the line - 294 line - FileInputStream fIn = openFileInput ( FILENAME ) ; – Alex Jun 05 '13 at 11:37
0

Is openFileInput( FILENAME ); a method that you designed? If yes, it might throw the error, you should check it. If not, read the android doc about this function, it should tell you in which case it fails. Are you sure your file exists?

Basile Perrenoud
  • 3,810
  • 1
  • 24
  • 47