Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public Polynomial multiply (Polynomial p) {
  2.  
  3. Polynomial a = this;
  4. Polynomial total = new Polynomial();
  5. Node result = new Node (0,0,null);
  6. total.poly = result;
  7. Node curr = a.poly;
  8.  
  9. while (true){
  10. if (curr == null){ //stops while loop
  11. break;
  12. }
  13.  
  14. Node other = p.poly;
  15. Polynomial product = new Polynomial();
  16. Node curr_product = new Node (0,0,null);
  17. product.poly = curr_product;
  18.  
  19. while (other != null){
  20.  
  21. int new_degree = curr_product.term.degree;
  22. float new_coeff = curr_product.term.coeff;
  23.  
  24. new_degree = curr.term.degree + other.term.degree;
  25. new_coeff = curr.term.coeff * other.term.coeff;
  26.  
  27. curr_product.next = new Node (new_coeff, new_degree, null);
  28. curr_product = curr_product.next;
  29.  
  30. other = other.next;
  31. }
  32.  
  33. product.poly = product.poly.next;
  34. curr = curr.next;
  35.  
  36. //null pointer exception here vvvv;
  37. total = total.add(product);
  38.  
  39. }
  40. return total;
  41. }
  42.  
  43. It also gives me an error in the add method at line 173:
  44. result.next = new Node (other.term.coeff, other.term.degree,null);
  45.  
  46. Although it runs and compiles if I do run the add method. It only gives me nullpointerexception error when I run mult. method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement