0

Hello everyone,

I am having an issue with my code, null pointer exception error. It says lines 24 and 75 are the issue, but I am not exactly sure what the problem is. Can anyone help with some suggestions as to what I might be doing wrong ? Much appreciated. I am supposed to have an array sorted by product name. Compiles, but does not run due to error. Thanks in advance.

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;

public class InventoryPart3rd
{

	public static Monitors[] sortArray(Monitors[] monitors)
			{
			String[] names = new String[monitors.length];
			Monitors[] tempProduct = new Monitors [monitors.length];
				for (int i = 0; i < monitors.length; i++)
					{
					names[i] = monitors[i].getProductName();
					}

			Arrays.sort(names);

				for (int i = 0; i < names.length; i++)
					{
					for (int j = 0; j < names.length; j++)
						{
							if (monitors[i].getProductName().equalsIgnoreCase(names[j]) )
								{
								tempProduct[j] = monitors[i];
								break;
								}
						}
					}

				return tempProduct;

			}

	public static double  calculateInventoryValue(Monitors[] monitors)
			{

			double total = 0;

			for (int i = 0; i < monitors.length; i++)
				{
				total += monitors[i].calculateValue();
				}

				return total;

			}

	public static void main ( String args[] )
			{

			NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

			Monitors[] monitors = new Monitors[5];

			Monitors example1 = new Monitors(8701, "Dell 22'' LCD Flatpanel", 6, 156.99);
			Monitors example2 = new Monitors(9049, "HP 19'' CRT", 2, 120.99);
			Monitors example3 = new Monitors(3952, "LG 21'' LED Superflat", 9, 225.99);
			Store example4 = new Store(8946, "Acer 19'' Plasma Flatpanel", 3, 124.79, "Walmart");
			Store example5 = new Store(9406, "Samsung 20'' LCD Flatpanel", 8, 191.99, "Best Buy");

			monitors[0] = example1;
			monitors[1] = example2;
			monitors[2] = example3;
			monitors[3] = example4;
			monitors[4] = example5;

			monitors = sortArray(monitors);

			double inventoryValue = calculateInventoryValue(monitors);

			System.out.println( "Items in inventory: 		." + "\n");
			for (int i = 0; i < monitors.length; i++)
				{
				System.out.println(monitors[i]);
				System.out.println();
				}

				System.out.println( "Total value of inventory: 		." +nf.format(inventoryValue));

			}

}
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Arrays;


class Monitors
	{
	private int idNumber;
	private String productName;
	private int productCount;
	private double productPrice;
	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		public Monitors (int number, String name, int count, double price)
			{
			this.idNumber = idNumber;
			this.productName = productName;
			this.productCount = productCount;
			this.productPrice = productPrice;

			}


public double calculateValue()
		{
		    return productCount * productPrice;
     	}

public void setIdNumber (int idNumber)
		{
			this.idNumber = idNumber;
		}

public double getIdNumber()
		{
			return idNumber;
		}

public void setProductName (String productName)
		{
			this.productName = productName;
		}

public String getProductName()
		{
			return productName;
		}

public void setProductCount (int productCount)
		{
			this.productCount = productCount;
		}

public double getProductCount()
		{
			return productCount;
		}

public void setProductPrice (double productPrice)
		{
			this.productPrice = productPrice;
		}

public double getProductPrice()
		{
			return productPrice;
		}


public String toString () {
			return "Monitor Name:" + "\t" + productName + "\n" +
				   "Item Number:" + "\t" + idNumber + "\n" +
			       "Units:" + "\t \t" + productCount + "\n" +
			       "Price:" + "\t \t" + nf.format(productPrice) + "\n" +
			       "Item Total:" + "\t" + nf.format(calculateValue());
			       }

}

class Store extends Monitors{

	//class variables
	private String storeName;
	private double restockFee;
	private static final double restockFeePercentage = .05;

	//class constructor
	public Store (int idNumber, String productName, int productCount, double productPrice, String storeName)
		{
		super (idNumber, productName, productCount, productPrice);
		this.storeName = storeName;

		restockFee = productPrice * restockFeePercentage;
		}

	public String getStoreName()
		{
		return storeName;
		}

	public void setSerialNumber(String storeName)
		{
		this.storeName = storeName;
		}

	public double getRestockFee()
		{
		return restockFee;
		}

	public String toString ()
				{
				return super.toString() + "\n" +
				       "Serial Number: " + "\t" + storeName + "\n" +
				       "Restock Fee:" + "\t" + nf.format(getRestockFee());
			    }

}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by zeroliken
0

if you check the values of name at line 15 you'll see that they're all null
@ the Monitors class constructor try to use the parameters passed instead of assigning to itself

also your exporting a non public type, try to make class Monitors public if it's not in the InventoryPart3rd class

Edited by zeroliken: wrong spelling

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.