This is a really normal problem that all males go through during adolescence. It's not that there's something wrong with you, it's just that your hormones are going bat-shit nuts right now.
I'm not sure how old you are, but if you're in your mid to late teens, this is gonna happen a lot, and it's really, really normal. I promise that every single male on the face of the earth goes/went through it too.
The whole issue should go away by the time you reach your late teens/early twenties, but in the mean time, one thing that you can do to prevent this little problem from coming up (no pun intended, I assure you) is masturbating.
After a male reaches climax, his brain chemistry changes and his libido dies for a time, and he won't be able to get another erection for a couple hours or so. It should help take care of this little mishap for you. It helped me when I had this issue in school.
If this isn't practical, you could try crossing your legs and focusing on something else, like a movie you saw recently or what you're planning to do after you finish your homework that night. If she starts to strike up a conversation with you, focus on the conversation and do your best to keep it going. Getting rid of an erection can actually be rather easy.
I hope that this was able to help you out somewhat. Don't fret over random erections; that's as common as breathing for a teenage boy.
This will go away after a while. Soon it won't happen as often and it'll be fairly easy to control. Take care! And don't worry about it. Every other guy in your school has the same issue. I guarantee it.