Reputation: 49
What I want the program to do: create a stack and manipulate it with the calculate method in HP12C class.
I'm kinda lost here becouse it's been a while since I used Java and I'm having troubles creating and using objects, if you could help me with that...
Since the last time I posted here people were acusing me of posting my homework for you to do, I don't wan't that. If you feel like what I'm asking is too much, just give me a tip on what I'm doing wrong, maybe I can go from there. Thanks.
public interface Stack {
public void push(String element);
public String pop();
public int size();
public String peek();
}
public class LinkedStack implements Stack {
String elements[];
int top;
@Override
public void push(String element) {
top++;
elements[top] = element;
}
@Override
public String pop() {
String element;
element = elements[top];
top--;
return element;
}
@Override
public int size() {
return 0;
}
@Override
public String peek() {
return null;
}
}
public class HP12C {
public Stack stack;
public HP12C() {
stack = new LinkedStack();
}
public double calculate(String line) {
String n, n1, n2;
double d = 0, d1 = 0, d2 = 0;
// Quebrar a linha nos parametros
String elements[] = line.split(" ");
// Empilhar e calcular
for (String e : elements) {
// Distinguir o que eh operador e o que nao eh
switch (e) {
case "+":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 + d2;
n = String.valueOf(d);
stack.push(n);
break;
case "-":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 - d2;
n = String.valueOf(d);
stack.push(n);
break;
case "*":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 * d2;
n = String.valueOf(d);
stack.push(n);
break;
case "/":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 / d2;
n = String.valueOf(d);
stack.push(n);
break;
default:
stack.push(e);
break;
}
stack.push(e);
}
return -1;
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class HP12CTest {
@Test
public void testSimple() {
HP12C hp = new HP12C();
double result = hp.calculate("10 15 +");
assertEquals(25, result, 0);
}
}
trace of the error:
java.lang.NullPointerException
at LinkedStack.push(LinkedStack.java:9)
at HP12C.calculate(HP12C.java:55)
at HP12CTest.testSimple(HP12CTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Upvotes: 0
Views: 265
Reputation: 184
You have to initialize your array:
String[] elements = new String[lengthOfYourArray];
Upvotes: 0
Reputation: 44240
Within LinkedStack you have a field which is never initialised:
String elements[];
This is equivalent to
String elements[] = null;
When you try to access elements[top]
, you are doing so with a null reference, resulting in your exception.
See: How to initialize an array in Java?
Be aware that if you initialise your stack to, for example, 20 elements then you will need to handle the situation where a user of the class tries to push more than 20 elements to the stack.
You could do this by copying the underlying array and doubling the size or alternatively you could use an auto-resizing datastructure like one of Java's List
implementations instead of a primitive array.
Upvotes: 2