2017-10-13 135 views
-2

我遇到了堆栈溢出异常的问题,但我无法知道是什么原因导致异常被抛出。我正在使用一个类库,它包含我需要的所有方法和对象,并从控制台应用程序运行它。没有递归的堆栈溢出异常

任何帮助将不胜感激,因为这是在几个小时内到期的任务的一部分。

这里是我的代码:

TrafficIncidentNotificationRadiusCalculator类

namespace TrafficIncident 
{ 
public class TrafficIncidentNotificationRadiusCalculator 
{ 
    public double meters; 
    public double CONFIGURED_NOTIFICATION_RADIUS 
    { 
     get { return CONFIGURED_NOTIFICATION_RADIUS; } 
     set { CONFIGURED_NOTIFICATION_RADIUS = meters; } 
    } 

    public List<string> GetNotificationRecipientsList(List<User> users, List<UserLocationUpdate> userLocation, TrafficIncidentReport report) 
    { 
     int i = 0; 
     List<string> userNotificationIds = new List<string>(); 
     while (i < userLocation.Count) 
     { 
      UserLocationUpdate userLoc = userLocation.ElementAt(i); 
      userNotificationIds.Add(userLoc.userNotificationId); 
      Console.WriteLine(userNotificationIds.ElementAt(i)); 
      i++; 
     } 
     return userNotificationIds; 
    } 
} 
} 

TrafficIncidentReport类

namespace TrafficIncident 
{ 
public class TrafficIncidentReport 
{ 
    public double[] incidentLocation; 

    public double latitude 
    { 
     get { return latitude; } 
     set { latitude = value; } 
    } 

    public double longitude 
    { 
     get { return longitude; } 
     set { longitude = value; } 
    } 

    public void SetIncidentLocation() 
    { 
     incidentLocation = new double[] { latitude, longitude }; 
    } 

    public double[] GetIncidentLocation() 
    { 
     return incidentLocation; 
    } 
} 
} 

User类

namespace TrafficIncident 
{ 
public class User 
{ 
    public string userFName 
    { 
     get { return userFName; } 
     set { userFName = value; } 
    } 

    public string userLName 
    { 
     get { return userLName; } 
     set { userLName = value; } 
    } 
} 
} 

使用者1 ocationUpdate类

namespace TrafficIncident 
{ 
public class UserLocationUpdate 
{ 
    public string userNotificationId 
    { 
     get { return userNotificationId; } 
     set { userNotificationId = value; } 
    } 

    public double lastKnownLatitude 
    { 
     get { return lastKnownLatitude; } 
     set { lastKnownLatitude = value; } 
    } 

    public double lastKnownLongitude 
    { 
     get { return lastKnownLongitude; } 
     set { lastKnownLongitude = value; } 
    } 
} 
} 

然后这是一个类库运行从控制台应用程序:

namespace ClassLibraryTestApp 
{ 
class Program 
{ 

    static void Main(string[] args) 
    { 
     List<User> users = new List<User>(); 
     List<UserLocationUpdate> userLocation = new List<UserLocationUpdate>(); 

     User user1 = new User(); 
     user1.userFName = "Scott"; 
     user1.userFName = "Gersbank"; 
     users.Add(user1); 

     User user2 = new User(); 
     user2.userFName = "John"; 
     user2.userFName = "Smith"; 
     users.Add(user2); 

     User user3 = new User(); 
     user3.userFName = "James"; 
     user3.userFName = "Moore"; 
     users.Add(user3); 

     UserLocationUpdate user1Location = new UserLocationUpdate(); 
     user1Location.lastKnownLatitude = 0; 
     user1Location.lastKnownLongitude = 0; 
     user1Location.userNotificationId = "user1"; 
     userLocation.Add(user1Location); 

     UserLocationUpdate user2Location = new UserLocationUpdate(); 
     user1Location.lastKnownLatitude = 1; 
     user1Location.lastKnownLongitude = 1; 
     user1Location.userNotificationId = "user2"; 
     userLocation.Add(user2Location); 

     UserLocationUpdate user3Location = new UserLocationUpdate(); 
     user1Location.lastKnownLatitude = 2; 
     user1Location.lastKnownLongitude = 2; 
     user1Location.userNotificationId = "user3"; 
     userLocation.Add(user3Location); 

     TrafficIncidentReport trafficReport = new TrafficIncidentReport(); 
     trafficReport.latitude = 1; 
     trafficReport.longitude = 1; 
     trafficReport.SetIncidentLocation(); 

     TrafficIncidentNotificationRadiusCalculator TINRC = new TrafficIncidentNotificationRadiusCalculator(); 
     TINRC.meters = 20000; 
     TINRC.GetNotificationRecipientsList(users, userLocation, trafficReport); 
    } 
} 
} 
+3

堆栈跟踪(的部分)将是非常有益的在这里。 – Ivar

+1

它在哪个部分引发异常? – SeM

+5

*您的所有属性似乎都是递归的。 (如果你要提供财产方法的机构,他们不应该指自己) –

回答

5

这并不是创建属性正确的方式,定义一个私有字段,则该属性本身:在你的情况下,它会递归地调用set_latitude()方法并导致堆栈溢出异常。

错误:

public double latitude 
{ 
    get { return latitude; } 
    set { latitude = value; } 
} 

右:

private double latitude 

public double Latitude 
{ 
    get { return latitude; } 
    set { latitude = value; } 
} 

或者使用Auto-Implemented Properties

public double Latitude { get; set; } 
1

你的代码递归分配开始,第一个递归是在这里:

public double meters; 
public double CONFIGURED_NOTIFICATION_RADIUS 
{ 
    get { return CONFIGURED_NOTIFICATION_RADIUS; } 
    set { CONFIGURED_NOTIFICATION_RADIUS = meters; } 
} 

有什么不对:

每当你一定值设置为一个属性它的setter将触发, ,每当你访问一个属性的值的setter方法 触发。在上述情况下,你是在它分配属性 值的设置方法,这将反复触发你得到的异常

查看所有的getter和setter是错误的setter和 汉斯,你应该使用一个备份变量或者使用它们作为{get;set}。在userNotificationId的情况下,你应该定义属性为如下所示:

private _UserNotificationId 
public string UserNotificationId 
{ 
    get { return _UserNotificationId; } 
    set { _UserNotificationId= value; } 
} 

或者干脆

public string UserNotificationId { get; set; }