2014-10-21 12 views
-2

미리 결정된 파일을 읽고 다항식 형식으로 주어진 숫자를 추가해야하는 프로그램을 작성하고 있습니다. 코드가 파일의 끝에 도달하면 NullPointerException이 계속 발생합니다. 오류가 라인 (11)에서 발생 및/또는 도움이되는 것보다 12파일을 읽을 때 NullPointerException이 발생했습니다.

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; 
} 

이 프로그램에 어떤 도움이 될 것입니다 더

: 다음은 나에게 오류를 제공 코드의 SNIPPIT입니다. 감사합니다

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

    ... 
} 
관련 문제