2017-10-13 134 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; }