Java编程疑问,关于excel导入eclipse

王田心 发布于 2015/05/24 11:00
阅读 775
收藏 0
POI

初学Java,以下代码运行时一直出现

Exception in thread "main" java.lang.NullPointerException

 at Department.<init>(Homework.java:111) at Homework.main(Homework.java:18)的错误,求解


import java.util.ArrayList;



import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;


import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Homework {

public static void main(String[] args) {
Department departement = new Department("e:\\13信管1班好朋友.xlsx"); //第18行
Student bestFriend = departement.getBestFriend("陈思玲");
System.out.println(bestFriend.getName() );
}
}
class Student{



private String name;
private String gender;
private HashMap<String, Double> interests;
private Department department;


public Student(String name, String gender, Department department) {
this.name = name;
this.gender = gender;
this.interests = new HashMap<String, Double>();
this.department = department;
}

public String getName() {
return name;
}


public String getGender() {
return gender;
}


public HashMap<String, Double> getInterests() {
return interests;
}








public void addInterest(String interest, double value){
this.interests.put(interest, value);
}


public double getSimilarity(Student other){
double totolDifference=0;
for(String intererst: this.department.getInterests()){
double thisValue= this.getInterests().get(intererst);  
double otherValue = other.getInterests().get(intererst);
totolDifference = totolDifference + Math.abs(thisValue-otherValue);
}
double base = this.department.getInterests().size() * 8;
return (base-totolDifference)/base;
}

public boolean isSameGender(Student other){
return this.gender.equals(other.getGender());
}


}


class Department{



private Sheet loadSheet(String filePath){
Sheet sheet=null;
try {
OPCPackage pkg = OPCPackage.open( filePath);
Workbook wb = new XSSFWorkbook( pkg );
sheet = wb.getSheetAt(0);

}catch (Exception e){
e.printStackTrace();
}
return sheet;
}
 
private ArrayList<String> interests;
private ArrayList<Student> students;



public ArrayList<String> getInterests() {
return interests;
}


public ArrayList<Student> getStudents() {
return students;
}


public Department(String filePath) {
interests = new ArrayList<String>();
students = new ArrayList<Student>();

Sheet sheet=loadSheet(filePath);
Row r = sheet.getRow(0);
  for (int cn = 2; cn <= r.getLastCellNum(); cn++) {
   Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
interests.add(c.toString());\\第111行
}

   for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
    r = sheet.getRow(rowNum);
    Cell c = r.getCell(1, Row.RETURN_BLANK_AS_NULL);
String name = c.toString();
c = r.getCell(2, Row.RETURN_BLANK_AS_NULL);
String gender= c.toString();
Student student = new Student(name, gender, this);
       for (int cn = 2; cn <= r.getLastCellNum(); cn++) {
      c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
      double value = Double.parseDouble( c.toString() );
      String interest = interests.get( cn-2);
      student.addInterest(interest, value);
    }
       students.add(student);
   }



}

public Student getBestFriend(Student other){
double maxSimilarity=0;
Student bestFriend =null;
for(Student student: this.students){
// System.out.println(student.getName() + ":" +student.getSimilarity(other));
if (maxSimilarity<= student.getSimilarity(other)  ){
maxSimilarity= student.getSimilarity(other);
bestFriend= student;
}
}
return bestFriend;
}

public Student getBestFriend(String name){
return  null;
}
public Student getStudentByName(String name){
return null;
}



public ArrayList<Student> getBestFriends(){
return null;
}




/**
* @param args
*/



}

加载中
0
Cobbage
Cobbage
自己debug呗,看看谁是空的 c可能就是有空的
0
巴拉迪维
巴拉迪维

请楼主:

  1. 学会把代码格式化
  2. 学会问问题

然后再来这里提问吧!

吐槽的达达仔
吐槽的达达仔
代码不清楚,日志也没有。。 这样怎么帮你嘛。。小伙子。。
0
淘气的小猴子
淘气的小猴子

我还在纳闷什么时候excel能导入eclipse了

返回顶部
顶部