2016-03-30 7 views
-1

私は学生のメリットリストを印刷するプログラムを作ろうとしています。サブクラスメソッドmsortの呼び出しが行われると、エラーが発生します。プログラムはマークに従ってソートし、合計マークが同じであれば、個々のマークをチェックする別の関数を呼び出します。サブクラスメソッドの呼び出しが行われたときに、NULLポインタ例外が発生するのはなぜですか?

import java.util.*; 


class Student { 
    int[] ph, ch, ma; 
    int[] total; 
    int[] total_sort; 

    Student() { 
    } 

    // length of array and marks are received via constructor. 
    Student(int x, int[] p, int[] c, int[] m) { 

     // an array of p,c,m along with 2 arrays of total marks is made. 
     total = new int[x]; 
     total_sort = new int[x]; 
     ph = new int[x]; 
     ch = new int[x]; 
     ma = new int[x]; 
     for (int i = 0; i < x; i++) { 
      ph[i] = p[i]; 
      ch[i] = c[i]; 
      ma[i] = m[i]; 
      total[i] = (p[i] + c[i] + m[i]); 
      total_sort[i] = total[i]; 
     } 
    } 

    // sorts the array accoring to the total marks 
    void Sort() { 

     // to use subclass method. 
     Stud_new t = new Stud_new(); 

     for (int d = 0; d < total.length - 1; d++) { 
      for (int f = 0; f < total.length - d - 1; f++) { 
       if (total_sort[f] < total_sort[f + 1]) { 
        int temp = total_sort[f]; 
        total_sort[f] = total_sort[f + 1]; 
        total_sort[f + 1] = temp; 

       } 
       // checks for higher maths marks. 
       else if (total_sort[f] == total_sort[f + 1]) { 
        int m = t.mSort(f); 
        int temp1 = total_sort[f]; 
        total_sort[f] = total_sort[m]; 
        total_sort[f + 1] = temp1; 
       } 
      } 
     } 
    } 

    /* returns the index from original total array so as to identify the students' name. 
    * marks from sorted array are compared with original array to find the index of marks in originial array . 
    * this index is used to find the students' name. 
    */ 
    int GetList(int a) { 
     for (int j = 0; j < total.length; j++) { 
      if (total[j] == a) { 
       return j; 
      } 
     } 
     return -1; 
    } 
} 

class Stud_new extends Student { 

    int cSort(int ci) { 
     if (ch[ci] > ch[ci + 1]) { 
      return ci; 
     } else if (ch[ci] < ch[ci + 1]) { 
      return (ci + 1); 
     } 

     //else if(ph[pi] == ph[pi + 1]) 
     //csort(pi); 
     return -1; 
    } 

    int pSort(int pi) { 

     if (ph[pi] > ph[pi + 1]) { 
      return pi; 
     } else if (ph[pi] < ph[pi + 1]) { 
      return (pi + 1); 
     } else if (ph[pi] == ph[pi + 1]) { 
      return (cSort(pi)); 
     } 

     return -1; 
    } 

    int mSort(int mi) { 

     if (ma[mi] > ma[mi + 1]) { 
      return mi; 
     } else if (ma[mi] < ma[mi + 1]) { 
      return (mi + 1); 
     } 

     // checks higher phy marks 
     else if (ma[mi] == ma[mi + 1]) { 
      return (pSort(mi)); 
     } 

     return -1; 
    } 
} 

class Mlist { 

    public static void main(String args[]) { 

     // initializes the names and marks. 
     String[] name = {"foo", "bar", "baz"}; 
     int[] phy = {100, 112, 100}; 
     int[] chem = {100, 120, 80}; 
     int[] maths = {40, 68, 60}; 

     Student stud = new Student(name.length, phy, chem, maths); 

     stud.Sort(); 

     System.out.println("Name\tPhysics\tChemi\tMaths\tTotal"); 

     // prints the merit list 
     for (int i = 0; i < name.length; i++) { 

      // index of name is received by a function call 

      /* STUD.TOTAL_SORT[i] IS STUDENTS' MARKS IN SORTED MANNER. 
      * THIS IS PASSED AS AN ARGUMENT TO GETLIST METHOD WHICH USES LINEAR SEARCH 
      * TO FIND THE INDEX IN THE ARRAY OF NAMES FOR THE STUDENT WHOSE MARKS IS PASSES 
      * THE FUNCTION RETURNS INT WHICH IS THE INDEX FOR NAME[] ARRAY. and others all well. 
      * HERE STUD.TOTAL_SORT[I] IS AN ARGUMENT TO THE FUNCTION STUD.GETLIST() WHICH RETURNS AN INT    
      */ 

      System.out.println(name[stud.GetList(stud.total_sort[i])] + "\t" + phy[stud.GetList(stud.total_sort[i])] + "\t" + chem[stud.GetList(stud.total_sort[i])] + "\t" + maths[stud.GetList(stud.total_sort[i])] + "\t" + stud.total_sort[i]); 
     } 
    } 
} 
+1

例外スタックトレースを貼り付けることもできますか? – Bajji

+0

デバッガを使用してコードにステップインして何が起きているのかを確認します – user7

+0

私はこれを再び開くつもりです。これは、これに微妙に微妙なNPE質問です。 – Makoto

答えて

0

あなたがStud_newの新しいインスタンスを作成するときには、Studentの新しいインスタンスを扱っています。 1つのインスタンスで初期化する変数の状態を保持する必要はありません。

これを解決するには、superをミラーリングする単純なコンストラクタで十分です。

public Stud_new(final int x, final int[] p, final int[] c, final int[] m) { 
    super(x, p, c, m); 
} 

あなたは、親Studentクラスの同様のパラメータでそれを呼び出します。

Stud_new t = new Stud_new(total.length, ph, ch, ma); 

覚えておいてください:継承は、データではなく構造の完全性を保持します。親が子に持つデータが必要な場合は、それを提供する必要があります。

これは、並べ替えの正確さに関するコメントではありません。これは、NPEに対処するためのものです。

+0

ありがとうございました! – shiwchiz

+0

今私は 'Student'クラスだけを使用していますが、同じエラーが表示されていますが、あなたによればこの場合エラーは発生しません。 – shiwchiz

+0

私は、私が記述した内容以外の動作を保証しませんでした。私はそれをローカルでテストし、うまく動作します。あなたは私がここに示したもの以外の何かをしているに違いない。 – Makoto

関連する問題