0

So this doesn't seem to be working, but then again you can't return a String from a void method. The problem is I absolutely need to return a String based on how my classes are structured. What can I do to accomplish this? I need to get a value for the price of the item.

@Override
public String getCost() {
final String[] productValue = {"null"};
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
        query.whereEqualTo("productName", "Capris");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    for (ParseObject productCost : list) {
                        productValue[0] = (String) productCost.get("productPrice");
                        // Cannot return a value from a method with void result type
                        return productValue[0];
                    }
                }
                else {
                    // Cannot return a value from a method with void result type
                    return null;
                }
            }
        });
        return null;
    }

1 Answer 1

2

Your conditions are wrong

 @Override
    public String getCost() {
        final String[] productValue = {null};
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>
        query.whereEqualTo("productName", Capris);
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> list, ParseException e) {
                if (e == null) { //no exception, hence success
                    productValue[0] = list.get(0).getString("productPrice");
                }
            }
        });
        return productValue[0];
    }

In above code productValue[0] may be null as its an aysnc call So replace findInBackground with find()

public String getCost() {
    String productValue = null;
    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Inventory");
    query.whereEqualTo("productName", "Capris");
    try {
        List<ParseObject> results = query.find();
        productValue = results.get(0).getString("productPrice");
        return productValue;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return productValue;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Right. Thanks for pointing that out but it still doesn't address the problem that I can't return a String on a void method. Either way, I need to extract that String. My getCost() method must return a String.
Hmm... the problem is if the return does not occur in the "done" method then the productValue string is never used. But if the return occurs outside of that scope the String is inaccessible and cannot be assigned. There's something about the "done" method that refuses me to let me return a String. This might be a deeper problem than it appears on the surface to be.
Thanks for the effort. I really do appreciate it but it couldn't possible work if the return value is not taking place within the "done" method. There's no explicit connection between the value that's going into the string and the value that's being returned at the end of the method. I was just wondering if there was an alternative way of doing this entirely. It's returning null as expected because the variable isn't being assigned the new value from the query.
Is there a way to do this Async?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.