2013-12-17 37 views
0

我正在嘗試使用其[0]元素和[1]元素創建一個字符串,其他元素加倍。我想在其他類中使用這些數組作爲我的座標。將字符串更改爲數組中的兩倍

我得到一個NullPointerException,我做錯了什麼?

進入這裏

public class ExampleTextInArrayReader 
{ 
    //naam document van ECG-patiént 1 
    public static final String FILE_NAME = "ecg1.txt"; 
    //array waarin de String van ECG-waarden is omgezet in een ArrayMetDoubles 
    private Double[] arrayMetDoubles = new Double[722-2]; 
    private String[] lines, gegevensPatiënt; 
    //velden voor patientnummer en de bijbehorende afdeling 
    public void main() 
    { 
     // Open the file 
     TextInArrayReader in = new TextInArrayReader(FILE_NAME); 

     // Check for errors during opening file 
     if (in.getError() != null) 
     { 
      System.out.println("Error opening file: " + in.getError()); 
      System.exit(1); 
     } 

     // Read the file 
     String[] lines = in.getLines(); 
     if (in.getError() != null) // check for errors during reading file 
     { 
      System.out.println("Error reading file: " + in.getError()); 
      System.exit(1); 
     } 

     // Print file contents 
     for (int i = 0; i < lines.length; i++) System.out.println(lines[i]); 
    } 

    public String[] drukGegevensPatiëntAf() 
    { 
     gegevensPatiënt = new String[2]; 
     for(int i = 0; i < 2; i++) 
     { 
      gegevensPatiënt[i] = lines[i]; 
     } 
     return gegevensPatiënt; 
    } 

    public void changeArray() 
    { 
     // Get the ECG-values from the lines array and change them in doubles and put them in an arrayMetDoubles array 
     arrayMetDoubles = new Double[lines.length - 2]; 
     for(int i = 2; i < arrayMetDoubles.length; i++) 
     {   
      arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]); 
     } 
    } 

    public Double [] getLijnen() 
{ 
    changeArray(); 
    return arrayMetDoubles; 
} 

}

+0

WHere你是否得到例外? –

+0

ExampleTextInArrayReader.changeArray(ExampleTextInArrayReader.java:48) – user3111770

+0

這是哪一行? –

回答

0

我喜歡你的代碼怎麼用英語混合荷蘭整個代碼! :)

不管怎樣,在for循環,從0環路長度爲2 ...不然代碼將填補指數2 ArraymetDoubles兩個地方的陣列

public void changeArray() 
{ 

    arrayMetDoubles = new Double[lines.length - 2]; 
    for(int i = 0; i < arrayMetDoubles.length; i++) 
    {   
     arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]); 
    } 
} 
+0

我的問題仍然沒有解決。我得到另一個NullPointerException異常:java.lang.NullPointerException \t at ExampleTextInArrayReader.changeArray(ExampleTextInArrayReader.java:48) 現在我的數組(arrayMetDoubles)甚至沒有收到行[i] – user3111770

+0

Verdorie中的雙打!也許你可以仔細檢查arrayMetDoubles變量和lines-variable是否被初始化並設置正確?如果行== NULL,Lines.length會拋出拋出錯誤,我認爲... –

+0

問題已解決。非常感謝!我現在給你的問題是,我如何將那些分配給我的ExampleTextInArrayReader中的數組的數組值分配到另一個可以計算不同的x,y座標並將它們放入另一個Double [] xcoordinate,ycoordinate的類中? – user3111770

1

監守類的成員外varaible線尚未assigned.In Main方法已assingned局部變量

String[] lines = in.getLines(); 

更改上面的行至

lines = in.getLines(); 
0 Main()方法中的

並確保在changeArray方法之前調用Main()方法。

相關問題