总是只能执行一半,只能加入一个信息,不会用动态聚集~~
import java.util.StringTokenizer;
import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.Vector;//commond四个子类:免税商品taxfree,进口商品imgoods,进口免税商品import_free和普通商品common
class taxfree extends commond
{
   public taxfree (int unit, String name, double price)
   {
    super(unit, name, price,0);
    }
   public taxfree (commond c,String name, double price)
    {
    super(c.unit, name, price,0);System.out.print("请:");
    }
}//免税商品
class import_free extends commond
{
    public import_free (int unit, String name, double price)
   {
     super(unit, name, price,0.05);
    }
   public import_free (commond c, String name, double price)
   {super(c.unit, name, price,0.05);}
}//进口免税商品
class imgoods extends commond
{
    public imgoods (int unit, String name, double price)
   {
     super(unit, name, price,0.15);    }
   public imgoods (commond c, String name, double price)
    {super(c.unit, name, price,0.15);}
}//进口商品
class common extends commond
{
     public common (int unit, String name, double price)
   {
     super(unit, name, price,0.1);
    }
     public common (commond c, String name, double price)
    {super(c.unit, name, price,0.1);}
}//普通商品
//commond类
class  commond
{
    protected int unit;
    protected String name;
    protected double price;
    protected double tax;
    protected double price_tax;   //“商品”类的属性 名称 价格 基本营业税   public commond (int unit, String name, double price, double tax)
   {
     this.unit=unit+1;
     this.name=name;
     this.tax=tax;
     this.price_tax=price*tax;
     this.price=price+price_tax;
    }    public commond()
    {}   void print(int t,Vector c) 
 {
     DecimalFormat   nf   =   new   DecimalFormat("##0.##");
     double total_price=0.0;
     double total_tax=0.0;
     for(int j=0; j<t;j++)
     {
        total_tax=total_tax+((commond)c.elementAt(j)).price_tax;
        total_price=total_price+((commond)c.elementAt(j)).price;
        System.out.println(((commond)c.elementAt(j)).unit+((commond)c.elementAt(j)).name+": $ "+nf.format(((commond)c.elementAt(j)).price));
      } 
      System.out.println("total price is :"+nf.format(total_price));
      System.out.println("total tax is :"+nf.format(total_tax));
  }
}public class basket   //basket类 
{
public Vector c;
void basket()
{
  c=new Vector();
}
//方法:打印商品list
 void drawlist(Vector c,int t)

     commond b;   b=new  commond();
     b.print(t, c);
}//方法:得到数据的过程
static public int getdetail(String[] A,Vector c)
{
   int a;
   int t=0;
   BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
   System.out.print("继续?(不为零的数:继续; 0:打印商品list):");
   try{
     a=Integer.parseInt(buf.readLine());
     while( a!=0)
     {
       System.out.println("输入所买商品编号:(1.书;2.CD;3.巧克力;4.进口巧克力;5.进口香水;6.香水;7.头疼药)");
     try{
         int number=Integer.parseInt(buf.readLine());
         System.out.print("请输入价格:");
         try{
            double mm=Double.parseDouble(buf.readLine());
            
            switch (number)
           {
              case 1 :   case 3:  case 7:
              {
               taxfree newtaxfree=new taxfree(0,A[number-1], mm);
               c.insertElementAt((Object)newtaxfree,t);    System.out.print("123123123123");
               t++;      break;
              }
              case 2 :  case 6:
             {
               common newcommon=new common(0, A[number-1], mm);
               c.insertElementAt((Object)newcommon,t);
               t++;           break;
               }
               case 4 :
              {
                import_free  newimport_free=new import_free(0, A[number-1], mm) ;
                c.insertElementAt((Object)newimport_free,t);
                t++;           break;
               }
              case 5 :
              {
                imgoods  newimgoods=new imgoods(0, A[number-1], mm);
                c.insertElementAt((Object)newimgoods,t);
                t++;           break;
               }
              default:
              {System.out.println("商品号超出范围!请重新输入");break;}
           }//switch
          } catch(IOException e)  {System.err.println("error!!"+e); }    //测试mm输入
       } catch(IOException e)  {System.err.println("error!!"+e); }     //测试number输入       System.out.print("继续?(输入不为零的数)");
         try{
             a=Integer.parseInt(buf.readLine());
             }  catch(IOException e) {System.err.println("error!!"+e);}
      }//while
       if( a==0)
       return t;
    } catch(IOException e) {System.err.println("error!!"+e);}//测试a输入
     return  t;
}
//main测试数据
static public void main(String args[])
{
   String A[]={"书","CD","巧克力","进口巧克力","进口香水","香水","头疼药"};
   basket b= new  basket();
   int tt=b.getdetail(A,b.c);
   b.drawlist(b.c,tt) ;
 }//main}

解决方案 »

  1.   

    void basket() 

      c=new Vector(); 

    这个构造函数写错了,改成 
    public basket(){
      c=new Vector();
    }
    就可以了................
      

  2.   

    构造函数是没有返回类型的,void也是一种类型,那void basket就不是构造函数了,
    你在main函数里用的那个new basket()是默认的构造函数,不是你写的那个,所以程序不会报错
    可是你仔细看异常报告就会发现是一个NullPointerException,说明c没有实例化,自然是你写的那
    个构造函数没有用上