5 Replies - 1141 Views - Last Post: 15 January 2010 - 06:31 PM Rate Topic: -----

#1 Flea333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-December 09

Exception in thread "main" java.lang.NullPointerException

Post icon  Posted 15 January 2010 - 10:21 AM

Hi, I am working on an inventory program and have received an error when I run my program. The error is "Exception in thread "main" java.lang.NullPointerException" and the lines that the errors are on are at dogsinventory.Inventory.bubbleSort(Inventory.java:122) and at dogsinventory.Inventory.main(Inventory.java:88). Here is my code:
 for (int x = 0; x < count - j; x++)
			{
				String name1 = arr[x].getDogName();
				String name2 = arr[x+1].getDogName();
				if ( name1.compareTo(name2) > 0)
				{
					tmp = arr[x];
					arr[x] = arr[x + 1];
					arr[x + 1] = tmp;
					swapped = true;
				}//end if
			}//end for 


and this is the second error location.

 //array
		Dogs[] dArray = new Dogs[100];
		dArray[0] = d1;
		dArray[1] = d2;
		dArray[2] = d3;
		dArray[3] = d4;
		dArray[4] = d5;
		dArray[5] = d6;
		dArray[6] = d7;
		dArray[7] = d8;

		bubbleSort(dArray, count);

		//print above
		for(int x = 0; x < count; x++)
		{
			System.out.println( dArray[x].toString() );
		} 


I hope this is enough information and in the right format. This is my first post. I apologize if I did something wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 902
  • View blog
  • Posts: 3,163
  • Joined: 12-May 09

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 January 2010 - 10:41 AM

What is the value of count before you call bubbleSort?
Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 January 2010 - 10:45 AM

Wheres is the initialization of d1~d8, arr and count? Post your whole class.

You probably accessing a non-initialized variable of the array.
Was This Post Helpful? 0
  • +
  • -

#4 Flea333  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-December 09

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 January 2010 - 10:51 AM

View Postxclite, on 15 Jan, 2010 - 09:41 AM, said:

What is the value of count before you call bubbleSort?



Currently it is 100. I changed it to the exact number of the array and get the same error.

View Postanonymouscodder, on 15 Jan, 2010 - 09:45 AM, said:

Wheres is the initialization of d1~d8, arr and count? Post your whole class.

You probably accessing a non-initialized variable of the array.


 public class Inventory
{
	public static void main(String[] args)
	{
		int count = 0;

		//Creating products for inventory
		Dogs d1 = new Dogs();
		d1.setDogName("Pomeranian");
		d1.setDogPrice(350.00);
		d1.setDogCount(3);
		d1.setDogItemNumber(1);
		d1.setDogColor("Cream");
		count++;

		Dogs d2 = new Dogs();
		d2.setDogName("Yorkshire Terrior");
		d2.setDogPrice(650.00);
		d2.setDogCount(5);
		d2.setDogItemNumber(2);
		d2.setDogColor("Brindle");
		count++;

		Dogs d3 = new Dogs();
		d3.setDogName("Schnauzer");
		d3.setDogPrice(350.00);
		d3.setDogCount(10);
		d3.setDogItemNumber(3);
		d3.setDogColor("Black");
		count++;

		Dogs d4 = new Dogs();
		d4.setDogName( "Shih Tzu" );
		d4.setDogPrice(450.00);
		d4.setDogCount(4);
		d4.setDogItemNumber(4);
		d4.setDogColor("Black and White");
		count++;

		Dogs d5 = new Dogs();
		d5.setDogName( "Maltese" );
		d5.setDogPrice(550.00);
		d5.setDogCount(7);
		d5.setDogItemNumber(5);
		d5.setDogColor("White");
		count++;

		Dogs d6 = new Dogs();
		d6.setDogName( "German Shephard" );
		d6.setDogPrice(250.00);
		d6.setDogCount(1);
		d6.setDogItemNumber(6);
		d6.setDogColor("Black and Tan");
		count++;

		Dogs d7 = new Dogs();
		d7.setDogName( "Pomeranian" );
		d7.setDogPrice(100.00);
		d7.setDogCount(1);
		d7.setDogItemNumber(7);
		d7.setDogColor("Dark Brown");
		count++;

		Dogs d8 = new Dogs();
		d8.setDogName( "Yorkshire Terrior" );
		d8.setDogPrice(100.00);
		d8.setDogCount(1);
		d8.setDogItemNumber(8);
		d8.setDogColor("Brindle");
		count++;

		//array
		Dogs[] dArray = new Dogs[100];
		dArray[0] = d1;
		dArray[1] = d2;
		dArray[2] = d3;
		dArray[3] = d4;
		dArray[4] = d5;
		dArray[5] = d6;
		dArray[6] = d7;
		dArray[7] = d8;

		bubbleSort(dArray, count);

		//print above
		for(int x = 0; x < count; x++)
		{
			System.out.println( dArray[x].toString() );
		}

		System.out.printf("**Total Value of all Dogs: $%.2f \n",  calculateTotalValueOfAllDogs( dArray, count));
	}
	public static double calculateTotalValueOfAllDogs( Dogs d[], int count)
	{
		double total = 0.0;
		for(int x = 0; x< count; x++)
		{
			total = total + d[x].getDogInventoryValue();
		}

		return total;
	}

	public static void bubbleSort(Dogs[] arr, int count)
	{
		boolean swapped = true;
		int j = 0;
		Dogs tmp;
		while (swapped)
		{
			swapped = false;
			j++;
			for (int x = 0; x < count - j; x++)
			{
				String name1 = arr[x].getDogName();
				String name2 = arr[x+1].getDogName();
				if ( name1.compareTo(name2) > 0)
				{
					tmp = arr[x];
					arr[x] = arr[x + 1];
					arr[x + 1] = tmp;
					swapped = true;
				}//end if
			}//end for
		}//end while
	}//end bubblesort
}//end class 

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2698
  • View blog
  • Posts: 11,363
  • Joined: 20-September 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 January 2010 - 04:37 PM

Quote

I changed it to the exact number of the array and get the same error.


You probably didn't do that accurately. That's the reason for your error
Was This Post Helpful? 0
  • +
  • -

#6 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 January 2010 - 06:31 PM

Yes, please post all of your relevant classes... Chances are, hes right and you tried to access part of your array that isn't there...

Thanks for helping yourself!

- Zach
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1