• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

5-2购物车空指针异常

package com.immo.car;
public class Goods {
   private   String   goodId;
   private   String goodName;
   private   double    price;
   private   String    goodDesp;
   
   Goods(String  goodId,String  goodName,double price,String goodDesp){
    this.goodId=goodId;
    this.goodName=goodName;
    this.price=price;
    this.goodDesp=goodDesp;
   }
   Goods(){
   
   }
public String getGoodId() {
return goodId;
}
public void setGoodId(String goodId) {
this.goodId = goodId;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getGoodDesp() {
return goodDesp;
}
public void setGoodDesp(String goodDesp) {
this.goodDesp = goodDesp;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((goodDesp == null) ? 0 : goodDesp.hashCode());
result = prime * result + ((goodId == null) ? 0 : goodId.hashCode());
result = prime * result + ((goodName == null) ? 0 : goodName.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
  return true;
if (obj == null)
  return false;
if (getClass() != obj.getClass())
  return false;
Goods other = (Goods) obj;
if (goodDesp == null) {
  if (other.goodDesp != null)
   return false;
} else if (!goodDesp.equals(other.goodDesp))
  return false;
if (goodId == null) {
  if (other.goodId != null)
   return false;
} else if (!goodId.equals(other.goodId))
  return false;
if (goodName == null) {
  if (other.goodName != null)
   return false;
} else if (!goodName.equals(other.goodName))
  return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
  return false;
return true;
}
@Override
public String toString() {
return "[商品编号:" + goodId + ", 商品名称:" + goodName + ", 商品价钱:"
   + price + ", 商品描述:" + goodDesp + "]";
}
   
}

package com.immo.car;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage {
        private  Set  goodset;
  public Set getGoodset() {
   return goodset;
  }
  
  public GoodsManage() {
// TODO Auto-generated constructor stub
         }
  
  public void setGoodset(Set goodset) {
   this.goodset = goodset;
  }
        Set   s=new HashSet();
  public   void   improtGoods(){
   Goods   goods1=new Goods("s1001","矿泉水",90,"娃哈哈私人订制矿泉水");
   Goods   goods2=new Goods("s1002","旺仔牛奶",100,"再看再看就把你喝掉");
   Goods   goods3=new Goods("s1003","薯片",40,"咔吱脆");
   Goods   goods4=new Goods("s1004","苹果",30,"糖心苹果");
   s.add(goods1);
   s.add(goods4);
   s.add(goods3);
   s.add(goods2);
   setGoodset(s);
  }
  public  void   displayAllGoods(){
  /*Iterator it= s.iterator();
  while (it.hasNext()) {
    System.out.println("目前:"+it.next());
   
  }
*/ Set set= this.getGoodset();
      for (Goods goods : set) {
  System.out.println(goods);
}
  }
  
}



package com.immo.car;
public class GoodsInCart {
     private  Goods  goods;
     private  int num;
     
     GoodsInCart(Goods  goods,int num){
      this.goods=goods;
      this.num=num;
     }
     
     GoodsInCart(){
      
     }
public Goods getGoods() {
  return goods;
}
public void setGoods(Goods goods) {
  this.goods = goods;
}
public int getNum() {
  return num;
}
public void setNum(int num) {
  this.num = num;
}
@Override
public String toString() {
  return "GoodsInCart [goods=" + goods + ", num=" + num + "]";
}
     
     
}

package com.immo.car;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class ShopingCart {
      public   Map map;
      
      Scanner   sc=new Scanner(System.in);
     Boolean  flag=true;
     GoodsInCart   inCa;
     int  num=0;
      public  void   addGoodsCart(GoodsManage gm){
      if (gm.s.isEmpty()) {
      System.out.println("请先导入数据"+gm.s.isEmpty());   
      }else {
           System.out.println("友情提示:");
             gm.displayAllGoods();
            
           System.out.println("请输入商品编号");
            String  id=sc.next();
          Set set= gm.getGoodset();
           for (Goods  g : set) {
         if (g.getGoodId().equals(id)) {
      System.out.println("请输入数量");
      num=sc.nextInt();
         inCa=new  GoodsInCart(g, num);
             System.out.println("数量"+inCa.toString());
             System.out.println(g);
             map.put(id, inCa);
    // System.out.println(map.isEmpty());
   
  }
}
    //System.out.println(it.next());
  }
  
}
  public   void   Anset(){
   System.out.println("请输入商品编号");
   String  Goodsid=sc.next();
   
   Set set=map.keySet();
   for (String st:set) {
    if (st.contains(Goodsid)) {
     System.out.println("请输入数量");
     num=sc.nextInt();
     if (num==0) {
      map.remove(st);
     }
    }
   }
   
   
  }
  
  public   void   balanceGoods(){
  Collection c= map.values();
  for (GoodsInCart goodsInCart : c) {
  double  price= goodsInCart.getNum()*goodsInCart.getGoods().getPrice();
  System.out.println("共计:"+price);
  }
  }
}
      

import java.util.Iterator;
import java.util.Set;
public class Test {
        public static void main(String[] args) {
         Goods  sGoods=new Goods();
   ShopingCart  s=new ShopingCart();
   //GoodsInCart  inCart=new  GoodsInCart();
       GoodsManage  manage=new GoodsManage();
     
       manage.improtGoods();
  Set set= manage.getGoodset();
  for (Goods goods : set) {
   System.out.println(goods);
  }
   //manage.displayAllGoods();
   s.addGoodsCart(manage);
  }
}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

050001bue228jnjrjfwjll.jpg
首先来看报错位置,ShopingCart的31行;
050002ljmbjrgclvjmqzjq.jpg
从这里可以看到是你的map出问题了,上下检查之后发现,map是ShopingCart的成员属性,但是,你在Shoping类中仅仅是定义了一个map属性,但是map没有进行初始化,所以不能直接调用put的方法进行添加键值对的操作,请同学对map进行初始化之后,再进行添加操作!祝,学习顺利!

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!