关于空指针异常
麻烦大佬帮我看下那里出现了BUG-----本来在学线程那块,想写点东西来用一下锁
结果到测试的时候发现出现了空指针异常。。。。。。
代码如下:
package threadApp;
import java.util.ArrayList;
/**
- 四虎影院2–用容器list表示位置
*/
import java.util.List;
class HappyCinemaV1{
public static void main(String[] args) {
//四虎影院中原有的座位号–可用的位置
List a = new ArrayList();
a.add(1);
a.add(2);
a.add(5);
a.add(6);
a.add(7);
a.add(3);
//嬲哥要买的座位号
List b = new ArrayList();
b.add(5);
b.add(2);
b.add(1);
//包子要买的座位
List<Integer> bb = new ArrayList<Integer>();
bb.add(6);
bb.add(2);
bb.add(3);
SihuCinema sihu = new SihuCinema(a, "四虎影院");
happycustomer c = new happycustomer(b, "嬲哥");
happycustomer d = new happycustomer(bb, "包子");
c.start();
d.start();
}
}
//四虎影院
class SihuCinema{
List uselocation;
String name ;
happycustomer people;
//构造器
public SihuCinema(List list , String name) {
this.name = name;
uselocation = list;
}
//买票看电影
public boolean buylocation(List<Integer> a) {
List<Integer> copy = new ArrayList<Integer>();
copy.addAll(uselocation);
copy.removeAll(a);
if (uselocation.size()-copy.size() != a.size()) {
System.out.println("购票失败!!");
return false;
}
System.out.println("购票成功!!");
uselocation = copy;
return true;
}
}
class happycustomer extends Thread{
SihuCinema sihu;
List location; //顾客要买的座位号
String name;
//要买票的信息传入
public happycustomer(List b,String name) {
this.location = b;
this.name = name;
}
@Override
public void run() {
synchronized (sihu) {
boolean flag = sihu.buylocation(location);
if (flag) {
System.out.println(Thread.currentThread().getName() + "买的位置是:" + location);
}else {
System.out.println("位置不够");
}
System.out.println("剩余的位置为:" + sihu.uselocation);
}
}
}
204

被折叠的 条评论
为什么被折叠?



