2015-10-05 35 views
0

Hello StackOverflow社區!我是僅有1年經驗的最近Java學習者。我被要求設計一個模擬學校數據庫的軟件,存儲所有名稱,類,卷和其他細節。當被問到時,它會顯示適當的消息,比如計算性能,刪除記錄等。它還不完整,但第一部分(接受和存儲細節)已完成。我花了很多時間在這裏,我唯一得到的是一個nullPointerError。對不起,但我被要求堅持基礎,所以沒有閃光的代碼。我用過繼承。超類是「學生」。用Java進行臨時管理的應用程序

public class Student { 
int roll,age = 0; // Roll to be auto-updated 


String cl,name; 
// Marks variables now 
int m_eng, m_math, m_physics, m_chem, m_bio = 0; 
public Student(){ 
} 
public Student(int a, String cla){ 
    age = a; 
    cl = cla; // Assign values 

} 
void setMarks(int eng, int math, int phy, int chem, int bio){ 
    m_eng = eng; m_math = math; m_physics = phy; m_chem = chem; m_bio = bio; 
} 
} 

這裏的錯誤:

java.lang.NullPointerException 
at Application.accept_data(Application.java:35) 
at Application.execute(Application.java:23) 
at Application.input(Application.java:16) 
at Application.main(Application.java:101) 

下面是代碼,但:

import java.util.Scanner; 
public class Application extends Student { 
static int n; static Scanner sc = new Scanner(System.in); 
static Student s[]; 
void input(){ 
    System.out.println("Enter the number of students: "); 
    n = sc.nextInt(); 
    s = new Student[n]; // Create array for n students 
    System.out.println("Enter your choice: "); 
    System.out.println("1. Accept student's details "); 
    System.out.println("2. Display all records "); 
    System.out.println("3. Display data student-wise "); 
    System.out.println("4. Delete record"); 
    System.out.println("5. Display performance status"); 
    System.out.println("6. Exit"); 
    execute(); 
} 
static void execute(){ 
    boolean correct = false; 
    while (!correct){ 
     int op = sc.nextInt(); 
     switch(op){ 
     case 1: accept_data(); correct = true; 
     case 2: disp_data();correct = true; 
     case 3: disp_studentwise();correct = true; 
     case 4: del_record();correct = true; 
     case 5: performance();correct = true; 
     case 6: System.exit(0); correct = true;//Terminate 
     default: System.out.println("You must enter a choice. Kindly re-enter: ");correct = false; 
     } 
    } 
} 
static void accept_data(){ 
    for (int i = 0; i<s.length; i++){ 
     s[i].roll = i+1; //Autoupdate roll 
     System.out.println("Enter name: "); s[i].name = sc.nextLine(); 
     System.out.println("Enter age: "); s[i].age = sc.nextInt(); // Refer to object prope. 
     System.out.println("Enter class: "); s[i].cl = sc.nextLine(); 
     System.out.println("We're heading for marks entry!"); 
     System.out.println("Enter marks in the following order: ENGLISH, MATH, PHYSICS, CHEMISTRY, BIOLOGY"); 
     s[i].setMarks(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt()); 
    } 
    System.out.println("Thanks. Main menu, please enter your choice now: "); 
    execute(); 
} 
static void disp_data(){ 
    System.out.println("The system will display all stored information of students available."); 
    for (int i = 0; i<s.length; i++){ 
     if (s[i].roll != -1){ 
      continue; // In case record is deleted, it won't display 
     } 
     else { 
      printrec(i); 
     } 
    } 
    System.out.println("Main menu, please enter your choice: "); 
    execute(); 
} 
static void disp_studentwise(){ 
    System.out.println("Enter the roll number"); 
    int r = sc.nextInt(); 
    boolean ok = (r>s.length||r<0)?false:true; 
    while (!ok){ 
     System.out.println("Incorrect roll. Please re-enter: "); 
     r = sc.nextInt(); 
     if (r>s.length) ok = false; 
     else ok = true; 
    } 
    printrec(r-1); 
    System.out.println("Main menu, please enter your choice: "); 
    execute(); 
} 
static void printrec(int n){ 
    int i = n; 
    System.out.println("For roll number " + s[i].roll + ", details: "); 
    System.out.println("Name: " + s[i].name); System.out.println("Age: " +  s[i].age); 
    System.out.println("Class: " + s[i].cl); 
    System.out.println("Subject \t Marks"); 
    System.out.println("English: \t " + s[i].m_eng); // Display record with marks 
    System.out.println("Maths: \t " + s[i].m_math); 
    System.out.println("Physics: \t " + s[i].m_physics); 
    System.out.println("Chemistry: \t " + s[i].m_chem); 
    System.out.println("Biology: \t " + s[i].m_bio); 
} 
static void del_record(){ 
    System.out.println("Enter the roll number you want to delete: "); 
    int rll = sc.nextInt(); 

    for (int i = 0; i<s.length; i++){ 
     if (rll == s[i].roll){ 
      s[i].roll = -1; // Assign non-positive value to refer deleted items 
     } 

    } 

} 
static void performance(){ 
} 
public static void main(String[] args){ 
    Application ob = new Application(); 
    ob.input(); // Start program 
} 
} 

任何人都可以指出什麼地方出了錯?爲什麼在按第一個選項後接受學生的詳細資料會出現問題?它在s [i] .roll上顯示nullPointer。請記住,卷是自動的,用戶不會介入。它作爲主鍵。解釋會是有益的,如果可能的話,我渴望學習。謝謝。

+0

'accept_data()'裏面的哪個行引發異常? – Rehman

+0

他說這是在'[i] .roll = i + 1' –

+2

與您的NPE無關,但默認情況下Java中的開關語句是落後的。你的'execute()'方法將在其switch語句中的每一個命令都連續運行,而不僅僅是'accept_data()'。 – azurefrog

回答

0

這樣的:

s = new Student[n]; // Create array for n students 

你只是創造「N」學生對象的數組這裏...這並不意味着你的「N」學生被初始化...您的陣列只包含「空的價值觀......

你可能想在你accept_data方法做:

for (int i = 0; i<s.length; i++){ 
    s[i] = new Student(); 
    s[i].roll = i+1; //Autoupdate roll 

....

+0

但是如果用戶選擇不同的選項,如「顯示所有記錄」呢?他應該用'input'方法填充數組。 –

+0

我剛剛回答了這個問題;-) ...我希望,我相信@Mission編碼會做對了...... – Pras

0

由於您在input方法中創建了一個Student的數組,因此您將得到一個NPE,但您永遠不會使用Student對象填充它,因此在accept_data中,您試圖訪問不存在的對象上的roll字段。

在撥打accept_data之前,您需要在input方法中用新的Student對象填入陣列。

相關問題