- Joined
- Oct 29, 2009
- Messages
- 1
- Reaction score
- 0
Hi ppl. I've tried to remove the 'java.lang.NullPointerException' error for hours but to no avail. Would anyone please point out what causes the problem in my code? Here it goes...
This is for MyShop.java:
import java.util.*;
public class MyShop {
private String shopName = "ITGrocers"; //name of the shop
int capacity = 2; //maximum number of items that the shop can sell
Item[] listOfItems; //list of items in the shop
int numItems = 0; //number of items in the shop
public String getShopName() {
return shopName;
}
public void setShopName(String name) {
shopName = name;
}
public int addItem(String itemName, double itemPrice) {
boolean itemInShop = false;
if (numItems != 0) {
for(int i = 0; i < numItems; i++) {
if (itemName.equalsIgnoreCase(listOfItems[i…
itemInShop = true;
}
}
if (itemInShop)
return 0;
if (numItems == capacity)
return 1;
else {
listOfItems[numItems].setItemName(itemNa…
listOfItems[numItems].setItemPrice(itemP…
numItems += 1;
return 2;
}
}
public boolean updateItemPrice(String itemName, double newItemPrice) {
boolean itemInShop = false;
for(int i = 0; i < numItems; i++) {
if (listOfItems.getItemName().equalsIgno… {
listOfItems.setItemName(itemName);
listOfItems.setItemPrice(newItemPrice…
itemInShop = true;
}
}
if (itemInShop)
return true;
else
return false;
}
public void display() {
System.out.println("Shop name: " + shopName + "\nMaximum capacity: " + capacity);
if (numItems == 0)
System.out.println("No item added yet!\n");
else {
System.out.println("Total: " + numItems + " item(s)");
for (int i = 0; i < numItems; i++)
System.out.println(listOfItems.toStri… + "\n");
}
}
public static void main (String[] args) {
MyShop obj = new MyShop();
Item[] listOfItems = new Item[obj.capacity];
for (int i = 0; i < obj.capacity; i++) {
listOfItems = new Item();
}
boolean loopChoice = true;
int choice;
double itemPrice;
String itemName;
Scanner s = new Scanner(System.in);
while (loopChoice) {
loopChoice = false;
System.out.println("--------------------… to Shop Management Program--------------------");
System.out.println("1.Add Item\n2.Update Item Price\n3.Display all items\n4.Exit");
System.out.println("Enter choice:");
choice = s.nextInt();
s.nextLine();
switch (choice) {
case 1: {
System.out.print("Enter item name:");
itemName = s.nextLine();
System.out.print("Enter item price:");
itemPrice = s.nextDouble();
if (obj.addItem(itemName,itemPrice) == 0)
System.out.println("This item is already in the list.");
else if (obj.addItem(itemName,itemPrice) == 1)
System.out.println("Adding failed. The shop has already reached its maximum capacity.");
else
System.out.print("New item added successfully.");
loopChoice = true;
break;
}
case 2: {
System.out.println("Enter item name:");
itemName = s.nextLine();
System.out.println("Enter item new price:");
itemPrice = s.nextDouble();
if (obj.updateItemPrice(itemName,itemPrice)…
System.out.println("Item updated successfully.");
else
System.out.println("The item does not exist.");
loopChoice = true;
break;
}
case 3: {
obj.display();
loopChoice = true;
break;
}
case 4: {
System.out.print("Thank you for using the program. Goodbye!");
break;
}
default: {
System.out.println("Invalid choice!");
loopChoice = true;
}
}
}
}
}
This is for Item.java:
class Item {
private String itemName;
private double itemPrice;
public String getItemName() {
return itemName;
}
public void setItemName(String n) {
itemName = n;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double p) {
itemPrice = p;
}
public String toString() {
return itemName + "\t$" + itemPrice;
}
}
The error lies at line 36 and 94 which are as follows:
36: listOfItems[numItems].setItemName(itemNa…
94: if (obj.addItem(itemName,itemPrice) == 0)
Detailed explanations would be helpful. U may rewrite the prog cos i'm really a noob at this. hv to hand in dis program latest by 10am
This is for MyShop.java:
import java.util.*;
public class MyShop {
private String shopName = "ITGrocers"; //name of the shop
int capacity = 2; //maximum number of items that the shop can sell
Item[] listOfItems; //list of items in the shop
int numItems = 0; //number of items in the shop
public String getShopName() {
return shopName;
}
public void setShopName(String name) {
shopName = name;
}
public int addItem(String itemName, double itemPrice) {
boolean itemInShop = false;
if (numItems != 0) {
for(int i = 0; i < numItems; i++) {
if (itemName.equalsIgnoreCase(listOfItems[i…
itemInShop = true;
}
}
if (itemInShop)
return 0;
if (numItems == capacity)
return 1;
else {
listOfItems[numItems].setItemName(itemNa…
listOfItems[numItems].setItemPrice(itemP…
numItems += 1;
return 2;
}
}
public boolean updateItemPrice(String itemName, double newItemPrice) {
boolean itemInShop = false;
for(int i = 0; i < numItems; i++) {
if (listOfItems.getItemName().equalsIgno… {
listOfItems.setItemName(itemName);
listOfItems.setItemPrice(newItemPrice…
itemInShop = true;
}
}
if (itemInShop)
return true;
else
return false;
}
public void display() {
System.out.println("Shop name: " + shopName + "\nMaximum capacity: " + capacity);
if (numItems == 0)
System.out.println("No item added yet!\n");
else {
System.out.println("Total: " + numItems + " item(s)");
for (int i = 0; i < numItems; i++)
System.out.println(listOfItems.toStri… + "\n");
}
}
public static void main (String[] args) {
MyShop obj = new MyShop();
Item[] listOfItems = new Item[obj.capacity];
for (int i = 0; i < obj.capacity; i++) {
listOfItems = new Item();
}
boolean loopChoice = true;
int choice;
double itemPrice;
String itemName;
Scanner s = new Scanner(System.in);
while (loopChoice) {
loopChoice = false;
System.out.println("--------------------… to Shop Management Program--------------------");
System.out.println("1.Add Item\n2.Update Item Price\n3.Display all items\n4.Exit");
System.out.println("Enter choice:");
choice = s.nextInt();
s.nextLine();
switch (choice) {
case 1: {
System.out.print("Enter item name:");
itemName = s.nextLine();
System.out.print("Enter item price:");
itemPrice = s.nextDouble();
if (obj.addItem(itemName,itemPrice) == 0)
System.out.println("This item is already in the list.");
else if (obj.addItem(itemName,itemPrice) == 1)
System.out.println("Adding failed. The shop has already reached its maximum capacity.");
else
System.out.print("New item added successfully.");
loopChoice = true;
break;
}
case 2: {
System.out.println("Enter item name:");
itemName = s.nextLine();
System.out.println("Enter item new price:");
itemPrice = s.nextDouble();
if (obj.updateItemPrice(itemName,itemPrice)…
System.out.println("Item updated successfully.");
else
System.out.println("The item does not exist.");
loopChoice = true;
break;
}
case 3: {
obj.display();
loopChoice = true;
break;
}
case 4: {
System.out.print("Thank you for using the program. Goodbye!");
break;
}
default: {
System.out.println("Invalid choice!");
loopChoice = true;
}
}
}
}
}
This is for Item.java:
class Item {
private String itemName;
private double itemPrice;
public String getItemName() {
return itemName;
}
public void setItemName(String n) {
itemName = n;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double p) {
itemPrice = p;
}
public String toString() {
return itemName + "\t$" + itemPrice;
}
}
The error lies at line 36 and 94 which are as follows:
36: listOfItems[numItems].setItemName(itemNa…
94: if (obj.addItem(itemName,itemPrice) == 0)
Detailed explanations would be helpful. U may rewrite the prog cos i'm really a noob at this. hv to hand in dis program latest by 10am