Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why NULL reference exception?
Message
De
27/06/2013 12:00:37
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01577321
Message ID:
01577322
Vues:
75
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>I can not understand why do I have NULL reference exception on this line
>
>
>Decimal bookingNo = 4000001;
>         BookingRow bRow = bookings.GetBookingRow(bookingNo);
>
bookings is null I expect - put a breakpoint and use the debugger




>where GetBookingRow is defined this way:
>
>
>internal BookingRow GetBookingRow(Decimal bookingNo)
>      {
>         BookingRow bookingRow = null;
>         using (SqlCommand sqlCommand = new SqlCommand())
>         {
>            sqlCommand.CommandText = @"SELECT * 
>                  FROM dbo.b_sched 
>                   WHERE booking_id = @booking_id;";
>
>            sqlCommand.CommandType = CommandType.Text;
>            sqlCommand.Parameters.Add("@booking_id", SqlDbType.BigInt).Value = bookingNo;
>
>            DataSet ds;
>            String messageText ="";
>            Int32 statusCode = 0;
>            if (database.ExecuteSqlCommand(sqlCommand, out ds, ref messageText, ref statusCode))
>            {
>               if (1 == ds.Tables[0].Rows.Count)
>                  bookingRow = new BookingRow(ds.Tables[0].Rows[0]);
>            }
>         }
>         return bookingRow;
>      }
>
>The method is not being hit at all, I get an error right at the call. I put the breakpoint into the method, but it's not being activated.
>
>So, what is wrong here?
>
>Thanks in advance.
>
>
>System.NullReferenceException was unhandled by user code
>
>The class itself is rather simple
>
>
>/// <summary>
>   /// Booking Row class
>   /// </summary>
>   public class BookingRow
>   {
>      public Decimal BookingId {get; set;}
>      public String Resource { get; set; }
>      public String RequiredResource { get; set; }
>      public Int16 Quantity { get; set; }      
>      public Byte Sex { get; set; }
>      public String SexName { get; set; }
>      public Byte Experience { get; set; }
>      public Boolean Locked { get; set; }      
>      public Byte InstructorType { get; set; }
>      public DateTime? StartTime { get; set; }
>      public DateTime? EndTime { get; set; }
>      public String PayStatus {get;set;}
>    
>      public String Notes { get; set; }
>      public List<Byte> PreferenceLevels = new List<Byte>();
>
>      public List<Boolean> CriteriaBit = new List<Boolean>();
>      public Int16 Criteria1 { get; set; }
>      public Int32 Criteria4 {get; set;}
>      public Decimal Criteria5 { get; set; }
>      public Byte Criteria6 { get; set; }
>      public Byte Criteria7 { get; set; }
>      public Dictionary<String, String> Levels = new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase);
>
>      public BookingRow()
>      {
>
>      }
>      
>      public BookingRow(DataRow row)
>      {
>         BookingId = row.Field<Decimal>("booking_id");
>         Resource = row.Field<String>("resource1");
>         Quantity = row.Field<Int16>("quantity");
>         RequiredResource = row.Field<String>("resrc_req1");
>         PayStatus = row.Field<String>("pay_status");
>         Locked = row.Field<Boolean>("locked");
>         InstructorType = row.Field<Byte>("instr_type");
>         Experience = row.Field<Byte>("experience");
>         StartTime = row.Field<DateTime?>("start_time");
>         EndTime = row.Field<DateTime?>("end_time");
>
>         Sex = row.Field<Byte>("sex");
>         if (Sex > 1)
>            SexName = "Male";
>         else
>            SexName = "Female";        
>
>         Levels.Add("alpine", row.Field<String>("tch_level"));
>         Levels.Add("snowboard", row.Field<String>("snb_level"));
>         Levels.Add("telemark", row.Field<String>("tele_level"));
>         Levels.Add("other", row.Field<String>("misc_level"));
>         Notes = row.Field<String>("notes");
>
>         Criteria1 = row.Field<Int16>("criteria1");
>         Criteria4 = row.Field<Int32>("criteria4");
>         Criteria6 = row.Field<Byte>("criteria6");
>         Criteria7 = row.Field<Byte>("criteria7");
>
>         for (int i = 1; i <= 12; i++)
>         {
>            if (i >=2 && i<=3)
>               CriteriaBit.Add(row.Field<Boolean>(String.Format("criteria{0}", i)));
>
>          
>            PreferenceLevels.Add(row.Field<Byte>(String.Format("prev_lvl{0}", i)));            
>         }       
>      }
>   }
>
>What am I missing here?
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform