I'm getting either a null pointer, or some other kind of problem. So far, in this program, I only have the multiply function completed to the point where it ought to work. However, either in my toString method or my function call, i think I've got something wrong. The problem may be elsewhere, too, but I think it's just there. You have to enter a value first, then you select multiply on the case statement. I have to do this part first so I can test to make sure the code I've got works. Without further ado:

My main:

Code :

import java.util.Scanner; public class main { public static void main(String [] args) { Scanner scan = new Scanner(System.in); int x = 0; String choice = null; String entry = null; String answer = null; Number n = new Number(); Number m = new Number(); while (x != 1) { /* This is a simple menu, which feeds into a switch statement. The switch * has to be done with a literal or an integer, so we first parse the * string given by the scanner to just the character at position 0 (the * first spot). It switches accordingly. */ System.out.print("Enter a value: e Add a value: a\n" + "Subtract: s Multiply: m\n" + "Reverse sign: r Clear: c\n" + "Quit: q\n -->" ); choice = scan.next(); char choice1 = choice.charAt(0); switch(choice1) { case 'e': System.out.print("Please enter a number: "); entry = scan.next(); n = new Number(entry); break; case 'a': break; case 's': break; case 'm': System.out.print("Please enter a number: "); entry = scan.next(); m = new Number(entry); n = m.multiply(n); answer = n.toString(); System.out.println("" + answer); break; case 'r': n.reverseSign(); break; case 'c': n = new Number(); break; case 'q': x = 1; break; default: System.out.println("Invalid selection, please try again"); } } } }

My Number Class

Code :

public class Number { private Node low, high; private int digitCount = 0; private int decimalPlaces = 0; private boolean negative = false; public Number()// constructor { low = null; high = null; } // while I'm pretty sure that to enter a value, you don't need to worry about this // this constructor is needed when you are subtracting or doing any other elementary // operation. without it, it wouldn't be able to make it a linked list. public Number(String str) { accept(str); } public void accept(String str) { int length = str.length(); // in case of a decimal number, we have to make sure we're ready for it. we do that by parsing a float, then // later casting it to an integer. we use this only to see if x is greater than 0. int x = (int)Float.parseFloat(str); // if the integer is less than 0, it is negative // and we flip the sign, else we do nothing if (x < 0) { System.out.println("" + x); reverseSign(); } for(int i = 0; i < length; i++) { char place = str.charAt(i); /* I'm checking to see if the character at the place is a * digit. If it is, I cast it as an integer, which it is, * and send it to the insertNode method, which adds it as a node. */ if(Character.isDigit(place)) { insertNodeBack((int)place); } else if(place == '.') { decimalPlaces = length - i; } else { //Do nothing, it's a negative sign. The loop will increment. } } } public Number add(Number n) { return n; //. Returns a Number which represents "this + n". } public Number subtract(Number n) { return n; //. Returns a Number which represents "this - n". } public Number multiply(Number n) { int newDigit; Number product = new Number(); Node npointer = n.high; while (npointer != null) { Number partialProduct = new Number(); int carry = 0; Node current = this.low; while(current != null) { newDigit = npointer.getValue() * current.getValue(); carry = newDigit / 10; newDigit = newDigit %10; partialProduct.digitCount++; current = current.previous; if(carry != 0) { partialProduct.insertNodeFront(carry); partialProduct.digitCount++; } product.insertNodeBack(0); product.digitCount++; product = product.addAbsolute(partialProduct); npointer = npointer.next; product.decimalPlaces = this.decimalPlaces + n.decimalPlaces; } } return product; } public void reverseSign() //This method changes the sign of the number represented by the linked list. { if (negative == true) negative = false; else negative = true; } //Returns a String representation of the Number (so it can be displayed by System.out.print()). public String toString() { int beforeDecimal = digitCount - decimalPlaces, digit; Node current = this.high; String solution = null; if(negative) solution += '-'; for(int i = 0; i < beforeDecimal; i++) { digit = current.getValue(); solution += digit; current = current.next; } solution += '.'; for(int i = digitCount - beforeDecimal; i < digitCount; i++) { digit = current.getValue(); solution += digit; current = current.next; } return solution; } /* Because of the way I've set up the linked list, the first piece added will always be the highest * order node. Thus, this method is the same to just adding a last node every time. When this method is called * we already know if we have a negative or not, from the parsing done in accept. If the list isn't empty, we simply * set the pointer for the low node to the node we create. */ private void insertNodeBack(int value) { Node newNode = new Node(value); if (isEmpty()) this.high = newNode; else { low.next = newNode; newNode.previous = low; } this.low = newNode; } /*This method tells if the list is empty or not.*/ private boolean isEmpty() { return high == null; } private Number addAbsolute(Number n) { Number sum = new Number(); int carry = 0; int added; Node thisPointer = this.low; Node nPointer = n.low; while (thisPointer != null) { added = thisPointer.getValue();// + nPointer.getValue(); carry = added / 10; added = added %10; sum.digitCount++; thisPointer = thisPointer.previous; nPointer = nPointer.previous; if(carry != 0) { sum.insertNodeFront(carry); sum.digitCount++; } //set ap value for decimal places in sum } return sum; } /* Well, because of the functions we use, we have to properly be able to add to the head of the list * instead of just the back. To do this, we'll do pretty much the same thing as we did on insertNodeTail, * however, we'll insert to the front. */ private void insertNodeFront(int value) { Node newNode = new Node(value); if (isEmpty()) low = newNode; //its both high and low if list is empty else { high.previous = newNode; newNode.next = high; // reassign the pointers for existing high } high = newNode; //override existing high } private int compareToAbsolute(Number n) { return decimalPlaces; } private Number subtractAbsolute(Number n) { Number difference = new Number(); int borrow = 0, newDigit; Node thisPointer = this.low; Node nPointer = n.low; while (thisPointer != null) { newDigit = thisPointer.getValue() - nPointer.getValue(); if (newDigit < 0) { newDigit +=10; borrow = 1; } else { borrow = 0; } difference.insertNodeFront(newDigit); difference.digitCount++; //set app value for dec. places } return difference; } }

And my Node class

Code :

public class Node { public int data; public Node next; public Node previous; public Node(int value) { data = value; } public int getValue() { return data; } }

Any help is greatly appreciated. It's my shot at what is supposed to be a calculator capable of the functions provided.