當前位置: 首頁>>代碼示例>>C#>>正文


C# NullReferenceException類代碼示例

本文整理匯總了C#中System.NullReferenceException的典型用法代碼示例。如果您正苦於以下問題:C# NullReferenceException類的具體用法?C# NullReferenceException怎麽用?C# NullReferenceException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NullReferenceException類屬於System命名空間,在下文中一共展示了NullReferenceException類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main(string[] args)
   {
      int value = Int32.Parse(args[0]);
      List<String> names;
      if (value > 0)
         names = new List<String>();
      
      names.Add("Major Major Major");       
   }
}
// Compilation displays a warning like the following:
//    Example1.cs(10) : warning BC42104: Variable //names// is used before it 
//    has been assigned a value. A null reference exception could result 
//    at runtime.
//    
//          names.Add("Major Major Major")
//          ~~~~~
開發者ID:.NET開發者,項目名稱:System,代碼行數:23,代碼來源:NullReferenceException

輸出:

Unhandled Exception: System.NullReferenceException: Object reference 
not set to an instance of an object.
at Example.Main()

示例2: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      List<String> names = new List<String>();
      names.Add("Major Major Major");
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:12,代碼來源:NullReferenceException

示例3: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
       int[] values = null;
       for (int ctr = 0; ctr <= 9; ctr++)
          values[ctr] = ctr * 2;
          
       foreach (var value in values)
          Console.WriteLine(value);   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:15,代碼來源:NullReferenceException

輸出:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main()

示例4: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
       int[] values = new int[10];
       for (int ctr = 0; ctr <= 9; ctr++)
          values[ctr] = ctr * 2;
          
       foreach (var value in values)
          Console.WriteLine(value);   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:15,代碼來源:NullReferenceException

輸出:

0
2
4
6
8
10
12
14
16
18

示例5: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                          "Abraham", "Adrian", "Ariella", 
                                          "Arnold", "Aston", "Astor" } );    
      String nameToFind = "Robert";
      Person found = Array.Find(persons, p => p.FirstName == nameToFind);
      Console.WriteLine(found.FirstName);
   }
}

public class Person
{
   public static Person[] AddRange(String[] firstNames) 
   {
      Person[] p = new Person[firstNames.Length];
      for (int ctr = 0; ctr < firstNames.Length; ctr++)
         p[ctr] = new Person(firstNames[ctr]);

      return p;
   }
   
   public Person(String firstName)
   {
      this.FirstName = firstName;
   } 
   
   public String FirstName;
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:34,代碼來源:NullReferenceException

輸出:

Unhandled Exception: System.NullReferenceException: 
Object reference not set to an instance of an object.
at Example.Main()

示例6: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                          "Abraham", "Adrian", "Ariella", 
                                          "Arnold", "Aston", "Astor" } );    
      String nameToFind = "Robert";
      Person found = Array.Find(persons, p => p.FirstName == nameToFind);
      if (found != null)
         Console.WriteLine(found.FirstName);
      else
         Console.WriteLine("{0} not found.", nameToFind);   
   }
}

public class Person
{
   public static Person[] AddRange(String[] firstNames) 
   {
      Person[] p = new Person[firstNames.Length];
      for (int ctr = 0; ctr < firstNames.Length; ctr++)
         p[ctr] = new Person(firstNames[ctr]);

      return p;
   }
   
   public Person(String firstName)
   {
      this.FirstName = firstName;
   } 
   
   public String FirstName;
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:37,代碼來源:NullReferenceException

輸出:

Robert not found

示例7: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      var pages = new Pages();
      if (! String.IsNullOrEmpty(pages.CurrentPage.Title)) {
         String title = pages.CurrentPage.Title;
         Console.WriteLine("Current title: '{0}'", title);
      }
   }
}

public class Pages 
{
   Page[] page = new Page[10];
   int ctr = 0;
   
   public Page CurrentPage
   {
      get { return page[ctr]; }
      set {
         // Move all the page objects down to accommodate the new one.
         if (ctr > page.GetUpperBound(0)) {
            for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
               page[ndx - 1] = page[ndx];
         }    
         page[ctr] = value;
         if (ctr < page.GetUpperBound(0))
            ctr++; 
      }
   }
   
   public Page PreviousPage
   {
      get {
         if (ctr == 0) { 
            if (page[0] == null)
               return null;
            else
               return page[0];
         }
         else {
            ctr--;
            return page[ctr + 1];
         }
      }
   }         
}

public class Page
{
   public Uri URL;
   public String Title;
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:57,代碼來源:NullReferenceException

輸出:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main()

示例8: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      var pages = new Pages();
      Page current = pages.CurrentPage;
      if (current != null) {  
         String title = current.Title;
         Console.WriteLine("Current title: '{0}'", title);
      }
      else {
         Console.WriteLine("There is no page information in the cache.");
      }   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:18,代碼來源:NullReferenceException

輸出:

There is no page information in the cache.

示例9: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      String[] values = { "one", null, "two" };
      for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
         Console.Write("{0}{1}", values[ctr].Trim(), 
                       ctr == values.GetUpperBound(0) ? "" : ", "); 
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:NullReferenceException

輸出:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main()

示例10: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      String[] values = { "one", null, "two" };
      for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
         Console.Write("{0}{1}", 
                       values[ctr] != null ? values[ctr].Trim() : "", 
                       ctr == values.GetUpperBound(0) ? "" : ", "); 
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:15,代碼來源:NullReferenceException

輸出:

one, , two

示例11: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      List<String> names = GetData();
      PopulateNames(names);
   }

   private static void PopulateNames(List<String> names)
   {
      String[] arrNames = { "Dakota", "Samuel", "Nikita",
                            "Koani", "Saya", "Yiska", "Yumaevsky" };
      foreach (var arrName in arrNames)
         names.Add(arrName);
   }
   
   private static List<String> GetData() 
   {
      return null;   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:25,代碼來源:NullReferenceException

輸出:

Unhandled Exception: System.NullReferenceException: Object reference 
not set to an instance of an object.
at Example.PopulateNames(List`1 names)
at Example.Main()


注:本文中的System.NullReferenceException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。