我有以下代码:
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."创造一个新的东西.我猜是:
有任何想法吗?
我正在尝试创建一个由子类定义的对象数组(我认为这是正确的术语).我可以看到问题是反复出现,但实施仍然存在问题.
我的代码
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是一种前进的方式,但我正在努力掌握底层的概念.
注意 …
谁能告诉我这段代码有什么问题?
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) 我正在创建一个用于连接到带有平板电脑/手机的嵌入式蓝牙设备的程序.我的代码主要由我给出的不同代码组成,我发现了.处理蓝牙设备连接的代码主要来自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)