That question exhibits a host of basic problems. The critical ones are:
The error message is not reproduced, making the problem difficult to ascertain.
A large block of code is posted, unedited (and badly formatted).
No evidence of any effort to solve the problem or even isolate it is provided.
No explanation is given concerning what the code is intended to do.
Any one of these would be ample reason to close the question. In general, this community and its moderators are very tolerant (perhaps because many GIS people are not really programmers) and will make extra efforts to improve or even answer questions that exhibit only one or two of these problems. But when just about every aspect of a question indicates it's a bad one, it gets closed fast.
(About the only thing that could make this question worse would be if the O.P. demanded answers right now because they're in a hurry...)
Some less-critical problems include
The SE philosophy about questions of the form "what's wrong with my code" is clearly explained in a meta thread entitled Does SO discourage questions like “this is my code, please find the bug”?. The answers are unequivocal: such questions do not belong. To be taken seriously as the originator of a question anywhere on SE you should exhibit a minimum of common sense and courtesy for your readers:
Isolate the part of the code that causes the problem so we don't have to wade through pages of irrelevant stuff;
Tell us what environment it's running in so we can tell as quickly as possible whether we have the capability to understand the code and reproduce its behavior;
Give an accurate account of the bad or unexpected behavior--we cannot read your mind, no matter how gifted you think we may be;
Tell us what behavior is intended or expected--or be prepared to get answers that may make your code work, but in the wrong way!
It shouldn't be necessary to state these things: they are not particular demands made here at SE, but are what anybody with any kind of computing problem needs to do in order to address their problem.