当前位置: 代码迷 >> Java相关 >> [求助]关于Exception in thread "main" java.lang.NullPointerExcepti ...
  详细解决方案

[求助]关于Exception in thread "main" java.lang.NullPointerExcepti ...

热度:826   发布时间:2007-06-09 11:12:42.0
[求助]关于Exception in thread "main" java.lang.NullPointerException的问题

能通过编译,但是不能运行!请各位高人帮忙看看!!
package com.wish.interfacedemo;
interface ComputerTotalSales
{
public double totalSalesByYear();
}
class Television implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 200;
}
}
class Computer implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 300;
}
}
class Mobile implements ComputerTotalSales
{
public double totalSalesByYear()
{
return 800;
}
}
class Shop
{
ComputerTotalSales[] goods;
double totalSales=0;
public Shop(ComputerTotalSales[] goods)
{
this.goods=goods;
}
public double giveTotalSales()
{
totalSales=0;
for(int i=0;i<goods.length;i++)
{
totalSales=totalSales+goods[i].totalSalesByYear();
}
return totalSales;
}
}

public class HappySales
{
public static void main(String[] args)
{
ComputerTotalSales[] goods=new ComputerTotalSales[10];
for(int i=0;i<goods.length;i++)
{
if(i%3==0)
{
goods[i]=new Television();
}
else if(i%3==1)
{
goods[i]=new Computer();
}
else if(i%3==2)
{
goods[i]=new Mobile();
}
Shop shop=new Shop(goods);
System.out.println("商店年度销售额:"+shop.giveTotalSales());
}
}
}

[此贴子已经被作者于2007-6-9 11:16:26编辑过]

搜索更多相关的解决方案: Exception  thread  java  lang  

----------------解决方案--------------------------------------------------------

public class HappySales
{
public static void main(String[] args)
{
ComputerTotalSales[] goods=new ComputerTotalSales[10];
for(int i=0;i<goods.length;i++)
{
if(i%3==0)
{
goods[i]=new Television();
}
else if(i%3==1)
{
goods[i]=new Computer();
}
else if(i%3==2)
{
goods[i]=new Mobile();
}
Shop shop=new Shop(goods); //这两句应该放在for循环外面.
System.out.println("商店年度销售额:"+shop.giveTotalSales());
}
}
}


----------------解决方案--------------------------------------------------------

谢谢!


----------------解决方案--------------------------------------------------------
  相关解决方案