Lily S
Lily S

Reputation: 245

Retrieving Stock Quotes using Eclipse - Error: The value of the local variable is not used

I'm trying out a java application to retrieve stock quotes using Yahoo API. http://greatwebguy.com/programming/java/stock-quote-and-chart-from-yahoo-in-java/

No amendments to the sample code above. I've just added a main class.

public class Main {

    public static void main (String[] args) {

        StockBean stock = StockTickerDAO.getInstance().getStockPrice("GOOG");     
    }
}

I'm unable to execute the main class due to: The value of the local variable stock is not used.

Can anyone spot what I'm missing? Thank you in advance!

Upvotes: 0

Views: 1166

Answers (3)

pmkent
pmkent

Reputation: 1016

Yahoo discontinued it's stock quote service after it was acquired by Verizon. Intrinio is the alternative right now. There is a sample java program in GitHub at https://github.com/pmkent/intrinio-java-sample

Upvotes: 0

Thomas
Thomas

Reputation: 88747

It's just what the message said: you don't do anything with the variable stock. Normally this is a warning but it might have beed changed to be an error. To fix it, use the variable or just don't introduce it.

Alternatively, adapt the Eclipse settings to make this a warning or even ignore it, or add the @SuppressWarnings("unused") annotation to the main method.

Upvotes: 0

adarshr
adarshr

Reputation: 62603

You're not doing anything wrong. You're just not using the retrieved stock figures.

Take a peak inside the StockBean class to see what methods it exposes. I am assuming something like StockBean.getPrice() will be publicly exposed. Just use that as so:

StockBean stock = StockTickerDAO.getInstance().getStockPrice("GOOG");
System.out.println("Stock Price: " + stock.getPrice());

For debugging / logging purposes, you might want a convenient approach that spits out the contents of the entire bean. That can be done if your StockBean had the toString method overridden.

If that was the case, you could've just done the below and it would've neatly enlisted all the properties.

System.out.println(stock);

If you can edit the StockBean class, I suggest you implement toString by using Eclipse or by hand.

Upvotes: 2

Related Questions