相关疑难解决方法(0)

Java - 不能访问类型为Foo的封闭实例

我有以下代码:

class Hello {
    class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        Thing thing1 = new Thing();
        System.out.println("Hello, World!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道Thing什么都不做,但我的Hello,World程序在没有它的情况下编译得很好.这只是我定义的类失败了.

它拒绝编译.我开始No enclosing instance of type Hello is accessible."创造一个新的东西.我猜是:

  1. 我有系统级问题(在DrJava或我的Java安装中)或
  2. 我对如何在java中构建一个工作程序有一些基本的误解.

有任何想法吗?

java inner-classes

297
推荐指数
5
解决办法
23万
查看次数

如何在Java中创建对象数组

我正在尝试创建一个由子类定义的对象数组(我认为这是正确的术语).我可以看到问题是反复出现,但实施仍然存在问题.

我的代码

public class Test {

    private class MyClass {
        int bar = -1;
    }

    private static MyClass[] foo;

    public static void main(String args[]) {

        foo = new MyClass[1];
        foo[0].bar = 0;

    }       
}
Run Code Online (Sandbox Code Playgroud)

给出错误

线程"main"java.lang.NullPointerException中的异常.

为了使其合理化,我将其分解为最简单的术语:

public class Test {

    private static int[] foo;

    public static void main(String args[]) {

        foo = new int[1];
        foo[0] = 0;

    }       
}
Run Code Online (Sandbox Code Playgroud)

这似乎工作.我只是看不出我的两个例子之间的区别.(我知道我的第一个没有意义,但MyClass最终将包含更多数据.)

我很确定这里会问这个问题并得到很好的回答.我想我实施了解决方案:

MyClass[] foo = new MyClass[10];
foo[0] = new MyClass();
foo[0].bar = 0;
Run Code Online (Sandbox Code Playgroud)

但上面的第二行发出错误

不能访问类型为Test的封闭实例.

我确实理解ArrayList是一种前进的方式,但我正在努力掌握底层的概念.

注意 …

java arrays object

5
推荐指数
2
解决办法
6万
查看次数

获取"没有封闭的类型实例..."错误

谁能告诉我这段代码有什么问题?

public abstract class BoardTestBean{
    protected String month;
    protected String day;
    protected String name;


    public String getMonth() {
        return month;
    }
    public void setMonth(String month) {
        this.month = month;
    }
    public String getYear() {
        return day;
    }
    public void setYear(String day) {
        this.day = day;
    }
    public String getName(){
        return name;
    }


    //Classes
    public class SAT {
        boolean pre2005=false;
        private String verbal;
        private String quantitative;
        private String writing="";//if pre-2005, do not set. It is not used. 

        public SAT() {
            super(); …
Run Code Online (Sandbox Code Playgroud)

java subclass object abstract

3
推荐指数
1
解决办法
6124
查看次数

不能访问类型为Foo的封闭实例

我正在创建一个用于连接到带有平板电脑/手机的嵌入式蓝牙设备的程序.我的代码主要由我给出的不同代码组成,我发现了.处理蓝牙设备连接的代码主要来自BlueTerm程序的源代码.我试图消除对某个给定类之一的需要,并开始得到一些我不知道如何解决的错误.这是我的开始活动的代码:

public class AndroidBluetooth extends Activity {

/** Called when the activity is first created. */
    private static BluetoothAdapter myBtAdapter;
    private static BluetoothDevice myBtDevice;
    private ArrayAdapter<String> btArrayAdapter;
    private ArrayList<BluetoothDevice> btDevicesFound = new ArrayList<BluetoothDevice>();
    private Button btnScanDevice;
    private TextView stateBluetooth;
    private ListView listDevicesFound;
    private InputStream iStream;
    private OutputStream oStream;
    private BluetoothSocket btSocket;
    private String newDeviceAddress;
    private BroadcastReceiver mReceiver;

    private static BluetoothSerialService mSerialService = null;

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE = 1;

    private static TextView mTitle;

    // Message …
Run Code Online (Sandbox Code Playgroud)

android bluetooth

2
推荐指数
1
解决办法
3992
查看次数

标签 统计

java ×3

object ×2

abstract ×1

android ×1

arrays ×1

bluetooth ×1

inner-classes ×1

subclass ×1