0

我知道我犯了一個愚蠢的錯誤,但不幸的是,即使經過大量的調試後,我仍然無法找到它。 我已經作出了類「naivebayes」和其他類連接未處理的異常:NullReferenceException:對象引用未設置爲

========================這是的方法連接 ==================

public NaiveBayes[] ReadOBj() 
    { 
     SqlConnection conn = new SqlConnection(connectionString); 
     conn.Open(); 

     SqlCommand command = new SqlCommand(@"SELECT NAME, CODE, DEPARTMENT, TS, CD, REPEAT, CORE, [Content], Grade FROM   Transcript WHERE  (Grade <> 'UNKNOWN')", conn); 
     SqlDataReader reader = null; 
     reader=command.ExecuteReader(); 
     NaiveBayes[] a=new NaiveBayes[10]; 
     NaiveBayes1.NaiveBayes na = new NaiveBayes(); 

     int i = 0; 
     while (reader.Read()) 
     { 
      i++; 

       string Namee = (string)reader["NAME"]; 
       Console.WriteLine(Namee); 
       na.Name = Namee; 

      string depte=reader["DEPARTMENT"].ToString(); 
      na.Dept = depte; 

      string core= reader["CORE"].ToString(); 
      Boolean.TryParse(core,out na.Core); 

      string rpet=reader["REPEAT"].ToString(); 
      Boolean.TryParse(core,out na.Repeat); 
      int tse,cde; 

       int.TryParse(reader["TS"].ToString(),out tse) ; 
       int.TryParse(reader["CD"].ToString(),out cde); 
      na.TS=tse; 
      na.CD=cde; 
       string contente=reader[7].ToString(); 
       na.Content = contente; 


      string grade=reader["Grade"].ToString(); 
      na.Grade = grade; 

      a[i] = na; 


     } 

     conn.Close(); 
     return a; 
    } 

1)問題是,當我嘗試訪問的NaiveBayes屬性它給空引用異常

 Forexample : 
      a[i].Name="ABC"; 
     This will raise the following Exception. 
    Unhandled Exception: System .NullReferenceException :Object Reference is not set to an instance of object 

2)第二個問題是,在[I]的所有對象必須具有不同的值,但該值被複制(

Forexample when i=2 ,and a[1].Name was "IstName" .and a[2].Name must be "2ndName". At the end both a[1].Name and a[2].Name has same value"2ndName" 

的最後一次迭代)====== ========================這是NaiveBayes類==================== ==

namespace NaiveBayes1 
{ 
    public class NaiveBayes 
    { 
    public string Name ; 
    public string Dept ; 
    public string Content ; 
    public string Grade ; 
    public Boolean Core ; 
    public Boolean Repeat; 
    public int TS ; 
    public int CD ; 


    public NaiveBayes() 
    { 

    Name = ""; 
    Dept = ""; 
    Content = ""; 
    Grade = ""; 
    Core = false; 
    Repeat = false; 
    TS = 0; 
    CD = 0;  
    } 
} 

問題2的================回答===================== ===

NaiveBayes[] na = new NaiveBayes[5]; 
    NaiveBayes[0].Name ="ABC" // NaiveBayes[0] is null. The array was allocated but not initialized. 
       // There is no NaiveBayes class to set the Name for. 

全部答案就在這裏What is a NullReferenceException, and how do I fix it?

+2

可能重複[什麼是一個NullReferenceException,如何解決呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i - 固定它) – Alberto

回答

1

我想這:

NaiveBayes1.NaiveBayes na = new NaiveBayes(); 

應在while循環等你拿在每次循環一個新的,否則你只是將每個相同的對象時間。

+0

雖然這是真的,它不是主要問題 – AbZy

3

您的第一個錯誤很可能來自於您的數據庫中少於10行的事實,因此不是所有10個數組元素都正在設置。

第二個問題是因爲您將相同的實例分配給每個數組元素。簡單的解決辦法:

NaiveBayes[] a=new NaiveBayes[10]; 
NaiveBayes1.NaiveBayes na; 

int i = 0; 
while (reader.Read()) 
{ 
    na = new NaiveBayes(); 
    i++; 

一個用於第一個問題提出的解決方案是使用列表而不是數組:

List<NaiveBayes> nbs = new List<NaiveBayes>(); 

int i = 0; 
while (reader.Read()) 
{ 
    NaiveBayes1.NaiveBayes na = new NaiveBayes(); 
    i++; 

    //... 

    nsb.Add(na); 
} 

所以後來當您嘗試使用它,你可以驗證對nbs.Count所需的索引屬性。或者您可以使用.ToArray()從您的方法中返回。

+0

第二個問題已解決,但第一個問題仍然存在 – Charlie

+0

因爲NaiveBayes [] a =新NaiveBayes [gettotalRecords()];或者沒有工作 – Charlie

+0

@Charlie所以停止訪問數組的邊界外。我增加了另一種可能性,但尚不清楚你想達到什麼目的。 – BartoszKP

1

您的數據庫是一個通用選擇語句,但您預計會有10個記錄回來。如果少於10條記錄,則會得到Null Reference Exception,如果超過10條,您將得到一個Index Out of Range Exception。要解決此問題,請使用List或其他可調整大小的容器。

此外,重複值的第二個問題可能是因爲您正在循環中重用NaiveBayes類的實例。

另外,由於相當多的變量充當數據讀取器和對象之間的媒介,我簡化了。的

public NaiveBayes[] ReadOBj() 
{ 
    SqlConnection conn = new SqlConnection(connectionString); 
    SqlCommand command = new SqlCommand(@"SELECT NAME, CODE, DEPARTMENT, TS, CD, REPEAT,  CORE, [Content], Grade 
    FROM Transcript 
    WHERE (Grade <> 'UNKNOWN')", conn); 

    conn.Open(); 
    SqlDataReader reader = command.ExecuteReader(); 
    List<NaiveBayes> a = new List<NaiveBayes>(); 

    while (reader.Read()) 
    { 
     NaiveBayes1.NaiveBayes na = new NaiveBayes(); 
     na.Name = (string)reader["NAME"]; 
     na.Dept = reader["DEPARTMENT"].ToString(); 
     Boolean.TryParse(reader["CORE"].ToString(), out na.Core); 
     Boolean.TryParse(reader["REPEAT"].ToString(),out na.Repeat); 
     int.TryParse(reader["TS"].ToString(),out na.TS) ; 
     int.TryParse(reader["CD"].ToString(),out na.CD); 
     na.Content = reader["Content"].ToString(); 
     na.Grade = reader["Grade"].ToString(); 

     a.Add(na); 
    } 

    conn.Close(); 
    return a.ToArray(); 
} 
相關問題