为什么会出现空指针异常= =

为什么会出现空指针异常= =


public static void main(String[] args) {

GoodsManage goodsManage=new GoodsManage(new HashSet<Goods>());

goodsManage.importGoods();

goodsManage.displayAllGoods();

ShoppingCart shop=new ShoppingCart(new HashMap<String,GoodsInCart>());

shop.addGoodsToCart(goodsManage);

shop.displayAllInCart();

}


我已经实例化了GoodsManage  为什么后面调用还会出现空指针异常,请老师帮忙解答一下,谢谢

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
chrismorgen 2018-07-27 18:09:37

如下图所示,建议你在GoodsManager类中创建一个set集合,按下图修改就会在类加载的时候创建set集合,如果不创建set集合,只是声明set,那么在使用set集合的时候就会出现空指针异常,如果我的建议解决了你的问题,请采纳,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5b5aee820001317407030167.jpg

提问者 Demnz 2018-07-27 16:59:49

package com.imooc.marketManager;


import java.util.HashSet;

import java.util.Iterator;

import java.util.Set;


public class GoodsManage {

// 存放商品的容器

private Set<Goods> goodsSet;


public GoodsManage() {


}


public GoodsManage(HashSet<Goods> goodsSet) {

this.setGoodsSet(goodsSet);

}


public Set<Goods> getGoodsSet() {

return goodsSet;

}


public void setGoodsSet(Set<Goods> goodsSet) {

this.goodsSet = goodsSet;

}


public void importGoods() {

Goods one = new Goods("goods001", "水杯", 56, "不锈钢水杯");

Goods two = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机");

Goods three = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本");

Goods four = new Goods("goods004", "手机", 2300, "android手机");

goodsSet.add(one);

goodsSet.add(two);

goodsSet.add(three);

goodsSet.add(four);

System.out.println("商品导入成功!");

}


public void displayAllGoods() {

if (goodsSet.isEmpty()) {

System.out.println("商品还未导入,请先导入商品信息");

} else {

Iterator<Goods> it = goodsSet.iterator();

System.out.println("所有商品信息");

while (it.hasNext()) {

System.out.println(it.next());

}

}

}


}


Tender10 2018-07-27 16:22:26

GoodsManage gm = new GoodsManage();这样直接实例化。如果还有问题,可以将GoodsManage类的代码贴出来。


  • 提问者 Demnz #1
    谢谢,我去试试
    2018-07-27 16:46:20
  • 提问者 Demnz #2
    package com.imooc.marketManager; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManage { // 存放商品的容器 private Set<Goods> goodsSet; public GoodsManage() { } public GoodsManage(HashSet<Goods> goodsSet) { this.setGoodsSet(goodsSet); } public Set<Goods> getGoodsSet() { return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this.goodsSet = goodsSet; } public void importGoods() { Goods one = new Goods("goods001", "水杯", 56, "不锈钢水杯"); Goods two = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机"); Goods three = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本"); Goods four = new Goods("goods004", "手机", 2300, "android手机"); goodsSet.add(one); goodsSet.add(two); goodsSet.add(three); goodsSet.add(four); System.out.println("商品导入成功!"); } public void displayAllGoods() { if (goodsSet.isEmpty()) { System.out.println("商品还未导入,请先导入商品信息"); } else { Iterator<Goods> it = goodsSet.iterator(); System.out.println("所有商品信息"); while (it.hasNext()) { System.out.println(it.next()); } } } }
    2018-07-27 16:58:50
  • 提问者 Demnz #3
    试了,还是出现错误
    2018-07-27 17:00:14
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师