2013-04-22 79 views
-3
public class Author { 
private int id; 
private String name; 
private String university; 
private String department; 
private String email; 
private int article1; 
private int article2; 
private int article3; 
private int article4; 
private int article5; 
//constructors and getter/setters are generated automatically, not adding to have space 
} 

這是我的作者類。這個類只有這些屬性。另外,我有一個readDaFile類,它被創建爲讀取author.txt並創建作者對象。使用構造函數時獲得nullpointerexception

import java.util.Scanner; 
import java.io.*; 


public class readAuthor { 

private Scanner reader; 
private String temp; 
private String[] split; 
public Author[] authorList; 
private int dummyInt,dummyArticle1=0 ,dummyArticle2=0 ,dummyArticle3=0,dummyArticle4,dummyArticle5; 
private int i=0; 
private String name , university , department , email ; 
public void tryOpeningOrDieTrying(){ 
try{ 
    reader = new Scanner(new File("Author.txt")); 
} 
catch(Exception exo){ 
System.out.println("Can not find file."); 
} 
} 
public void readDaFile(){ 

    while(reader.hasNext()){ 
     temp = reader.nextLine(); 
     split = temp.split(" "); 

     name = "NOTGIVEN"; 
     university = "NOTGIVEN"; 
     department = "NOTGIVEN"; 
     email = "NOTGIVEN"; 
     dummyInt = 0; 
     dummyArticle1 = 0; 
     dummyArticle2 = 0; 
     dummyArticle3 = 0; 
     dummyArticle4 = 0; 
     dummyArticle5 = 0; 

     dummyInt = Integer.parseInt(split[1]); 
     if(split.length>2){ name = split[2]; } 
     if(split.length>3){ university = split[3]; } 
     if(split.length>4){ department = split[4]; } 
     if(split.length>5){ email = split[5]; } 
     if(split.length>6){ dummyArticle1 = Integer.parseInt(split[6]); } 
     if(split.length>7){ dummyArticle2 = Integer.parseInt(split[7]); } 
     if(split.length>8){ dummyArticle3 = Integer.parseInt(split[8]); } 
     if(split.length>9){ dummyArticle4 = Integer.parseInt(split[9]); } 
     if(split.length>10){ dummyArticle5 = Integer.parseInt(split[10]); } 

     System.out.println(dummyInt+name+university+department+email+dummyArticle1+dummyArticle2+dummyArticle3+dummyArticle4+dummyArticle5); 
     //authorList[i] = new Author(dummyInt,name,university,department,email,dummyArticle1,dummyArticle2,dummyArticle3,dummyArticle4,dummyArticle5); 
i++; 
     //System.out.println(split[1]); 
    //System.out.println(split.length); 
    } 
} 
public void sealDaGates(){ 
reader.close(); 
} 
} 

只要我先閱讀線條然後將它們拆分成子元素來創建作者對象。但是Author.txt可能不會給出所有的作者屬性。
例如:

AUTHOR 100 
AUTHOR 101 Ruonan_Li MIT Computer_Science [email protected] 1002001 1002009 1002004 

爲了防止發送空參數構造函數的作者,我每次初始化變量屬性爲每個循環。我還檢查了初始化的屬性變量printf-他們。他們似乎按預期工作。如果我無法成功從txt讀取屬性,程序會將NOTGIVEN0發送給構造函數。不過還是我有nullpointerexception在行:

authorList[i] = new Author(dummyInt,name,university,department,email,dummyArticle1,dummyArticle2,dummyArticle3,dummyArticle4,dummyArticle5); 

在此先感謝

+0

帖子堆棧跟蹤 – PSR 2013-04-22 12:47:34

+3

將來,請花費更多的精力來設置您問題中的代碼格式 - 並且只發布*相關*代碼。在10行短代碼中顯示相同的問題是完全可能的。 – 2013-04-22 12:49:01

+1

你知道什麼是NullPointerException嗎? – djechlin 2013-04-22 12:49:01

回答

5

你永遠初始化authorList,所以這是空。它不是失敗的構造函數調用 - 它是數組中的賦值。您需要:

authorList = new Author[...]; 

某處。或者 - 並且幾乎可以肯定地優選 - 使用例如List<Author>,例如,

private final List<Author> authorList = new ArrayList<Author>(); 
+0

謝謝,現在它按預期工作。我現在不使用ArrayList,但會盡快更改它。 – 2013-04-22 13:25:15

0

看起來你忘了初始化authorList數組。在構造函數中,添加這一行authorList = new Author[100];並且應該修復它。將100更改爲您希望的任意數量的元素。

相關問題