Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,347 people online right now. Registration is fast and FREE... Join Now!




Need Help With Error Message

 
Reply to this topicStart new topic

Need Help With Error Message, Cannot find symbol - variable

he4dhuntr
15 Oct, 2008 - 07:24 AM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

Hey guys,
I'm writing some code for a school assignment and I'm almost done except that I'm now getting this error that I can't quite figure out. I'm sure it's pretty beginner stuff, but I'm really new to this whole java thing, so if someone could possibly explain to me why it's occuring, I could work on fixing it! I would like to understand why it's happening though, not just the answer code. Thanks!

Basically I'm creating two methods. One of them adds flowers and quantities to an order. That's the addOrder() method. The other should print the total of the order, that's the printTotal() method.

I'm getting this error: "cannot find symbol - variable newFlower" at this line (near the bottom in the printTotal() method):
CODE

            oldTotal = newFlower[i].quantity * newFlower[i].price;


I kind of get why it's happening (but could use some clarifying), but how would I go about making this method print the total of the additions to the order made by the method before it?

Here's my code so far. Thanks for any help! smile.gif

CODE

public class FlowerCounterArrays {

    private String flowerName;
    private double price;
    private int quantity;
    
    public double getPrice() {
        return price;
    }
    
    public void setPrice(double p) {
        price = p;
    }
    
    public String getFlowerName() {
        return flowerName;
    }
    
    public void setFlowerName(String name) {
        flowerName = name;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public void setQuantity(int q) {
        quantity = q;
    }
    
    public String toString() {
        return flowerName;
    }

    public void addOrder(String flowerName, int quant) {
        
        FlowerCounterArrays[] newFlower = new FlowerCounterArrays[5];
    
        newFlower[0] = new FlowerCounterArrays();
        newFlower[1] = new FlowerCounterArrays();
        newFlower[2] = new FlowerCounterArrays();
        newFlower[3] = new FlowerCounterArrays();
        newFlower[4] = new FlowerCounterArrays();
        
        newFlower[0].setFlowerName("petunia");
        newFlower[1].setFlowerName("pansy");
        newFlower[2].setFlowerName("rose");
        newFlower[3].setFlowerName("violet");
        newFlower[4].setFlowerName("carnation");
        
        newFlower[0].setPrice(0.50);
        newFlower[1].setPrice(0.75);
        newFlower[2].setPrice(1.50);
        newFlower[3].setPrice(0.50);
        newFlower[4].setPrice(0.80);
        
        int x = 0;
        
        for (FlowerCounterArrays flower : newFlower) {
            if (flower.toString().equals(flowerName)) {
                newFlower[x].setQuantity(quant);
                System.out.println("You have added " + newFlower[x].quantity + " " + newFlower[x].flowerName + "(s) to your order. These flowers are priced at $" + newFlower[x].price + " per stem. Thank you for your business!");
                break;
            }
            else {
                x++;
            }
        }
    }
    
    public void printTotal() {
        int i = 0;
        double oldTotal = 0;
        double newTotal = 0;
        double total = 0;
        while (i < 5) {
            oldTotal = newFlower[i].quantity * newFlower[i].price;
            total = newTotal + oldTotal;
            newTotal = total;        
            i++;
        }
        System.out.println("The total for your order is $" + total);
    }
}


This post has been edited by he4dhuntr: 15 Oct, 2008 - 07:31 AM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Need Help With Error Message
15 Oct, 2008 - 07:31 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
Your problem is that your newFlower variable is not an instance variable, just a variable in a function, and as such only lives as long as the function is being run. To make this better try something like so:
java
public class FlowerCounterArrays {

private String flowerName;
private double price;
private int quantity;
private FlowerCounterArrays[] newFlower;

public double getPrice() {
return price;
}

public void setPrice(double p) {
price = p;
}

public String getFlowerName() {
return flowerName;
}

public void setFlowerName(String name) {
flowerName = name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int q) {
quantity = q;
}

public String toString() {
return flowerName;
}

public void addOrder(String flowerName, int quant) {

newFlower = new FlowerCounterArrays[5];

newFlower[0] = new FlowerCounterArrays();
newFlower[1] = new FlowerCounterArrays();
newFlower[2] = new FlowerCounterArrays();
newFlower[3] = new FlowerCounterArrays();
newFlower[4] = new FlowerCounterArrays();

newFlower[0].setFlowerName("petunia");
newFlower[1].setFlowerName("pansy");
newFlower[2].setFlowerName("rose");
newFlower[3].setFlowerName("violet");
newFlower[4].setFlowerName("carnation");

newFlower[0].setPrice(0.50);
newFlower[1].setPrice(0.75);
newFlower[2].setPrice(1.50);
newFlower[3].setPrice(0.50);
newFlower[4].setPrice(0.80);

int x = 0;

for (FlowerCounterArrays flower : newFlower) {
if (flower.toString().equals(flowerName)) {
newFlower[x].setQuantity(quant);
System.out.println("You have added " + newFlower[x].quantity + " " + newFlower[x].flowerName + "(s) to your order. These flowers are priced at $" + newFlower[x].price + " per stem. Thank you for your business!");
break;
}
else {
x++;
}
}
}

public void printTotal() {
int i = 0;
double oldTotal = 0;
double newTotal = 0;
double total = 0;
while (i < 5) {
oldTotal = newFlower[i].quantity * newFlower[i].price;
total = newTotal + oldTotal;
newTotal = total;
i++;
}
System.out.println("The total for your order is $" + total);
}
}


NOTE - I haven't tested this code, it may not work

Hope that helps.
User is offlineProfile CardPM
+Quote Post

he4dhuntr
RE: Need Help With Error Message
15 Oct, 2008 - 07:39 AM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

Ah, that's what I thought. Makes sense. I tried it and it compiles well. My addOrder() method still seems to work, but when I run my printTotal() method it gives me this error: "NullPointerExpression: null"

java.lang.NullPointerException
at FlowerCounterArrays.printTotal(FlowerCounterArrays.java:78)

At this line:
CODE

oldTotal = newFlower[i].quantity * newFlower[i].price;


If I move my printTotal() code into the addOrder() method, it would work. I'm guessing it gives me the null error since the array is not populated or something? How would I go about fixing that though... Sorry for all the trouble, I'm really new at all this!
Thanks for the help so far! Much appreciated!

This post has been edited by he4dhuntr: 15 Oct, 2008 - 07:45 AM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Need Help With Error Message
15 Oct, 2008 - 08:08 AM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
You could create a constructor for your class, that would look like so:

CODE
public FlowerCounterArrays(){
    addOrder(); // DON'T FORGET PARAMETERS!!!
}


However, as you are creating additional instances of the smae class that would give you problems with infinite loops.

So, I suggest doing a quick check before your loop
(this loop:
CODE
while (i < 5) {  
            oldTotal = newFlower[i].quantity * newFlower[i].price;  
            total = newTotal + oldTotal;  
            newTotal = total;          
            i++;  
        }  


Would be liks so:
CODE
if(newFlower[0] == null){
    addOrder(); // DON'T FORGET PARAMETERS!!!
}
while (i < 5) {  
            oldTotal = newFlower[i].quantity * newFlower[i].price;  
            total = newTotal + oldTotal;  
            newTotal = total;          
            i++;  
        }  


Hope that helps.
User is offlineProfile CardPM
+Quote Post

he4dhuntr
RE: Need Help With Error Message
15 Oct, 2008 - 12:58 PM
Post #5

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

Ah.. I'm kind of getting what you're saying. But I don't get what the parameters you're telling me to add are supposed to be or look like... sad.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:14PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month