You could have had your state pension stolen from you without knowing hit. A huge flaw in the National Insurance (NI) system means millions of people have paid for years but haven't had it credited.

I want to explain how to check if affected and how to fix it.

How the state pension works

You don't automatically get the full state pension on retirement, it depends on how many years you've paid national insurance for; fewer years means less pension. You either accrue NI while working or if you're on certain benefits.

How it works depends on when you retire:

• Anyone who retired after April 2010 or has yet to retire. To get the full basic state pension (currently £97.65 a week), you need to have paid NI for 30 years. If you're short, you'll usually get a pro-rata sum. So if you have half the number of NI years, you get £48.83 a week.

• Anyone who retired before April 2010. If you hit retirement age before 6 April 2010 and you've less than 25% of the required NI years, you're not entitled to a basic state pension. If you've more than 25%, the exact amount you get depends on your gender but, as always, the more years you pay, the more you get.

Either way, if you’re missing years, this can reduce the pension you get, which means you miss out week after week, year after year.

What’s the problem?

It's been revealed authorities haven't tracked 9m employees' payments between 2004 and 2009, £1.3 billion worth.

The Revenue (HMRC) says problems arise when employers send forms with the wrong NI number for an employee. The problem isn’t exclusive to any one group. It affects workers of all ages, so everyone needs to check.

How does this relate to the tax code error?

It doesn’t, though it's another nasty PR hit to the Revenue’s reputation. With the tax code fiasco, millions of people paid the wrong tax, as their tax code (the instruction to employers about how much tax to take) was incorrect. Most of the errors meant people overpaid, so you could be due £1,000s back. The Revenue's only gone back a few years so far, and people are due letters for those dates. Yet it's crucial that everyone checks their tax code's correct – use my free www.taxcodechecker.com to find if you're owed a rebate.

Some folks have underpaid too, and the Revenue will want the money back; there’s little you can do about it.

Back to NI – how do I know if I’m missing years?

The most obvious way is if HMRC wrote to you saying you're missing years. If that seemed strange, it could mean payments didn't track.

To check your situation, dig out the letter or call 0845 915 5996. Ask which years it has you down as paying NI and compare to the years you worked.

The Department for Work and Pensions also provides a state pension forecast to those approaching retirement, which details missing NI years. You can also look at the P60 the employers gives you each April. Is the NI number correct? If it's not, payments may be untracked.

How do I correct a mistake?

If you spot an error, contact HMRC's NI helpline on 0845 302 1479. It may ask you to simply tell your employer the correct NI number.

But if the old employer's gone bust, been taken over or destroyed records, you may need evidence from old P60s or wage slips.

If you've no paperwork or proof, you may still be able to get the rebate. Write to the National Insurance contributions office with details (full name, address, date of birth, where you worked, copies of payslips). It could still match the missing contributions to you.