2012-05-13 36 views
1

我有四个班。项目类,学生类,ProjectFile类和ProjectFrame JFrame。值没有被存储在数组中?

ProjectFrame仅用于GUI,我还没有触及它。

学生和项目类是构造函数,我编写了这些。

现在我试图通过从文本文件中读取然后存储要读取的数据来实现ProjectFile类。我有麻烦,因为我不确定为什么项目类的实例不存储数据。我看过我的循环,并且确实打印了变量以确保循环实际发生。它第一次工作,但当我尝试调用第二个数组时,它给了我一个NullPointerException。所以我假设它存储的值为空,但不应该如此。

这是我的project文件类

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package DecelAssignment; 

import java.io.*; 

/** 
* 
* @author Zane 
*/ 
public class ProjectFile { 

    private static Project[] pJect; 
    private static Student[] sDent; 
    private static Project ja; 
    private static BufferedReader br; 

    public static void readData() { 
     File inputFile = new File("projects.txt"); 

     try { 
      br = new BufferedReader(new FileReader(inputFile)); 
      String s = br.readLine(); 
      pJect = null; 
      pJect = new Project[Integer.parseInt(s)]; 
      //System.out.println(s); 
      for (int i = 0; i < pJect.length; i++) { 
       s = br.readLine(); 
       if (s == null) { 
        break; 
       } else { 
        String sLine[] = s.split(","); 
        int count = 3; 
//     for (int i2 = 0; i2 < Integer.parseInt(sLine[3]); i2++) { 
//      sDent[i2] = new Student(sLine[count+1], sLine[count+2], sLine[count+3], sLine[count+4]); 
//      count += 4; 
//     } 
        pJect[i] = new Project(sLine[0], sLine[1], sLine[2], sDent); 
        System.out.println(pJect[1].getTitle()); 
        System.out.println(sLine[0]); 
        System.out.println(i); 
       } 
      } 

     } catch (IOException e) { 

      System.out.println("I caught an IO Exception1"); 
     } 
//  } catch (NullPointerException e) { 
//   e.printStackTrace(); 
//   System.out.println("I caught a Null Pointer Exception!"); 
// 
//  } 
    } 

// public Project[] getProjectInfo() { 
//   
//   
//  return; 
// } 
    public static void main(String[] args) {  
     readData(); 
    } 
} 

这是文本文件,我是来自

3 
iPhone App,EEE,John Tan,1,P109520,Kelvin Tay,DBIT,M 
iPad App,DMIT,Mark Goh,3,P106286,Felicia Wong,DIT,F,P101803,Rachel Chang,DIT,F,P100036,Lewis Poh,DBIT,M 
Green Living,DMIT,Audrey Lim,2,P1,Peter Chua,DIT,M,P103287,Ng Ming Shu,DISM,F 

阅读某人能请解释一下其中我编码此错误?我无法弄清楚。

编辑:

这是工程类

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package DecelAssignment; 

/** 
* 
* @author Zane 
*/ 
public class Project { 
    private String title, school, supervisor; 
    private Student[] stDent; 

    public Project() { 
     title = ""; 
     school = ""; 
     supervisor = ""; 
     stDent = new Student[0]; 
    } 

    public Student[] getStDent() { 
     return stDent; 
    } 

    public Project(String title, String school, String supervisor, Student[] stDent) { 
     this.title = title; 
     this.school = school; 
     this.supervisor = supervisor; 
     this.stDent = stDent; 
    } 

    public String getSchool() { 
     return school; 
    } 

    public void setSchool(String school) { 
     this.school = school; 
    } 

    public String getSupervisor() { 
     return supervisor; 
    } 

    public void setSupervisor(String supervisor) { 
     this.supervisor = supervisor; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

} 
+3

不要捕获空白点。这样做没有意义 – keyser

+0

Project类的构造函数是什么?你真的把值赋给成员变量吗? –

+0

我知道。但我必须这样做,因为这是这项任务的要求。 – Zane

回答

4

我猜你的代码崩溃这里

 System.out.println(pJect[1].getTitle()); 

在第一循环pJect [1]将包含null导致碰撞

你可能打算

 System.out.println(pJect[i].getTitle()); 
+0

是的。但我想知道为什么? 编辑:哦。我知道了。哎呀。愚蠢的我 – Zane

+0

你试图为'null'做一个getTitle –