We first point out it is conditional to apply the variational approach to the nonlocal nonlinear Schrödinger equation (NNLSE), that is, the response function must be an even function. Different from the variational approach, the canonical Hamiltonian formulation for the first-order differential system are used to deal with the problems of the nonlinear light-envelope propagations. The Hamiltonian of the system modeled by the NNLSE is obtained, which can be expressed as the sum of the generalized kinetic energy and the generalized potential. The solitons correspond to extreme points of the generalized potential. The stabilities of solitons in both local and nonlocal nonlinear media are also investigated by the analysis of the generalized potential. They are stable when the potential has minimum, and unstable otherwise.