![](/img/trans.png)
[英]NullPointerException when trying to add a TableRow to a TableLayout programmatically
[英]Encountering NullPointerException when trying to add polynoms
我需要添加兩個多項式,它由兩個整數組成。 例如,系數和指數3x ^ 2將使用3和2作為參數構造。 我得到一個NullPointerException但我無法弄清楚為什么。 任何幫助,將不勝感激!
public class Polynomial {
private Node poly;
public Polynomial() {
}
private Polynomial(Node p) {
poly = p;
}
private class Term {
int coefficient;
int exponent;
private Term(int coefficient, int exponent) {
this.coefficient = coefficient;
this.exponent = exponent;
}
}
private class Node {
private Term data;
private Node next;
private Node(Term data, Node next) {
this.data = data;
this.next = next;
}
}
public void addTerm(int coeff, int exp) {
Node pointer = poly;
if (pointer.next == null) {
poly.next = new Node(new Term(coeff, exp), null);
} else {
while (pointer.next != null) {
if (pointer.next.data.exponent < exp) {
Node temp = new Node(new Term(coeff, exp), pointer.next.next);
pointer.next = temp;
return;
}
pointer = pointer.next;
}
pointer.next = new Node(new Term(coeff, exp), null);
}
}
public Polynomial polyAdd(Polynomial p) {
return new Polynomial(polyAdd(this.poly, p.poly));
}
private Node polyAdd(Node p1, Node p2) {
if (p1 == p2) {
Term adding = new Term(p1.data.coefficient + p2.data.coefficient,
p1.data.exponent);
p1 = p1.next;
p2 = p2.next;
return new Node(adding, null);
}
if (p1.data.exponent > p2.data.exponent) {
p2 = p2.next;
}
if (p1.data.exponent < p2.data.exponent) {
p1 = p1.next;
}
if (p1.next != null && p2.next != null) {
return polyAdd(p1, p2);
}
return new Node(null, null);
}
}
import javax.swing.JOptionPane;
/ ** * * @author Bill Kraynek * / public class Polynomials {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Polynomial p1 = new Polynomial();
Polynomial p2 = new Polynomial();
Polynomial p0 = new Polynomial();
p1.addTerm(5, 2);
p1.addTerm(4, 5);
p1.addTerm(3, 3);
p1.addTerm(1, 2);
p1.addTerm(5, 6);
p2.addTerm(3, 8);
p2.addTerm(2, 5);
p2.addTerm(1, 3);
Polynomial p3 = p1.polyAdd(p2);
JOptionPane.showMessageDialog(null, "p1 is " + p1 + "\np2 is " + p2 + "\np1+p2 is " + p3);
// Polynomial p4 = p1.polyMultiply(p2);
// JOptionPane.showMessageDialog(null, "p1 is " + p1 + "\np2 is " + p2 + "\np1*p2 is " + p4);
// Polynomial p5 = p2.polyMultiply(p2);
//JOptionPane.showMessageDialog(null, "p2 is " + p2 + "\np2*p2 is " + p5);
// Polynomial p6 = p0.polyMultiply(p2);
//JOptionPane.showMessageDialog(null, "p0 is " + p0 + "\n" + "p2 is " + p2 + "\np0*p2 is " + p6);
Polynomial p7 = p0.polyAdd(p2);
JOptionPane.showMessageDialog(null, "p0 is " + p0 + "\n" + "p2 is " + p2 + "\np0+p2 is " + p7);
p1 = p1.polyAdd(p2);
JOptionPane.showMessageDialog(null, "After p1 = p1+p2 p1 is " + p1);
// p2 = p2.polyMultiply(p2);
JOptionPane.showMessageDialog(null,"After p2 = p2*p2 p2 is " + p2);
}
有些行是//因為我還沒有第二種方法
節點指針= poly;
由於poly在調用默認的Polynomial構造函數時未實例化,因此無法訪問它。 但你的下一行是:
if(pointer.next == null){
哪個拋出空指針。 所以你應該檢查那里或禁用默認構造函數。
但這只是一個存在空指針異常的情況。 如果你能提供你的主要課程,那將會非常有幫助。
//編輯:如果你將非默認構造函數設為public並使用那個,你的代碼可以這樣使用:
Polynomial p = new Polynomial(new Node(new Term(3, 2), null)); Polynomial p2 = new Polynomial(new Node(new Term(3, 2), null)); p.polyAdd(p2);
沒有任何例外。 但我會考慮重新設計它,因為它是不必要的復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.