I'm new to java, so please be kind if I'm not making any sense or making a rookie mistake! But I have been working for HOURS trying to figure out where I'm going wrong to no avail. I'm pretty sure I'm just missing something really simple, but hopefully someone here understands & is able to help!

I have 3 classes having to do with an invoice, Invoice, InvoiceItem & ProcessInvoice.
InvoiceItem is basically just a bunch of instance variables & getters & setters.
Invoice holds an array of InvoiceItems and a total for the invoice. It loops through the array and calculates the total, then displays it. It also is to have a set method for the array.
ProcessInvoice holds an object of Invoice. It has a private method that instantiates 3 InvoiceItems into a local array, sets their instance variables, and then sets this local array into the Invoice object via the set method. ProcessInvoice has another method that instantiates the Invoice object, calls the private method, and calls the method for calculating & displaying the total.

And I don't know if that made any sense... ANYWAY, why would you make a set method for an array?

Everything compiles fine, except ProcessInvoice, and I run into the problem when it gets to "invoice.createInvoiceItems();" where I get a "cannot find symbol" error.

public class Invoice{
	private InvoiceItem[] invoiceArray;	
	private double 	      invoiceTotal;
	
	//set invoice items array
	public void setInvoiceArray(InvoiceItem itemOne, InvoiceItem itemTwo, InvoiceItem itemThree) {
		invoiceArray 	= new InvoiceItem[3];
		invoiceArray[0] = new InvoiceItem();
		invoiceArray[1] = new InvoiceItem();
		invoiceArray[2] = new InvoiceItem();
		
		invoiceArray[0] = itemOne;
		invoiceArray[1] = itemTwo;
		invoiceArray[2] = itemThree;
	}
	
	// calculate invoice
	public void calculateInvoice(){
		for(int counter = 0; counter < invoiceArray.length; counter++) {
			invoiceTotal = invoiceTotal + invoiceArray[counter].calculateItemTotal();
		}
	}
	
	// display invoice
	public void displayInvoice(){
		for(int counter = 0; counter < invoiceArray.length; counter++) {
			invoiceArray[counter].display();
			System.out.println("Invoice " + counter + " Total: " + getInvoiceTotal());
			System.out.println();
		}
	}
	
	// get invoice total
	public double getInvoiceTotal(){
		return invoiceTotal;
	}
 }
public class ProcessInvoice {
	private Invoice invoice;
	private void createInvoiceItems() {	
		
		// instantiate 3 invoiceitems into local array
		InvoiceItem[] localArray = new InvoiceItem[3];
		localArray[0] = new InvoiceItem();
		localArray[1] = new InvoiceItem();
		localArray[2] = new InvoiceItem();
		
		// set instance variables
		localArray[0].setItemID(9865);
		localArray[0].setItemQuantity(3);
		localArray[0].setItemPrice(16.99);
		localArray[0].setItemDescription("50-pack of blank CD-R discs");
		
		localArray[1].setItemID(8425);
		localArray[1].setItemQuantity(6);
		localArray[1].setItemPrice(10.99);
		localArray[1].setItemDescription("2GB USB 2.0 Flash Drive");
		
		localArray[2].setItemID(3241);
		localArray[2].setItemQuantity(2);
		localArray[2].setItemPrice(69.99);
		localArray[2].setItemDescription("250 GB External 2.0 Portable Hard Drive");
		
		// set array into invoice
		invoice.setInvoiceArray(localArray[0], localArray[1], localArray[2]);
	}
	public void runProcess() {
		invoice = new Invoice();
		
		invoice.createInvoiceItems();
		invoice.calculateInvoice();
		invoice.displayInvoice();
	}
 }

What causes an error of "cannot find symbol"? And how do I go about fixing it?

Paste the error message. Someone else might spot it, but I see nothing, the error message will make it obvious. I think the cannot find symbol message means you tried to use a variable or method that doesn't exist.

Paste the error message. Someone else might spot it, but I see nothing, the error message will make it obvious. I think the cannot find symbol message means you tried to use a variable or method that doesn't exist.

Here's the error message I'm getting.

ProcessInvoice.java:39: cannot find symbol
symbol : method createInvoiceItems()
location: class Invoice
invoice.createInvoiceItems();

The method createInvoiceItems is declared inside the class ProcessInvoice class. But you are doing this: invoice.createInvoiceItems() The Invoice class doesn't have the createInvoiceItems method the ProcessInvoice has.

Also I don't think it can be called from outside the class ProcessInvoice if it is private.
Also I suggest that you initial the invoiceTotal to zero in order to avoid certain warnings

The method createInvoiceItems is declared inside the class ProcessInvoice class. But you are doing this: invoice.createInvoiceItems() The Invoice class doesn't have the createInvoiceItems method the ProcessInvoice has.

Also I don't think it can be called from outside the class ProcessInvoice if it is private.
Also I suggest that you initial the invoiceTotal to zero in order to avoid certain warnings

Okay, thanks!
So, if I just say createInvoiceItems() , that would accomplish what I'm trying to do?

Okay, thanks!
So, if I just say createInvoiceItems() , that would accomplish what I'm trying to do?

Have you tried it?

Hey,
I compiled your code and my compiler gave me a ton of errors. A lot of errors like the one you posted.

File: /Users/rue64ja/java/personal_projects/Invoice.java [line: 4]
Error: /Users/rue64ja/java/personal_projects/Invoice.java:4: cannot find symbol
symbol : class InvoiceItem
location: class Invoice

When ever I get an error like this it usually means that I tried to access a variable outside of it's scope. That's most likely what's going on with your code.

In your code I see you trying to create instances of a class within the class itself, that is why your getting the compilation errors. When you use the new operator you are usually creating an object (class instance) or using a class as a data type (the arrays you have). So a class has to be previously defined… it's almost like you are trying to use a word to define itself (like a pencil is a pencil?). The compiler doesn't seem to like that.

You should try creating another class that creates your objects and performs all the calculations you want on them (possibly your ProcessInvoice class?). The other classes you defined should do nothing other than define data fields, constructors, and get/set methods. That is it. Also, I think someone else mentioned that you shouldn't use the private modifier if you expect to use the the methods and data fields outside of the class, which is what you want to do here.

Oh! and check this out

#
public class Invoice{
#
private InvoiceItem[] invoiceArray;  // ? where is the InvoiceItem class?
#
private double invoiceTotal;
#

You declared the class as Invoice but when you create your array you use InvoiceItem[], that is an incorrect data type because the class InvoiceItem isn't defined anywhere in your code, and it doesn't look like it was inherited either. What you should be using as your data type is Invoice[]

So, I suggest that you remove everything from the Invoice class aside from data fields and methods associated with an Invoice object, and place it all in the ProcessInvoice class. You don't have a main method for the JVM to execute so you should put that in the ProcessInvoice class as well.

Here is a bit of code to get you started,

public static void main(String[] args)
{
  /*  Here is where you should create all of your Invoice objects, and perform any operations on them as well.
   *  Remember to declare all of the methods and constructors in the Invoice class as public so you're able
   *  to access them in the ProcessInvoice class.  That is where the main method should be.*/
}

I hope this helps!! If it does could you give me a +up?

The way java runs is top to bottom you can not go back (not unless the class at the bottom part is being called by the top most.
the thing that happend to your invoice.createInvoiceItems was explained by justbelieved87. maybe if you would place the public void runProcess() method... it would probably work fine. however you will have another problem since

This article has been dead for over six months. Start a new discussion instead.