2016-12-31 12 views
2

次のコードを使用するアプリケーションを開発しています。 「nullオブジェクト参照で仮想メソッドを呼び出そうとしています」という予期しないエラーが発生しています。私はこれが起こっている理由を理解していません。エラーは、t[i].setTestname(getData(exams[i]));を含む行としてスローされます。誰かが私が間違っていることを指摘してもらえますか?ここでいくつかの助けを使うことができました。次のようにnullオブジェクト参照の問題で仮想メソッドを呼び出そうとしました

void processTestPerformance() 
{ 
    String exams[],rawdata; 
    rawdata=data.toString(); 
    int numberoftests=getNumbers("tabletitle03",rawdata); 
    Tests[] t= new Tests[numberoftests]; 
    exams=new String[numberoftests]; 
    rawdata=rawdata+"tabletitle03"; 
    for(int i=0;i<numberoftests;i++) 
    { 
     int j=rawdata.indexOf("tabletitle03"); 
     int k=(rawdata.substring(j+1)).indexOf("tabletitle03"); 
     exams[i]=rawdata.substring(j,j+1+k); 
     t[i].setTestname(getData(exams[i])); 
     rawdata=rawdata.substring(k); 
    } 
} 

クラスのテストのためのコードは次のとおりです。事前に

public class Tests 
{ 
    public int numberofsubjects; 
    public String testname; 
    public Subject s[]; 
    public void setS(Subject[] s) 
    { 
     this.s = s; 
    } 
    public void setNumberofsubjects(int numberofsubjects) 
    { 
     this.numberofsubjects = numberofsubjects; 
     s=new Subject[numberofsubjects]; 
    } 
    public void setTestname(String testname) 
    { 
     this.testname = testname; 
    } 
} 

感謝。

答えて

1

あなたはその配列内に見える場合がnullのシーケンスを見つけるサイズnumberoftests

Testsクラスの空の配列を作成します。あなたはそれを決して初期化しないからです。

t[i]がクラスのインスタンスを返すように配列を設定するだけで済みます。

t[i] = new Tests(); 
// now you can manipulate the object inside the array 
t[i].etTestname(getData(exams[i])); 
0
for(int i=0;i<numberoftests;i++) 
     t[i]=new Tests(); 

私の問題を解決:あなたのためのサイクルあなたは、たとえば、既定のコンストラクタを使用することができて

関連する問題