Hi guys.

I'm having this weird problem with my linked list. basically i'm making a linked list implementation of a polynomial; my nodes contain 2 elements (coefficient,powers). It works somewhat fine, when i directly add in a coefficient and exponent pair (example poly.add(5,3)) however it doesnt work through the users input and gives me a null pointer exception.

the user needs to input their text like so: 12x^5+3x^2 and then the regular expression will break it apart in the coefficient/exponent pairs. when i use this method, the linked list gives me the null pointer exception.

I'm pretty sure its a trivial error, but i'm just so fried from going over this for hours that i cant figure it out. I will be eternally grateful if someone can just test this program and let me know what the issue is here.

I think the error originates somewhere in myprepmethod, because when i bypass that method and just useaddit works pretty alright

Code:

Code :

package project2; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; class Polynomial { Node head; Polynomial() { head = null; } public void add(int coef, int power) { Node temp = new Node(coef, power, null); if (head == null) head = temp; else { Node p = null; Node q = head; //insert in front if exponent is higher if (temp.getElement2() > q.getElement2()) { temp.setNext(head); head = temp; } else {//insert at middle or end while ( q != null && q.getElement2() > temp.getElement2()) { p = q; q = q.getNext(); } p.setNext(temp); temp.setNext(q); } } } //This is the regex method, it will take the users input and break it apart; i think the error may originate from somewhere here. public Polynomial prep(String s) { Polynomial poly = new Polynomial(); String str1 = "(-?[0-9]{1,})x\\^(-?[0-9]{1,})?"; String str2 = "(-?[0-9]{1,})x"; String str3 = "(-?[0-9]{1,})"; Pattern p = Pattern.compile(str1); Pattern p2 = Pattern.compile(str2); Pattern p3 = Pattern.compile(str3); Matcher matcher = p.matcher(s); Matcher matcher2 = p2.matcher(s); Matcher matcher3 = p3.matcher(s); int index = 0; boolean matchFound; do{ if(matchFound = matcher.find(index)) { for (int i=1; i<=matcher.groupCount(); i++) { String groupStr = matcher.group(i); System.out.println(groupStr); poly.add(Integer.parseInt(matcher.group(1)),Integer.parseInt(matcher.group(2))); } index = matcher.end(); } else if (matchFound = matcher2.find(index)) { String groupStr = matcher2.group(1); System.out.println(groupStr); System.out.println(1); poly.add(Integer.parseInt(matcher2.group(1)),1); index = matcher2.end(); } else if (matchFound = matcher3.find(index)) { String groupStr = matcher3.group(1); System.out.println(groupStr); System.out.println(0); poly.add(Integer.parseInt(matcher3.group(1)),0); index = matcher3.end(); } else matchFound = false; }while(matchFound); System.out.println("Done"); return poly; } public String toString() { Node current = head.getNext(); String output = ""; while(current != null) { output += "(" +current.Element1 + "," + current.Element2 + ")"; current = current.getNext(); } return output; } class Node { int Element1, Element2; Node next; public Node(int Element1,int Element2) { this.Element1 = Element1; this.Element2 = Element2; next = null; } public Node(int Element1, int Element2, Node n) { this.Element1 = Element1; this.Element2 = Element2; next = n; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public int getElement1() { return Element1; } public void setElement1(int element1) { Element1 = element1; } public int getElement2() { return Element2; } public void setElement2(int element2) { Element2 = element2; } } } public class Project { public static void main(String[] args) { Polynomial p1 = new Polynomial(); Polynomial p2 = new Polynomial(); /*p1.add(2, 8); p1.add(0, 3); p1.add(4, 5); p1.add(6, 7);*/ p1 = p1.prep("-12x^5+6x^3-3x+5"); System.out.println(p1); } }