关于Java SE出现报错Exception in thread "main" java.lang.Error: Unresolved compilation problems...
来源:互联网 发布:windows 98系统下载 编辑:程序博客网 时间:2024/05/18 11:45
自学Java一个月了,今天在学习序列化得时候自己动手写了一个例程,如下:
package com.JoyWork.Seria;
import java.io.*;
public class text01 implements Serializable{
public static void main(String[] args) {
// TODO Auto-generated method stub
GameCharater Elt = new GameCharater(50,"Elf",new String[]{ "bow","sword","dust"});
GameCharater Troll = new GameCharater(200,"Troll",new String[]{"bara hands","big ax"});
try{
FileOutputStream fs = new FileOutputStream("GameCharater.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(Elt);
os.writeObject(Troll);
os.close();
} catch(Exception e){e.printStackTrace();}
try{
ObjectInputStream os = new ObjectInputStream(new FileInputStream("GameCharater.ser"));
Object Elf = os.readObject();
Object Trolll = os.readObject();
GameCharater elf = (GameCharater)Elf;
GameCharater troll = (GameCharater)Trolll;
System.out.println(elf.getPower()+" "+elf.getType());
System.out.println(troll.getPower()+" "+troll.getType());
} catch(Exception e){e.printStackTrace();}
}
public class GameCharater implements Serializable{
private int Power;
private String Type;
private String[] weapons;
public GameCharater (int p,String t,String[] w){
Power = p;
Type = t;
weapons = w;
}
public int getPower() {
return Power;
}
public String getType() {
return Type;
}
public String getWeapons() {
String List = null;
for(int i =0;i<weapons.length;i++){
List += weapons[i]+" ";
}
return List;
}
}
}
结果发现报错:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type text01 is accessible. Must qualify the allocation with an enclosing instance of type text01 (e.g. x.new A() where x is an instance of text01).
No enclosing instance of type text01 is accessible. Must qualify the allocation with an enclosing instance of type text01 (e.g. x.new A() where x is an instance of text01).
at com.JoyWork.Seria.text01.main(text01.java:8)
即类型text01没有外围实例访问。必须与类型text01的类实例限定分配(例如x.newA(),其中x是text01的一个实例)。
类型text01没有外围实例访问。必须与类型text01的类实例限定分配(例如x.newA(),其中x是text01的一个实例)。
经过查阅资料发现是内部类的问题,因为我写的内部类GameCharater是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。
解决办法有两个:1、不要写内部类,写成单独的一个bean对象 2、将内部类写成public static类型的
记录一下,也方便大家参考- 关于Java SE出现报错Exception in thread "main" java.lang.Error: Unresolved compilation problems...
- Exception in thread "main" java.lang.Error: Unresolved compilation problems:
- Exception in thread "main" java.lang.Error: Unresolved compilation problems 解决方案
- Exception in thread main java.lang.Error Unresolved compilation problem
- Exception in thread "main" java.lang.Error: Unresolved compilation problem
- Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- Exception in thread main java.lang.Error Unresolved compilation problem
- Exception in thread "main" java.lang.Error: Unresolved compilation problem
- Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- Atitit. Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- java.lang.Error: Unresolved compilation problems:
- java.lang.Error: Unresolved compilation problems:解决方案
- java.lang.Error: Unresolved compilation problems:解决方案
- java.lang.Error: Unresolved compilation problems:解决方案
- Caused by: java.lang.Error: Unresolved compilation problems:
- Caused by: java.lang.Error: Unresolved compilation problems:解决办法
- Hive启动报错:Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: java.io.
- 第七周上机实践项目-项目一-成员函数,友元函数和一般函数的区别
- form表单中用户名和密码对齐的简单实现
- 深度学习-LeCun、Bengio和Hinton的联合综述-Deep Learning一篇nature综述论文
- 欧拉函数(转)
- Android_照相机Camera_调用系统照相机返回data为空
- 关于Java SE出现报错Exception in thread "main" java.lang.Error: Unresolved compilation problems...
- 我爱学Java之接口和抽象类
- curl模拟post请求提交
- 结果集数据怎么转换成二维数组
- 剑指offer之字符数组内容翻转问题
- Camel Splitter分配器以及Aggregate聚合器使用实例
- WPF TreeviewItem Click 事件
- 祖玛(Zuma)
- 基于深度学习的图像识别进展:百度的若干实践