Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking for null return? #64

Closed
EXJUSTICE opened this issue Feb 4, 2017 · 7 comments
Closed

Checking for null return? #64

EXJUSTICE opened this issue Feb 4, 2017 · 7 comments

Comments

@EXJUSTICE
Copy link

Hi,

Great API, but I was having some issues checking for null returns if one types in the wrong ticker code.

My asyncTask code is shown below : despite checking for null at PostExecute I am still getting a NullpointerException, meaning that it passed the null check. Am I not checking in the correct manner?

`

    @Override
    protected void onPreExecute(){
        pdialog= new ProgressDialog(getActivity());
        pdialog.setMessage("Fetching stock data...");
        pdialog.show();

    }

    @Override
    protected Boolean doInBackground(String... params){
        try{
            Calendar from = Calendar.getInstance();
            Calendar to = Calendar.getInstance();

            temp = YahooFinance.get(params[0]);



        }catch (java.io.IOException e){

        }
        return true;
        //this temp is simply returned to onPostExecute
    }

    @Override
    protected void onPostExecute(Boolean result){
        if (pdialog.isShowing()){
            pdialog.dismiss();
        }

        if(temp==null){
            Toast.makeText(getActivity(),"Invalid ticker, please recheck",Toast.LENGTH_LONG).show();
        }

            moveResultToUI(temp);

`

@sstrickx
Copy link
Owner

sstrickx commented Feb 5, 2017

Hi,

I can't really tell from this piece of code what could be the problem.
Can you post the full stack trace?

@EXJUSTICE
Copy link
Author

Hey man,

Thanks for responding, first of all, this is a great api.

I'm just inputting random things in the sticker EditText to see what happens.

This is what I get:

02-05 20:48:04.092 18328-18328/com.xu.investo E/View: hasTransientState decremented below 0: unmatched pair of setHasTransientState calls 02-05 20:48:04.762 1290-1290/? E/CliptrayService: cliptrayEventReceiver :onReceivecom.lge.systemservice.core.cliptray.HIDE_CLIPTRAY 02-05 20:48:05.242 18328-18328/com.xu.investo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xu.investo, PID: 18328 java.lang.NullPointerException at yahoofinance.quotes.stock.StockQuote.getChange(StockQuote.java:53) at com.xu.investo.MainFragment.moveResultToUI(MainFragment.java:206) at com.xu.investo.MainFragment$FetchXDayData.onPostExecute(MainFragment.java:361) at com.xu.investo.MainFragment$FetchXDayData.onPostExecute(MainFragment.java:321) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5118) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610) at dalvik.system.NativeStart.main(Native Method)

@carvalho2707
Copy link

I think what he's asking is how can we validate if the stock exist or not

@sstrickx
Copy link
Owner

sstrickx commented Feb 8, 2017

That's indeed a bit difficult. Since Yahoo Finance is not returning an error if a stock doesn't exist, this library is just creating a new Stock object like it would for any successful request.
All the fields (except for the symbol) will be empty though, so currently the best way to check if the stock actually exists on Yahoo Finance is probably to check if the name is empty. (Stock.getName())

Not sure what would be the best way for the library to behave in this situation. Throw an exception when the name for the requested stock is empty?
What if a list of stocks are requested and only 1 of them seems to not exist? Return the full list, excluding this 1 stock, or drop everything and throw an exception?
Need to think a bit about this... If anybody has some ideas, I'd definitely like to hear them.

On top of that, from the stack trace it's clear that there's actually a problem in the StockQuote.getChange() method, which should check if there's a price and a previous close price before trying to do a calculation with those values. Will provide a small fix for that when I find some time.

@EXJUSTICE
Copy link
Author

Thank you both for your input.
I did go through the documentation, and did read that all methods return null if nothing is found, but I somehow thought that the null would replace the Stock object itself, rather than its data fields.

Name check makes sense, although I am confused as to how it got to getChange() - Shouldn't getClose() be called first?

In my current set up, the user adds their stocks one by one. For a List, I suggest iterating through it, and returning the Stocks one by one, using a name check (?) on them iteratively to make sure they exist.

@EXJUSTICE
Copy link
Author

EXJUSTICE commented Feb 11, 2017

Solved issue with condition

stock.getName()==null|

.

On another note, HistoricalQuotes cannot be withdrawn for bitcoin data or BTCUSD. After investigating this further, I've come to the conclusion that this is a Yahoo server side issue.

@sstrickx
Copy link
Owner

I've added some checks to prevent NullPointerExceptions in the StockQuote.
There's now also a Stock.isValid() method available to check if Yahoo Finance recognized the requested symbol (basically just a name != null check).
This has just been released in 3.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants