As others have said in the comments, you'll need to test your water quality to get a good idea of what the problem could be. It's very difficult to guess what's wrong with your fish (be it water quality, parasite or any number of other factors).
The first steps for debugging, however, is to gather information about the situation you're in and check it against a known formula that works:
- Make sure your tank is sufficiently sized for your fish (At least 20 Litres for a Betta).
- Check your tank is heated to 23-27 Celsius
- Ensure that your filtration is sufficient to turn your water over a number of times per hour (normally filters are rated for tank volume (in litres))
- Check that your levels of ammonia, nitrite and nitrate are acceptable (use a freshwater test kit for this).
The erratic swimming can be a sign that something isn't right with your fish. This behaviour can also be exhibited when the fish is 'spooked' by something in the environment it is in and begins to panic. Often, these triggers can be sudden changes in the tank surroundings - someone entering a room, for example.
Your fish could injure itself by hitting the walls of the tank but more likely is that the injury will result from gravel substrate or a pointy decoration. Remove any sharp decorations but ensure you replace it with soft cover so your fish doesn't panic. You could use java moss or another soft plant. You may also wish to turn your tank light off to relax the fish more.
Ultimately, you can't know what is causing this unless you're in possession of the facts. Testing your water is the first step.