2 Replies - 671 Views - Last Post: 09 April 2009 - 02:23 PM Rate Topic: -----

#1 babygina37   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 09

nullpointerexception

Post icon  Posted 09 April 2009 - 09:15 AM

The problems are that I am getting null exception and cannot figure out why. Exception in thread "main" java.lang.NullPointerException
at DVD.get(DVD.java:116)
at DVD.value(DVD.java:143)



		import java.awt.Component;
import java.util.ArrayList;



public class DVD extends Inventory{
	



	
	// This sets the array for the DVD collection

	private String name;
	private int itemNumber;
	private int stockQuantity;
	private double price;


	public DVD(String name, int itemNumber, double d, double price) {

		this.name = name; // Using this. allows for the return of one dvd
							// instead of all
		this.itemNumber = itemNumber;
		this.stockQuantity = (int) d;
		this.price = price;
	}

	@Override
	public String toString() {

		return "DVD Title: " + this.name + "\n" + "Item Number: "
				+ this.itemNumber + "\n" + "Stock Quantity: "
				+ this.stockQuantity + "\n" + "Price: " + this.price + "\n";
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getItemNumber() {
		return this.itemNumber;
	}

	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}

	public int getStockQuantity() {
		return this.stockQuantity;
	}

	public void setStockQuantity(int stockQuantity) {
		this.stockQuantity = stockQuantity;
	}

	public double getPrice() {
		return this.price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	// calculate inventory value
	public double value1() {
		return price * stockQuantity;
	} // end method value

	// Prints the value of inventory list.
	public void printvalue() {
		System.out.println("Inventory value...\n");
	}

	public String getDvdTitle() {
		String title = name;
		// TODO Auto-generated method stub
		return title;
	}

	public int getDvdStock() {
		// TODO Auto-generated method stub
		return stockQuantity;
	}

	public double getDvdPrice() {
		// TODO Auto-generated method stub
		return price;
	}

	public int getDvdItem() {
		// TODO Auto-generated method stub
		return itemNumber;
	}

	/**
	 * @param dvdlist the dvdlist to set
	 */
	private ArrayList<ExtendedDVD> dvdlist;

	public DVD() {
		dvdlist = new ArrayList<ExtendedDVD>();
	}

	// adding and getting items

	public void add(ExtendedDVD p) {
		dvdlist.add(p);
	}

	public ExtendedDVD get(int i) {
		return dvdlist.get(i);
	}

	public int size() {
		return dvdlist.size();
	}

	public void inventorysort() {
		// bubble sort
		int n = dvdlist.size();
		for (int search = 1; search < n; search++) {
			for (int i = 0; i < n-search; i++) {
				if (((Component) dvdlist.get(i)).getName().compareToIgnoreCase(((Component) dvdlist.get(i+1)).getName()) > 0) {
					// swap
					ExtendedDVD temp = dvdlist.get(i);
					dvdlist.set(i,dvdlist.get(i+1));
					dvdlist.set(i+1,temp);
				}
			}
		}
	}
	
//	value
	public double value() {
		double total = 0.0;
		{
			int i = 0;
			total += ((DVD) get(i)).value();
		
		return total;
	}
}

// end class DVD


   class myDVD extends InventoryGUI {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;}



Is This A Good Question/Topic? 0
  • +

Replies To: nullpointerexception

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: nullpointerexception

Posted 09 April 2009 - 09:24 AM

First and foremost welcome to Dream.In.Code babygina37, glad you decided to join us.

When posting programming questions please post them in the proper forum (forums are on the left of the screen under Programming Help) so I'll go a head and move this to the Java forum for you :)
Was This Post Helpful? 0
  • +
  • -

#3 Skyd   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 27
  • Joined: 09-April 09

Re: nullpointerexception

Posted 09 April 2009 - 02:23 PM

In the following code:

	public double value() {
		double total = 0.0;
		{
			int i = 0;
			total += ((DVD) get(i)).value();
		
		return total;
	}



There is an extra { sign on the 3rd line. Also, it is not clear what you are trying to do here. Are you trying to run a for loop with index i? or is this a recursive function?

The nullpointerexception usually occurs when you try to call dvdlist.get(i) without dvdlist having been instantiated. You have two constructors for DVD. One of them (the first one) does not create a dvdlist instance, and may be the cause of the problem. I see that you have 2 DVD constructors because you want one to represent a single dvd and one to represent a list of dvd. These should actually be seperated into two separate classes with different names because they represent different objects. Right now you are treating both objects as the same, and so when you do a get(), you might be running get on a single DVD object, which doesn't have the dvdlist instance created
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1