Java问题,在get() 的方法中,本应该有返回值的itemlist[i]的返回值变为null,出现nullpointer的错误。
classitem{privateStringitemcode;privateStringdescription;privatedoubleprice;privatein...
class item
{
private String itemcode;
private String description;
private double price;
private int stock;
Scanner scan = new Scanner(System.in);
public item(String code, String descri, double price1, int stock1)
{
itemcode = code;
description = descri;
price = price1;
stock = stock1;
}
public String getcode()
{
return itemcode;
}
}
public class PartB
{
private item itemlist[];
Scanner scan = new Scanner(System.in);
public item get(String itemcode)
{
for (int i=0; i < itemcode.length() ; i++)
if (itemlist[i].getcode().compareTo(itemcode)==0)
return itemlist[i];
return null;
}
public void newitemlist()
{
itemlist[0]= new item("GR1001","18 pack Kleenex Toilet Rolls",6.00,25);
itemlist[1]= new item("TF2000","Heinz Baked Beans 600g",0.90,85);
itemlist[2]= new item("GR3333","Morning Fresh 1 Ltr",3.25,44);
itemlist[3]= new item("TF1005","John West Tuna 450g",2.75,33);
itemlist[4]= new item("PF2222","Vetta Spaghetti No 9.500g",1.50,60);
}
public double order()
{
double total = 0;
int temp = 0;
do
{
System.out.println("Please enter the item code");
String itemcode = scan.nextLine();
item Ac = get(itemcode);
if (Ac == null)
{
System.out.println("No such item.");
temp = 1;
}
else
{
System.out.println("Please enter the amount ");
int amount = scan.nextInt();
do
{
Ac.withdraw(amount);
}
while(false);
total += amount * Ac.getprice() ;
}
}
while(temp == 1);
return total;
}
public void menu2()
{
int temp = 2;
do
{
System.out.println("1. Accept Supplier Delivery");
System.out.println("2. Update Item Price");
System.out.println("3. View Summary Report");
int choice = scan.nextInt();
switch (choice)
{
case 1:
order();
temp = 1;
break;
case 2:
updateprice();
temp = 1;
break;
case 3:
temp = 0;
break;
default : System.out.println("Error: invalid status");
}
}
while(temp == 1);
}
public class PartB
{
private item itemlist[];
Scanner scan = new Scanner(System.in);
public item get(String itemcode)
{
for (int i=0; i < itemcode.length() ; i++)
if (itemlist[i].getcode().compareTo(itemcode)==0)
return itemlist[i];
return null;
} 展开
{
private String itemcode;
private String description;
private double price;
private int stock;
Scanner scan = new Scanner(System.in);
public item(String code, String descri, double price1, int stock1)
{
itemcode = code;
description = descri;
price = price1;
stock = stock1;
}
public String getcode()
{
return itemcode;
}
}
public class PartB
{
private item itemlist[];
Scanner scan = new Scanner(System.in);
public item get(String itemcode)
{
for (int i=0; i < itemcode.length() ; i++)
if (itemlist[i].getcode().compareTo(itemcode)==0)
return itemlist[i];
return null;
}
public void newitemlist()
{
itemlist[0]= new item("GR1001","18 pack Kleenex Toilet Rolls",6.00,25);
itemlist[1]= new item("TF2000","Heinz Baked Beans 600g",0.90,85);
itemlist[2]= new item("GR3333","Morning Fresh 1 Ltr",3.25,44);
itemlist[3]= new item("TF1005","John West Tuna 450g",2.75,33);
itemlist[4]= new item("PF2222","Vetta Spaghetti No 9.500g",1.50,60);
}
public double order()
{
double total = 0;
int temp = 0;
do
{
System.out.println("Please enter the item code");
String itemcode = scan.nextLine();
item Ac = get(itemcode);
if (Ac == null)
{
System.out.println("No such item.");
temp = 1;
}
else
{
System.out.println("Please enter the amount ");
int amount = scan.nextInt();
do
{
Ac.withdraw(amount);
}
while(false);
total += amount * Ac.getprice() ;
}
}
while(temp == 1);
return total;
}
public void menu2()
{
int temp = 2;
do
{
System.out.println("1. Accept Supplier Delivery");
System.out.println("2. Update Item Price");
System.out.println("3. View Summary Report");
int choice = scan.nextInt();
switch (choice)
{
case 1:
order();
temp = 1;
break;
case 2:
updateprice();
temp = 1;
break;
case 3:
temp = 0;
break;
default : System.out.println("Error: invalid status");
}
}
while(temp == 1);
}
public class PartB
{
private item itemlist[];
Scanner scan = new Scanner(System.in);
public item get(String itemcode)
{
for (int i=0; i < itemcode.length() ; i++)
if (itemlist[i].getcode().compareTo(itemcode)==0)
return itemlist[i];
return null;
} 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询