In this case the Plaintiff, James Maloney, has previously been charged with a crime for possession of nunchucks. He is apparently suing to enjoin further enforcement of the law under which he was previously charged.
US Federal courts will only take up a "case or controversy", which means an issue where actual, not theoretical rights are at stake, and in order to vindicate a constitutional right, it must either have previously been violated, or there must be a plausible and immediate threat to it.
Claiming that a law is unconstitutional as part of a defense to a charge of violating that law is a common and probably the best known method of challenging a law (or a government action) for unconstitutionality. But there are other ways. One way is to apply for an injunction against enforcement of the law. That is the procedure that was followed in, for example, the recent case where there was a ruling against the ADA in a district court. In order to use that procedure, the plaintiff must present evidence that there is a credible threat that the law will be invoked against him (or her) if the action which the plaintiff claims is protected by a constitutional right is taken.
In short, one need not put oneself in a position where one goes to prison or is found guilty of a crime if one loses the case to challenge the constitutionality of a law or of a government policy or action. But one must establish that it is a real "case or controversy", with real parties in opposition to each other, and real rights at stake, not a mere law student's exercise, nor a collusive case, with both "sides" having the same actual goal.
In the case reported, the previous criminal charge helps to establish that this is a real issue.