Could someone check my code for why I'm getting NullPointerExceptions in various methods? I've been racking my brain and can't figure out what is wrong.
I'm attempting to create a polynomial class that stores polynomials in a linked list using an inner class Terms. If you have any questions on the methods, please let me know!
public class Polynomial {
// Create the "Term" Inner Class for Handling the Respective Nodes for this Class
protected class Term {
int coefficient; // Coefficient of the Current Term
int exponent; // Exponent of the Current Term
Term next; // Reference to the Next Term in the Linked List (if Possible)
public Term (int coefficient, int exponent, Term next) { // General Constructor Method
this.coefficient = coefficient;
this.exponent = exponent;
this.next = next;
}
public Term (int coefficient, int exponent) { // No "Term next" (The Next Term Needs to be Null!)
this (coefficient, exponent, null);
}
public Term () { // Basic Constructor Method (No Input Necessary)
this (0, 0, null);
}
}
protected Term poly; // Starting Point (Dummy Header Node)
public Polynomial() { // General Constructor Method
poly = new Term();
clear();
}
public Polynomial(int [] terms) { // Constructor Method (Using an Integer Array of Coefficients and Exponents)
if (!(terms.length % 2 == 0)) { // This Array MUST Possess an Even, Positive Number of Elements (Alternating Coefficients and Corresponding Exponents)
throw new IllegalArgumentException("Your array must have an even, positive number of elements for use in our program. Thanks!");
}
else { // Proceed Normally...
poly = new Term(); // Dummy Header Node
clear();
setTerms(terms);
}
}
// Create the clear() Method for Promptly Resetting the Polynomial Back to 0
public void clear() {
poly.next = null;
}
// Create the setTerms(int [] terms) Method for Setting the Polynomial to the Appropriate Set of Terms
public void setTerms(int [] terms) {
Term temp = poly;
for (int i = 0; i < terms.length; i = i + 2) { // Incrementing By 2 (Each Polynomial Necessitates a Coefficient & Exponent, or Lack Thereof)
if (terms[i + 1] < 0) { // Polynomials CANNOT Have Coefficients with Negative Exponents
throw new IllegalArgumentException("Coefficients in polynomials must have positive exponents for use in our program. Thanks!");
}
temp.next = new Term(terms[i], terms[i + 1]);
temp = temp.next;
}
}
// Create the toString() for Outputting the Polynomial to the User's Screen
public String toString() {
String polynomialString = "";
boolean firstTerm = true;
if (poly.next == null) {
return "0"; // The ONLY Time 0 is Outputted as a Coefficient is if the Polynomial = 0
}
while (poly.next != null) { // Reached the Conclusion of the Linked List?
if (poly.next.coefficient > 0) { // Checking for Positivity
if (!firstTerm) { // First Term DOESN'T Need a "+" Sign
polynomialString = polynomialString + "+";
}
}
if (poly.next.coefficient == 1) {
if (poly.next.exponent == 1) {
polynomialString = polynomialString + "x"; // Print ONLY the Variable (x)
}
else {
polynomialString = polynomialString + "x^" + poly.next.exponent; // Otherwise, Print "x^exponent"
}
}
else if (poly.next.coefficient == -1) {
if (poly.next.exponent == 1) {
polynomialString = polynomialString + "-x"; // Print ONLY the Variable (x)
}
else {
polynomialString = polynomialString + "-x^" + poly.next.exponent; // Otherwise, Print "x^exponent"
}
}
else if (poly.next.exponent == 1) {
polynomialString = polynomialString + poly.next.coefficient + "x"; // Print "coefficientx"
}
else if (poly.next.exponent == 0) { // Print the Coefficient Alone...
polynomialString = polynomialString + poly.next.coefficient;
}
else { // Proceed Normally (WITH Coefficient & Exponent)
polynomialString = polynomialString + poly.next.coefficient + "x^" + poly.next.exponent;
}
poly = poly.next;
firstTerm = false;
}
return polynomialString;
}
// Create the addTerm() Method that Incorporates the Term "Coefficient * x^Exponent" to the Respective Polynomial (Needs Help)
public void addTerm(int coefficient, int exponent) {
if (exponent < 0) { // Polynomials CANNOT Have Coefficients with Negative Exponents
throw new IllegalArgumentException("Coefficients in polynomials must have positive exponents for use in our program. Thanks!");
}
Term newTerm = new Term(coefficient, exponent);
Term p = poly;
while (p.next != null && p.next.exponent > exponent) {
p = p.next;
}
newTerm.next = p.next;
p.next = newTerm;
}
// Create the eval() Method that Returns the Result of Evaluating the Polynomial at x = val (USE Horner's Method for this Method)
public double eval(double val) {
double result = 0;
Term p = poly;
p = p.next;
result += p.coefficient * val + p.next.coefficient;
p = p.next;
while (p.next != null) {
result = result * val + p.next.coefficient;
p = p.next;
}
return result;
}
// Create the multiply() Method that Returns a NEW Polynomial After Multiplying Each Term by the scalar Variable
public Polynomial multiply(int scalar) {
Polynomial newPoly = new Polynomial();
Term p = poly.next;
while (p.next != null) {
newPoly.addTerm(scalar * p.coefficient, p.exponent); // Multiplying the Coefficients and Scalar Together
p = p.next;
}
return newPoly;
}
// Create the add(Polynomial rhs) Method that Returns the Result of ADDING the "rhs" polynomial to "this" polynomial
public Polynomial add(Polynomial rhs) {
Polynomial newPoly = new Polynomial();
Term p1 = poly.next; // this polynomial
Term p2 = rhs.poly.next; // rhs polynomial
while (p1.next != null && p2.next != null) {
if (p1.exponent == p2.exponent) { // Same Exponents?
newPoly.addTerm(p1.coefficient + p2.coefficient, p1.exponent);
p1 = p1.next;
p2 = p2.next;
}
else if (p1.exponent > p2.exponent) { // Greater than rhs Polynomial?
newPoly.addTerm(p1.coefficient, p1.exponent);
p1 = p1.next;
}
else { // Less than rhs Polynomial?
newPoly.addTerm(p2.coefficient, p2.exponent);
p2 = p2.next;
}
}
if (p1.next == null) {
while (p2.next != null) { // Does the rhs Polynomial have Extra Terms?
newPoly.addTerm(p2.coefficient, p2.exponent);
p2 = p2.next;
}
}
if (p2.next == null) {
while (p1.next != null) { // Does the this Polynomial have Extra Terms?
newPoly.addTerm(p1.coefficient, p1.exponent);
p1 = p1.next;
}
}
return newPoly;
}
// Create the multiply(Polynomial rhs) Method that Returns the Result of MULTIPLYING the "rhs" polynomial to "this" polynomial
public Polynomial multiply(Polynomial rhs) {
Polynomial newPoly = new Polynomial();
Term p1 = poly.next; // this polynomial
Term p2 = rhs.poly.next; // rhs polynomial
while (p1.next != null) {
while (p2.next != null) {
newPoly.addTerm(p1.coefficient * p2.coefficient, p1.exponent + p2.exponent);
p2 = p2.next;
}
p1 = p1.next;
}
return newPoly;
}