1

As I mentioned above, the constructor for CdwArtist is different. You have defined a constructor with the artist name parameter, so you must construct the object with that information. See comments I have added in your code below:

// begin display method 
    System.out.print("Enter up to 99 CD Names or STOP to Exit: "); 
    String nameInput = input.nextLine(); //read cd name 
    
    maxlength ++; 
    
     for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
     {// begin main While
      [B]cds[i] = new CdwArtist();   // This class does not have an empty constructor like this anymore. You changed it.[/B]
      cds[i].setName(nameInput);
      
      System.out.print("Enter CD Artist Name: "); // prompt for artist name
      CdwArtist artist = new CdwArtist(input.nextLine());  [B]// here you create with the correct constructor, but you don't use the object anywhere else.[/B]
                  
      System.out.print("Enter Price of this CD: "); // prompt for price
      cds[i].setPrice(input.nextFloat()); // price input from user. 
      while (cds[i].getPrice()<= 0) 
         {// begin while 
           System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
            cds[i].setPrice(input.nextFloat()); // cd price loop from user.
          } // End while

I would recommend going back to your original class definition:

import java.util.*;

public class CdwArtist extends Compactdisk
{
 private String artist; // artist performing on cd
 
 // Artist constructor
 public CdwAartist()
 {
 artist = "";
 }
 
 // set value
 public void setCdArtist(String cdArtist)
     { 
     artist = cdArtist;
    }
        
    // return value
    public String getCdArtist()
    {
    return (artist);
    }
    

} //End Class

Then your code is simply this

... <snipped top part>...
    // begin display method 
    System.out.print("Enter up to 99 CD Names or STOP to Exit: "); 
    String nameInput = input.nextLine(); //read cd name 
    
    maxlength ++; 
    
     for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
     {// begin main While
      [B]cds[i] = new CdwArtist(); [/B]
      cds[i].setName(nameInput);
      
      System.out.print("Enter CD Artist Name: "); // prompt for artist name
      [B]cds[i].setCdArtist(input.nextLine());[/B]
                  
      System.out.print("Enter Price of this CD: "); // prompt for price
      cds[i].setPrice(input.nextFloat()); // price input from user. 
      while (cds[i].getPrice()<= 0) 
         {// begin while 
           System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
            cds[i].setPrice(input.nextFloat()); // cd price loop from user.
          } // End while
Votes + Comments
never fails to be helpful
0

Ok. Let me play with this awhile. I only halfway understand that explaination, but with some of the stuff I have learned over the past few days, and the links from Cerberus I think I should be able to make sense of it.

Just FYI, why does it point to the "new" in that instance? Because new is trying to create a defalut that is not defined?

0

Yes, it is trying to find a constructor that does not exist. The code the TheGathering posted defines an empty constructor (meaning no parameters) that can be called with "new CdwArtist()".

0

I like the way you put those notes out to the side of the code lines, it helps me follow along.

I think I have this part, I am going to read those links again just to reinforce it. The changes have brought out an issue with the toString, program erros and never makes it to the list, so I am going to see if I can work that out. I have learned so much this week my head is spinning. After I get this block working the way I want, I think I am going to take a few days just to review what you guys have shown me, let it all sink in and make sure I understand it.
This language looks like it would be neat if I can just get rolling with it. There is an advanced java class I am looking at down the road, but I am nowhere near ready for that yet.

0

Hey guys. Be prepared to be shocked, but I could NOT figure out the problem with the exit sort. It is so aggravating to me. I hate feeling stupid, and I hate having to come out here day after day and feel like I am having Java spoon fed to me.
You all are great, I just do not want to feel like I am taking advantage of your skill.
This HAS to be something simple and dumb, I just do not see it. I am 99% sure it has to do with the sort, because once I enter "stop" and exit the main loop I get the error, and the only thing that happens at that point is this sort.
Everything compiles, everything runs fine, until exit, then I get all these errors in DOS.
Exception in thread "main" java.lang.NullPointException at java.util.Arrays.mergeSort(Arrays.java)
at java.util.Arrays.sort(Arrays.java)
at Inventory.sort(Inventory.java)
at Inventory.main(Inventory.java)
The more I think about it, the more I want to say it is with the array, because the sort should be inherited as is into the new array, but the array seems to be working ... so I do not know.
Here is the code in the new Inventory as it stands

import java.util.*;

public class Inventory 
{// begin class Inventory

 public static int maxlength = 0;
  public static CdwArtist[] sort(CdwArtist[] cds)
   { 
	Arrays.sort(cds, 0, maxlength); 
	return cds;
	}
	
 public static String toString(CdwArtist[] cds)
  { 
  	String toSend = "\n\n"; 
	
	for(int i = 0; i < maxlength; i ++)
	toSend = toSend + cds[i].getName() + "\n";
	return toSend;
  } 
  
  public static void main(String[] args)
  {//begin method main
  
   // create cd Array 
	CdwArtist[] cds = new CdwArtist[100];
	
 
	float totalValue = 0; 
	
	Scanner input = new Scanner(System.in); // create scanner 
	
	// begin display method 
	System.out.print("Enter up to 99 CD Names or STOP to Exit: "); 
	String nameInput = input.nextLine(); //read cd name 
	
	maxlength ++; 
	
	 for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
	 {// begin main While
	  cds[i] = new CdwArtist();
	  cds[i].setName(nameInput);
	  
	  System.out.print("Enter CD Artist Name: "); // prompt for artist name
	  CdwArtist artist = new CdwArtist(input.nextLine()); 
	  	  	  
	  System.out.print("Enter Price of this CD: "); // prompt for price
	  cds[i].setPrice(input.nextFloat()); // price input from user. 
	  while (cds[i].getPrice()<= 0) 
	 	{// begin while 
	   	System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
			cds[i].setPrice(input.nextFloat()); // cd price loop from user.
	  	} // End while 
	  
	  System.out.print("Enter CD Item Number: "); // prompt for cd item number
	  cds[i].setItemno(input.nextInt()); // cds item number input from user 
	  
	  System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock 
	  cds[i].setNstock(input.nextInt()); // cds in stock input from user
	  
	  System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name 
	  System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
	  System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
	  System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value 
	 
	  if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
	  System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
	  
	  System.out.print("Enter up to 99 CD Names or STOP to Exit: "); 
	  input=new Scanner(System.in); // internal loop prompt 
	  nameInput = input.nextLine(); //name input from user 
	  
	  maxlength ++; 
	  } // End main While
	  
	  	System.out.println(toString(sort(cds))); 
		System.out.print("Ending Program."); 

   }// end method main 
} // end class Payroll

Any clue would be welcomed, I obviously have none.

0

Sorry about the poor attitude in that last post, I am trying not to get frustrated, I know this is a learning process, I just have to stick with it.

I have decided that the problem lies somewhere in the initialization of the array. It logically cannot be the sort, the inheritance properties of the sub class is there to serve that very purpose. Right?

This is the endless circle I have been going in for 3 hours now! Array .... find nothing .... must be Sort .... find nothing ... must be Array!! :o)

0

Looks like I am talking to myself again! :o) At least I never get bored with the company. :o)
The more I look at it, I think the problem could be with the CdwArtist class. Look at this:

import java.util.*;

public class CdwArtist extends Compactdisk
{
	
	// CdwArtist class adds one field 
  public String artist; // artist performing on cd
 
 // Artist constructor
  public CdwArtist()
  {
  artist = "";
  }
	 	  
    public CdwArtist(String in)
    {
    artist=in;
    }
 
    // get value
    public void setArtist(String in)
    {
    artist=in;
    }
 
    // return value
    public String getArtist()
    {
    return (artist);
    }
	
} //End Class

That does not look right to me. If I take out the CdwArtist string then the application fails when I enter the name of the cd. Confusing to me.
The set field controls artist (like it should right?) .... so I think I have some wires crossed somewhere. A set of non bloodshot eyes would sure be helpful. :o)

1

You have 2 problems in the Inventory class:
1.) You're updating your counter 2x on the first iteration.
2.) Your for loop in the main is starting with 1, but the first index on the sort/display is starting at 0, meaning that you are trying to work with the first value [0] which isn't set in your main loop.

To fix 1.):
Remove the line that says: //***remove this line right here***//

public static void main(String[] args)
  {//begin method main
  
   // create cd Array 
	CdwArtist[] cds = new CdwArtist[100];
	
 
	float totalValue = 0; 
	
	Scanner input = new Scanner(System.in); // create scanner 
	
	// begin display method 
	System.out.print("Enter up to 99 CD Names or STOP to Exit: "); 
	String nameInput = input.nextLine(); //read cd name 
	
	maxlength ++; //***remove this line right here***//

To fix 2.) this you have 2 options:

1).
Update the for loop in main from

for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)

to

for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)

OR

change all instances of

cds[i]

to

cds[i-1]

I would prefer you do option one of the 2.) fix, but it's your choice.

Votes + Comments
One of the best helpers out here
0

I knew there was an issue with the counter, I had just never figured it out. My loop would always end on the last element of the array. I had played with the maxlenght stuff for awhile (I thought I even did what you suggessted here) but it did not change anything and I had bigger fish to fry so I moved on.
I knew it was something simple, but I am just so inexperienced in arrays that I would have never found that. I so much appreciate it.
I am going to close this issue out, and since it is so early I am going to open one more thing I have been trying on the side without success.
I would love to get that done and have a weekend just to read.
You guys are awesome.

0

Just an FYI for my future understanding, can someone take the time and give me a quick lesson on the way CdwArtist is set up.
public CdwArtist(String in)
{
artist=in;
}

// get value
public void setArtist(String in)
{
artist=in;
}
It looks to my untrained eye as if both are doing the same thing.

0

They are doing the same thing. However, one is a constructor and the other is a method. Constructors are used when the object is created. Methods are used after the object is created. If you wanted to create a CdwArtist object with the the artist name already set, you would use the constructor. If you did not know the artist name at the time of creation, you can use the empty constructor to create the object and then call the method to set the artist name later. Multiple constructors allow for flexibility in how an object is initialized upon creation.

0

i have problem about EasyIn class and i need to install it or can i creat it
can you help me?
iam beginner in java

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.