为什么TreeMap放置方法不适用于Java?

时间:2014-06-14 21:13:18

标签: java treemap

我正在尝试在TreeMap中添加2个列表的元素。我调试了项目,看起来TreeMap的put方法不起作用,因为它为Key和Value设置了null。

以下是代码:

package SubTest;
import java.io.*;
import java.util.*;

public class Matrice implements Cloneable,Comparable<Object> {

    private int nrCol;
    private int nrLin;
    private Object [][] matrix;

    public Matrice(){
        this.nrCol=0; this.nrLin=0;
        this.matrix=null;
    }
    public Matrice(int nrColoane, int nrLinii, Object[][]m){
        this.nrCol=nrColoane;
        this.nrLin=nrLinii;
        this.matrix=new Object[this.nrLin][this.nrCol];
        for(int i=0;i<this.nrLin;i++)
            for(int j=0;j<this.nrCol;j++)
                this.matrix[i][j]=m[i][j];
    }

    public int getnrCol(){
        return this.nrCol;
    }
    public void setnrCol(int c){
        this.nrCol=c;
    }
    public int getnrLin(){
        return this.nrLin;
    }
    public void setnrLin(int l){
        this.nrLin=l;
    }
    public Object[][] getMatrix(){
        return this.matrix;
    }

    public void citirefis(String fisier) throws FileNotFoundException, negativeNrLoc
    {
        BufferedReader bf=new BufferedReader(new FileReader(fisier));
        List<PasagerVIP> vip=new ArrayList<PasagerVIP>();
        List<PasagerEc> ec=new ArrayList<PasagerEc>();
        String line=null; int nr=0;
        try{
            while((line = bf.readLine()) !=null){
                nr++;
                StringTokenizer token=new StringTokenizer(line," ");
                int n=0; String nume=null; String mycard=null; float nrloc=0; float varsta=0; int id=0;
                while(token.hasMoreElements()){
                    switch(n){
                    case 0: nume=token.nextToken(); break;
                    case 1: nrloc=Float.parseFloat(token.nextToken()); break;
                    case 2: varsta=Float.parseFloat(token.nextToken()); break;
                    case 3: id=Integer.parseInt(token.nextToken()); break;
                    case 4: mycard=token.nextToken(); break;
                    }
                    n++;
                }
                if(mycard.contains("vip")) {
                    PasagerVIP v=new PasagerVIP(nume,nrloc,varsta,id,mycard);
                    vip.add(v);

                }
                else if(mycard.contains("ec")){
                    PasagerEc e=new PasagerEc(nume,nrloc,varsta,id,mycard);
                    ec.add(e);
                }
            }
            bf.close();
        }
        catch(IOException eoef){
            eoef.printStackTrace();}    

        Map<Float,Object> harta=new TreeMap<Float,Object>();
        int s1=vip.size(); 
        for(int i=0;i<s1;i++){

            harta.put(new Float(vip.get(i).getnrLoc()), vip.get(i));}
        int s2=ec.size();
        for(int i=0;i<s2;i++)
            harta.put(new Float(ec.get(i).getnrLoc()), ec.get(i));
        this.nrLin=(int)Math.sqrt(nr);
        this.nrCol=(int)Math.sqrt(nr);

        Set<Float> st=harta.keySet();
        Iterator<Float> it=st.iterator();
        for(int i=0;i<this.nrLin && it.hasNext();i++)
            for(int j=0;j<this.nrCol && it.hasNext();i++){
            Float loc=it.next();
            Object o=harta.get(loc);
            this.matrix[i][j]=o;}

        }

}

StackTrace:

Exception in thread "main" java.lang.NullPointerException
    at SubTest.Matrice.citirefis(Matrice.java:128)
    at SubTest.Main.main(Main.java:21)

1 个答案:

答案 0 :(得分:0)

构造函数应该正确地创建数组,或者应该有一个方法来在所需的维度中创建它,例如。

public void createMatrix(){
    this.matrix = new Object[nrLin][nrCol];
}

这应该在第86行调用:

this.createMatrix();

NPE的结果是this.matrix为空。