Help in printing n number of lines if a search string matches in a file

Hi
I have below script which is used to grep specific errors and if error string matches send an email alert.
Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert

Current code:-

#!/bin/bash
tail -Fn0 --retry /tmp/error.log | \
while read line ; do
        echo "$line" | egrep "java.lang.NullPointerException|java.lang.IllegalStateException|java.sql.SQLException"
        if [[ $? = 0 ]] ;
        then
HOSTNAME=abc.com
               SUBJECT="Error in "$HOSTNAME" Reported"
MESSAGE="/tmp/message.txt"
               TO="name@xyz.com"
echo "$line" > $MESSAGE
                /bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
fi
done

Appreciate any inputs.
I wish to keep the changes to code as minimal as possible.

Please use code tags as required by forum rules!

One option, just dreamed up from the blue sky: Set up a counter, after the read , incremented by 0 until your grep matches. Then, increment by 1 and collect $line into the message file. When the counter hits 10, send the mail, reset increment to 0, clear the file, and continue.
Or, do the entire thing in e.g. awk .

BTW, you can take all the variable assignments out of the loop as it seems they remain constant.

Don't do it in real time. Don't use tail -f which seems to me the problem.

Once an hour, or whatever, use tail to save the end part of the log file that has not been examined before. Save the end part to "tail.txt" for example. Use grep -C on tail.txt to get the context for each error line detected. Record total number of lines in the log file. Suppose there are 1000. Next time (in an hour), tail starts with line 1001 (or maybe 991 to make sure you have context).