I had someone beta-test the website today and they ran into an internal server error when trying to return to the index/show portfolio of stocks screen. The error was this:

  File "/home/ubuntu/pset8/finance/application.py", line 68, in index
    stock_info["total"] = stock_info["shares"] * stock_info["price"]
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

This is the section of the code I think the problem is in, but when querying the database, I'm getting (correct) values.

    if stock_symbols != []:
        stocks = []
        for symbol in stock_symbols:
            symbol_data = lookup(symbol["symbol"])
            stock_shares = db.execute("SELECT SUM(shares) FROM portfolio WHERE user_id=:user_id AND symbol=:symbol;",
            user_id=session["user_id"], symbol=symbol_data["symbol"])
            if stock_shares[0]["SUM(shares)"] == 0:
                stock_info = {}

                stock_info["name"] = symbol_data["name"]
                stock_info["symbol"] = symbol_data["symbol"]
                stock_info["price"] = symbol_data["price"]
                stock_info["shares"] = stock_shares[0]["SUM(shares)"]
                stock_info["total"] = stock_info["shares"] * stock_info["price"]


I see that it says stock_info["shares"] (and therefore stock_shares[0]["SUM(shares)"], but again, when querying for this, I am getting real values) is the NoneType, but where does that happen? Strangely enough, this doesn't seem to be a problem for all users. Some of my users can access their index/portfolio page with no problems, whereas others run into this error every time, even upon login.



If this SELECT SUM(shares) FROM portfolio WHERE user_id=:user_id AND symbol=:symbol;" finds no matching rows, it will return one row with None for SUM(shares).

sqlite has an aggregate function total() that will return 0.00 instead of NULL.

| improve this answer | |
  • This seemed to solve it, although now I have a new problem - purchases are appearing in the portfolio twice! Undo one knot, find another. – azb1297 Feb 28 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .