I don't think that this is the solution to go. You have twice a check wether fos != null. Isn't there a solution which considers this state earlier in the code, making this check superfluous? Simply checking !=null at several places most of the time points to a more serious error in the code flow!