TheBlueCat
TheBlueCat

Reputation: 1195

FileInputStream in Another class?

I'm loading a file that has been saved by another class via the FileOutputstream method. Anyway, I want to load that file in another class, but it either gives me syntax errors or crashes my App.

The only tutorials I could find where they saved and loaded the file in the same class, but I want to load it up in another class and couldn't find how to solve the problem of loading into another class.

Thanks

My Code:

public class LogIn extends Activity implements OnClickListener {
EditText eTuser;
EditText eTpassword;
CheckBox StaySignedIn;
Button bSubmit;
String user;
String pass;
FileOutputStream fos;
FileInputStream fis = null;
String FILENAME = "userandpass";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    eTuser = (EditText) findViewById(R.id.eTuser);
    eTpassword = (EditText) findViewById(R.id.eTpassword);
    StaySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    bSubmit = (Button) findViewById(R.id.bLogIn);
    bSubmit.setOnClickListener(this);
    File file = getBaseContext().getFileStreamPath(FILENAME);
    if (file.exists()) {
        Intent i = new Intent(LogIn.this, ChatService.class);
        startActivity(i);
    }
    // if if file exist close bracket
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // end of catch bracket
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // end of catch

} // create ends here

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.bLogIn:
        String user = eTuser.getText().toString();
        String pass = eTpassword.getText().toString();
        Bundle userandpass = new Bundle();
        userandpass.putString("user", user);
        userandpass.putString("pass", pass);
        Intent login = new Intent(LogIn.this, logincheck.class);
        login.putExtra("pass", user);
        login.putExtra("user", pass);
        startActivity(login);

        if (StaySignedIn.isChecked())
            ;
        String userstaysignedin = eTuser.getText().toString();
        String passstaysignedin = eTpassword.getText().toString();
        try {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(userstaysignedin.getBytes());
            fos.write(passstaysignedin.getBytes());
            fos.close();
        } catch (IOException e) {
            // end of try bracket, before the Catch IOExceptions e.
            e.printStackTrace();

        } // end of catch bracket

    } // switch and case ends here
}// Click ends here

}// main class ends here

Class B( Class that loads the data.)

public class ChatService extends Activity {
String collected = null;
FileInputStream fis = null;
String FILENAME;
TextView userandpass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chatservice);
    userandpass = (TextView) findViewById(R.id.textView1);

    try {
        fis = openFileInput(FILENAME);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] dataArray = null;
    try {
        dataArray = new byte[fis.available()];
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        while (fis.read(dataArray) != -1)
            ;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    {
        // while statement
    }
    userandpass.setText(collected);

}// create ends here

}// class ends here

LogCat:

03-03 21:03:34.725: E/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.bananaphone/com.gta5news.bananaphone.ChatService}: java.lang.NullPointerException
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.os.Looper.loop(Looper.java:123)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 21:03:34.725: E/AndroidRuntime(279):  at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:03:34.725: E/AndroidRuntime(279):  at java.lang.reflect.Method.invoke(Method.java:521)
03-03 21:03:34.725: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-03 21:03:34.725: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-03 21:03:34.725: E/AndroidRuntime(279):  at dalvik.system.NativeStart.main(Native Method)
03-03 21:03:34.725: E/AndroidRuntime(279): Caused by: java.lang.NullPointerException
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ContextImpl.makeFilename(ContextImpl.java:1599)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ContextImpl.openFileInput(ContextImpl.java:399)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
03-03 21:03:34.725: E/AndroidRuntime(279):  at com.gta5news.bananaphone.ChatService.onCreate(ChatService.java:25)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 21:03:34.725: E/AndroidRuntime(279):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-03 21:03:34.725: E/AndroidRuntime(279):  ... 11 more

Upvotes: 0

Views: 1054

Answers (3)

DNA
DNA

Reputation: 42617

The FILENAME string in the ChatService class is null. So you get a NullPointerException when you try to load a file using fis = openFileInput(FILENAME).

Also, your read loop throws away the data:

    while (fis.read(dataArray) != -1)
        ;

It needs to collect the data and set the value of your collected String.

Upvotes: 1

Thorn
Thorn

Reputation: 4057

I'm not sure about your program flow and if your two classes are running in the same thread, but it looks like you have a program flow issue. You are trying to open the file and getting a NullPointerException. Make sure the file is created and you have the correct reference to it before attempting to read.

If they are running in separate threads then you might try something like this:

try {
    int waitTries=1;
    fis = openFileInput(FILENAME);
    while(fis.available()<EXPECTEDSIZE && waitTries++<10)
        Tread.sleep(50);
} 

If you know how large the file should be (EXPECTEDSIZE is some constant you will set), then this may be what you are looking for.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692281

The stack trace tells you everything you need to know.

The error is a NullPointerException (meaning that you pass a null reference to a method expecting a non null reference, or that you call a method on a null reference).

The error occurs inside some android code (ContextWrapper.openFileInput()), which is called by your ChatService.onCreate() method, on line 25.

The line 25 is the following line:

fis = openFileInput(FILENAME);

So the error is clear: FILENAME is null. You haven't initialized it before this method is called.

Upvotes: 1

Related Questions