Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/10/12 12:10:22 (12 years ago)
Author:
ascheibe
Message:

#1890 fixed a NullReferenceException when serializing a calculator that has not been assigned a formula

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionModification/Calculator.cs

    r7228 r8778  
    33using System.Globalization;
    44using System.Linq;
     5using System.Text;
    56using System.Text.RegularExpressions;
    67using HeuristicLab.Common;
     
    89using HeuristicLab.Data;
    910using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    10 using System.Text;
    1111
    1212namespace HeuristicLab.Optimization {
     
    2121    [Storable]
    2222    public string Formula {
    23       get { return string.Join(" ", tokens); }
     23      get { return tokens == null ? string.Empty : string.Join(" ", tokens); }
    2424      set { tokens = Tokenize(value).ToList(); }
    2525    }
     
    5656      int i = 0;
    5757      try {
    58         for (; i<tokens.Count; i++) {
     58        for (; i < tokens.Count; i++) {
    5959          var token = tokens[i];
    6060          double d;
     
    6464            stack.Push(GetVariableValue(variables, token.Substring(1, token.Length - 2).Replace("\\\"", "\"")));
    6565          } else if (token.StartsWith("'")) {
    66             stack.Push(token.Substring(1, token.Length-2).Replace("\\'", "'"));
     66            stack.Push(token.Substring(1, token.Length - 2).Replace("\\'", "'"));
    6767          } else {
    6868            Apply(token, stack, variables);
    6969          }
    7070        }
    71       } catch (Exception x) {
     71      }
     72      catch (Exception x) {
    7273        throw new Exception(string.Format(
    7374          "Calculation of '{1}'{0}failed at token #{2}: {3} {0}current stack is: {0}{4}", Environment.NewLine,
    74           Formula, i, TokenWithContext(tokens, i, 3), 
     75          Formula, i, TokenWithContext(tokens, i, 3),
    7576          string.Join(Environment.NewLine, stack.Select(AsString))),
    7677          x);
     
    8990    private string TokenWithContext(List<string> tokens, int i, int context) {
    9091      var sb = new StringBuilder();
    91       if (i > context+1)
     92      if (i > context + 1)
    9293        sb.Append("... ");
    9394      int prefix = Math.Max(0, i - context);
     
    104105    private static void Apply(string token, Stack<object> stack, IDictionary<string, IItem> variables) {
    105106      switch (token) {
    106         case "null":  stack.Push(null); break;
    107         case "true":  stack.Push(true); break;
     107        case "null": stack.Push(null); break;
     108        case "true": stack.Push(true); break;
    108109        case "false": stack.Push(false); break;
    109110
     
    186187      try {
    187188        stack.Push(func(a));
    188       } catch (Exception) {
     189      }
     190      catch (Exception) {
    189191        stack.Push(a);
    190192        throw;
     
    199201      try {
    200202        stack.Push(func(a, b));
    201       } catch (Exception) {
     203      }
     204      catch (Exception) {
    202205        stack.Push(b);
    203206        stack.Push(a);
     
    214217      try {
    215218        stack.Push(func(a, b, c));
    216       } catch (Exception) {
     219      }
     220      catch (Exception) {
    217221        stack.Push(a);
    218222        stack.Push(b);
Note: See TracChangeset for help on using the changeset viewer.