簡體   English   中英

嘗試解析數組時出現空指針異常(java)

[英]Null pointer exception when trying to parse array (java)

嘗試解析字符串數組索引時,出現了空指針異常。 我該如何解決? 謝謝

public class studentclass {
public static void main(String[] args){

    Scanner scan = new Scanner(System.in);
    String[][] student = new String[10][];
    Double[][] results = new Double[10][];
    Double[] total = new Double[10];
    String tableln = "";

   for(int index = 0; index < student.length; index++){
    System.out.println("\nPlease enter student " + (index+1) + "'s details");
    String userinput = scan.nextLine();
    student[index] = userinput.split(",");

    //converts string array in to double array but receiving null pointer exception
    results[index][0] = Double.parseDouble(student[index][2]); 
    results[index][1] = Double.parseDouble(student[index][3]);
    results[index][2] = Double.parseDouble(student[index][4]);
    results[index][3] = Double.parseDouble(student[index][5]);
    total[index] = (results[index][0]*0.1+results[index][1]*0.4+
                    results[index][2]*0.3+results[index][3]*0.2);

看起來results[index]需要初始化。 您為帶有results[0..n] new的results[0..n]分配了內存,但沒有為results[index][0...n]分配內存。

如果您始終存儲四個值,請添加以下內容:

results[index] = new Double[4];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM