读取文件时出现NullPointerException

时间:2014-10-21 01:14:38

标签: java nullpointerexception polynomials

我正在编写一个程序,要求我读取预定的文件并将给定的数字添加到多项式格式中。当代码到达文件末尾时,我不断收到NullPointerException。以下是给我错误的代码片段:

错误发生在第11行和/或第12行。

public IList<Integer, Term> add(IList<Integer, Term> p1, IList<Integer, Term> p2)
{
    IList<Integer, Term> addList = new LList<Integer, Term>();

    //If the keys of both list equal each other, add them to the term 
    //being placed inside of addList

    //
    if(p1.getSize() >= p2.getSize() || p2.getSize() <= p1.getSize()){
     //Here is where the error is happening --v
        for(int i = 0; i < 10; i++){
            if(p1.find(i).equals(p2.find(i))){
                Term t1 = p1.find(i);
                Term t2 = p2.find(i);

                int c1 = t1.getCoef();
                int c2 = t2.getCoef();
                int c3 = c1 + c2;

                Term t3 = new Term(c3, i);

                //Add the added term, at the location of the previously added terms location
                addList.add(i, t3);
            }
        }
    }
    return addList;
}

对此计划的任何帮助都将非常有用。感谢

1 个答案:

答案 0 :(得分:0)

public IList<Integer, Term> add(IList<Integer, Term> p1, IList<Integer, Term> p2)
{
    IList<Integer, Term> addList = new LList<Integer, Term>();

    if (p1 == null || p2 == null) {
        return addList;
    }

    if(p1.getSize() >= p2.getSize() || p2.getSize() <= p1.getSize()){
        ...
    }

    ...
}