2014-10-21 7 views
-2

Я пишу программу, которая требует, чтобы я читал в предопределенном файле и добавлял числа, заданные в полиномиальный формат. Я продолжаю получать исключение NullPointerException, когда код достигает конца файла. Вот Snippit кода, который дает мне ошибку: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; 
} 

Любая помощь с этой программой будет более полезным. Спасибо

+2

Укажите конкретную линию, в которой находится NPE. Номер строки будет в трассировке стека, который Java barfs будет консоль при возникновении проблемы. – MarsAtomic

+0

@MarsAtomic updated – intrigatory57

+0

Чтобы повторить повтор, укажите конкретную строку, в которой находится NPE. –

ответ

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()){ 
     ... 
    } 

    ... 
} 
Смежные вопросы