ROCKLAND, Maine — A case in which the state claimed an out-of-state private investigator was doing work in Maine without a license, and which took nearly three years to go to court, ended Tuesday morning as the jury was waiting to enter the courtroom.

Justice Jeffrey Hjelm ruled that the state law being cited by the district attorney’s office in charging Joshua A. Gray, 36, of Winchendon, Mass., was the wrong statute.

Hjelm ruled that because the case had reached the point of a jury being selected for the nearly three-year-old misdemeanor case, the state would not be allowed to refile the charge under the correct law.

Gray expressed relief after the ruling and said he had done nothing wrong.

“I feel like the state wasted a lot of people’s time and resources,” Gray said.

The case was being monitored by other professional investigators who were at Knox County Superior Court on Tuesday to attend the trial.

Gray, who is licensed in Massachusetts, said he had employed a private investigator licensed in Maine to follow someone suspected of workers compensation fraud. That investigator had not been able to get any useful information, so Gray said he came to Maine in April 2011 to give some pointers to the Maine investigator.

While he was waiting in Union for that investigator to show up, Gray said he witnessed the target of the investigation doing wheelies on an all-terrain vehicle. Gray then testified at a workers compensation hearing on what he had witnessed.

The attorney representing that man then filed a complaint with the Maine State Police, claiming that Gray was acting as a private investigator in Maine without a license, Gray said.

Maine State Police Detective David Pelletier issued Gray a summons on March 6, 2013, and the district attorney’s office filed its criminal complaint for improper conduct in private investigating on April 22, 2013. Gray’s attorney, Roger Hurley, had unsuccessfully attempted in October to have the case dismissed because of the long time it took to charge him, which he said denied his client the right to a speedy trial.

Hurley had also pointed out at the October hearing that Gray had applied for a license from Maine.

Hjelm said at Tuesday’s hearing that he discovered the discrepancy in the charge filed by the district attorney’s office on Tuesday morning. The Maine Legislature changed the laws regulating private investigators in 2011, and those took effect in September of that year. The prosecution had charged Gray under the new statute even though the offense had occurred several months earlier.

Assistant District Attorney Jeffrey Baroody asked the judge to amend the charge, arguing that there was no substantial change between the earlier law and the revised one. He said it was simply a rephrasing of the law. He also said that there was no double jeopardy involved since the jury had not yet come in the courtroom to be sworn in.

Hjelm disagreed, however, and denied the motion to amend.

Among those interested in the case, Alan Goodman, president of Lawyers Investigating Service Inc. of Portland, said his concern is that investigators from outside the state are coming into Maine and doing work without being licensed. He said Maine has considerable regulations, and it was not a fair playing field for out-of-state investigators to come in to the state and not be licensed here.

Gray said, however, that he never billed his client, an insurance company, for his visit to Maine, and therefore, he was not working as a private investigator at that time in Maine.