Question: I am looking for a way to stop a higher-level function after evaluating part of its input sequence.

Consider a situation when you look for the first index in a sequence that satisfies a certain condition. For example, let's say we are looking for the first position in an array `a` of `Int`s where the sum of two consecutive values is above 100.

You can do it with a loop, like this:

```
func firstAbove100(a:[Int]) -> Int? {
    if a.count < 2 {
        return nil
    }
    for i in 0..<a.count-1 {
        if a[i]+a[i+1] > 100 {
            return i
        }
    }
    return nil
}

```

The looping stops as soon as the position of interest is discovered.

We can rewrite this code using `reduce` as follows:

```
func firstAbove100(a:[Int]) -> Int? {
    if a.count < 2 {
        return nil
    }
    return (0..<a.count-1).reduce(nil) { prev, i in
        prev ?? (a[i]+a[i+1] > 100 ? i : nil)
    }
}

```

However, the disadvantage of this approach is that `reduce` goes all the way up to `a.count-2` even if it finds a match at the very first index. The result is going to be the same, but it would be nice to cut the unnecessary work.

Is there a way to make `reduce` stop trying further matches, or perhaps a different function that lets you stop after finding the first match?

Answer: 
The issue with your code lies in `readingTheFile()`:

This is the return-statement:

```
return null;

```

Which - Captain Obvious here - returns `null`. Thus `secret` is `null` and the `IllegalArgumentException` is thrown.

If you absolutely want to stick to the `BufferedReader`-solution, this should solve the problem:

```
byte[] readingTheFile(){
    byte[] result = null;

    try(BufferedReader br = new BufferedReader(new FileReader(path))){
        StringBuilder sb = new StringBuilder();

        String line;
        while((line = br.readLine()) != null)
            sb.append(line).append(System.getProperty("line.separator"));

        result = sb.toString().getBytes();
   }catch(IOException e){
        e.printStackTrace();
   }

   return result;

```

}

Some general advice:  

`BufferedReader` isn't built for the purpose of reading a file `byte` for `byte` anyways. E.g. `'\n'` will be ignored, since you're reading line-wise. This may cause you to corrupt data while loading. Next problem: you're only closing the `FileReader` in `readingTheFile()`, but not the `BufferedReader`. **Always** close the ToplevelReader, not the underlying one. By using `try-with-resources`, you're saving yourself quite some work and the danger of leaving the `FileReader` open, if something is coded improperly.

If you want to read the bytes from a file, use `FileReader` instead. That'll allow you to load the entire file as `byte[]`:

```
byte[] readingTheFile(){
    byte[] result = new byte[new File(path).length()];

    try(FileReader fr = new FileReader(path)){
        fr.read(result , result.length);
    }catch(IOException e){
        e.printStackTrace();
    }

    return result;
}

```

Or alternatively and even simpler: use `java.nio`

```
byte[] readingTheFile(){
    try{
        return Files.readAllBytes(FileSystem.getDefault().getPath(path));
    }catch(IOException e){
        e.printStackTrace();
        return null;
    }
}

```
Change your Class "File" like below, this is how it will provide you the desire mechanism. I've modified the same class you have written.

```
public class File {
public byte[] readingTheFile() throws IOException {

    java.io.File file = new java.io.File("/Users/user/Desktop/altiy.pdf");

    /*FileReader in = new FileReader("/Users/user/Desktop/altiy.pdf");
    BufferedReader br = new BufferedReader(in);
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    in.close();
     */
    FileInputStream fin = null;
    try {
        // create FileInputStream object
        fin = new FileInputStream(file);

        byte fileContent[] = new byte[(int) file.length()];

        // Reads bytes of data from this input stream into an array of
        // bytes.
        fin.read(fileContent);
        // returning the file content in form of byte array
        return fileContent;
    } catch (FileNotFoundException e) {
        System.out.println("File not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading file " + ioe);
    } finally {
        // close the streams using close method
        try {
            if (fin != null) {
                fin.close();
            }
        } catch (IOException ioe) {
            System.out.println("Error while closing stream: " + ioe);
        }
    }
    return null;
}
}

```