Java NoSuchMethodError

时间:2016-04-19 22:53:49

标签: java

我正在尝试用对象理解堆栈,所以我输入了这个,但问题是我得到的这个非常奇怪的消息,我无法理解。它说“线程中的异常”主“java.lang.NoSuchMethodError:Stack.push(Ljava / lang / Object;)V     在TestObjectStack.main(TestObjectStack.java:12)“。我搜索了它,但我仍然无法弄清楚我做错了什么。我重新编写了主方法标题,但是没有修复它。有没有人有我缺少的任何建议或见解?非常感谢!:

public class TestObjectStack 
{
public static void main(String[] args)
{
    Object o;
    Stack test = new Stack();

    test.push("Fred");
    test.push(20);
    test.push(new ThingB("Barney", 42));

    Stack copy = new Stack(test);

    System.out.println("Stack test: " + test);
    System.out.println(test.pop());
    System.out.println("Stack test: " + test);
    System.out.println("Stack copy: " + copy);
    if(test.isEmpty()) System.out.println("Empty");

    o = test.pop();
    System.out.println(o);
    if(o instanceof String)
    {
        String s = (String) o;
        System.out.println("String length = " + s.length());
    }
    else
        System.out.println("Not a String");
    if(test.isEmpty()) System.out.println("Empty");

    o = test.pop();
    System.out.println(o);
    if(o instanceof String)
    {
        String s = (String) o;
        System.out.println("String length = " + s.length());
    }
    else
        System.out.println("Not a string");
    if(test.isEmpty()) System.out.println("empty");
}
 }

class ThingB
{
private String _name;
private int _ID;

public ThingB(String name, int ID)
{
    _name = name;
    _ID = ID;
}

public String toString()
{
    return "Thing B - name - " + _name + " ID = " + _ID;
}
}

class Stack
{
private Object[] _store;
private int _top;
private static final int MAXSIZE = 50;

public Stack()
{
    _store = new Object[MAXSIZE];
    _top = 0;
}

public Stack(Stack other)
{
    _store = new Object[other._store.length];
    _top = other._top;
    for(int i = 0; i < _top; ++i)
    {
        _store[i] = other._store[i];
    }
}

public boolean isEmpty()
{
    return (_top == 0);
}

public void push(Object item)
{
    if(_top >= _store.length)
    {
        Object[] temp = new Object[_store.length+ MAXSIZE];
        for(int i = 0; i < _top; ++i)
        {
            temp[i] = _store[i];
        }
        _store = temp;
    }
    _store[_top] = item;
    ++_top;
}

public Object pop()
{
    if(_top == 0) return 0;
    --_top;
    return  _store[_top];
}

public String toString()
{
    String s = "";

    s = s + "--Top--";
    for(int i = _top-1; i >= 0; --i)
    {
        s = s + " " + _store[i];
    }
    s = s + "--Bottom--";

    return s;
}

}

1 个答案:

答案 0 :(得分:-1)

我在IDE中执行了您的代码:IntelliJ IDEA。我有以下结果:

Stack test: --Top-- Thing B - name - Barney ID = 42 20 Fred--Bottom--
Thing B - name - Barney ID = 42
Stack test: --Top-- 20 Fred--Bottom--
Stack copy: --Top-- Thing B - name - Barney ID = 42 20 Fred--Bottom--
20
Not a String
Fred
String length = 4
empty

您的源代码工作正常,可能需要调整IDE参数。尝试使用简单的“Hello World”程序。

致以最诚挚的问候,

阿尔瓦罗