【问题标题】:why can't I create an instance of MyVector ?为什么我不能创建 MyVector 的实例?
【发布时间】:2012-01-10 03:57:49
【问题描述】:

鉴于以下代码,由于某种原因,它不会创建 MyVector 的实例。可能是什么问题?问题出现在 Main 行:

MyVector vec = new MyVector();

但是,当我使用另一个构造函数创建 MyVector 的实例时:

MyVector vec2 = new MyVector(arr);

它编译并分配实例。

类点:

public class Dot {

    private double dotValue;

    public Dot(double dotValue)
    {
        this.dotValue = dotValue;
    }

    public double getDotValue()
    {
        return this.dotValue;
    }

    public void setDotValue(double newDotValue)
    {
        this.dotValue = newDotValue;
    }

    public String toString()
    {
        return "The Dot's value is :" + this.dotValue;
    }

}

类 MyVector

public class MyVector {

    private Dot[] arrayDots;

    MyVector()
    {       
        int k = 2;
        this.arrayDots = new Dot[k];
    }

    public MyVector(int k)
    {
        this.arrayDots = new Dot[k];
        int i = 0;
        while (i < k)
            arrayDots[i].setDotValue(0);
    }

    public MyVector(double array[])
    {
        this.arrayDots = new Dot[array.length];
        int i = 0;
        while (i < array.length)
        {
            this.arrayDots[i] = new Dot(array[i]);
            i++;
        }
    }

}

和主要

public class Main {

    public static void main(String[] args) {


        int k = 10;
        double [] arr = {0,1,2,3,4,5};
        System.out.println("Enter you K");
        MyVector vec = new MyVector();  // that line compile ,but when debugging it crashes , why ? 
        MyVector vec2 = new MyVector(arr);


    }
}

问候 罗恩

【问题讨论】:

  • 抛出什么异常?
  • 您不能以这种方式初始化对象数组this.arrayDots = new Dot[k]; ...您必须使用for循环并初始化每个索引。
  • 您需要提供程序“崩溃”时调试器提供给您的堆栈跟踪。
  • @CoolBeans - 实际上,您可以那样初始化它。只是您将其初始化为所有null ...这可能不是您想要/需要的。
  • @StephenC - 对,这就是我的意思。感谢您澄清它:)

标签: java constructor


【解决方案1】:

我将您的代码复制到我的 Eclipse IDE 中并得到“org.eclipse.debug.core.DebugException:com.sun.jdi.ClassNotLoadedException:在检索数组的组件类型时发生类型未加载”。单击 arrayDots 变量时出现异常。

您的代码可以正常工作。调试器有问题,因为没有加载 Dot 类。 另见:http://www.coderanch.com/t/433238/Testing/ClassNotLoadedException-Eclipse-debugger

你可以改变你的 Main 如下(我知道这不是很漂亮)

public static void main(String[] args) {


    int k = 10;
    double [] arr = {0,1,2,3,4,5};
    System.out.println("Enter you K");
    new Dot(); // the classloader loads the Dot class
    MyVector vec = new MyVector();  // that line compile ,but when debugging it crashes , why ? 
    MyVector vec2 = new MyVector(arr);


}

【讨论】:

  • 嗨,“未加载点类”是什么意思?我已经在 c++ 上工作了三年,这是我在 Java 的第一周,所以我是新手,所以请放轻松...
  • 在 java 中,ClassLoader 负责加载类。这是按需动态完成的,即在第一次实例化对象时。这意味着“加载”。在您的情况下,到目前为止还没有创建 Dot 对象,因此 ClassLoader 没有加载它。在我 5 年的 Java 职业生涯中,我从未遇到过这样的问题 ;-)
  • 好的,那现在怎么办?将 Eclipse 扔出窗口?
  • 您可以按照我在代码中的建议通过在 new Vector() 之前调用 new Dot() 来强制 ClassLoader 加载它,或者查看处理相同问题的stackoverflow.com/questions/1367730/…。顺便说一句,我的调试器不会崩溃,它只是在您单击消息区域中的 arrayDots 变量时显示错误消息
【解决方案2】:

您的默认构造函数不可见。在构造函数前面添加public关键字。

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多