java之空指针异常

关于空指针异常
麻烦大佬帮我看下那里出现了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 bb = new ArrayList();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 a) {List copy = new ArrayList();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);
}
}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部