Why is my API not printing out to the textbox?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

51 Replies - 2889 Views - Last Post: 21 April 2016 - 03:49 PM Rate Topic: -----

#1 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Why is my API not printing out to the textbox?

Posted 19 April 2016 - 03:32 PM

Hello everyone! So after understanding how to get the API to work I managed to set it up so it prints out data to labels flawlessly. Now when I try to do the same thing but with a different API (Almost the same one) it decides not to print it out, feels like I missed some code, is it just me?

The first ticker (Update_BTC_Ticker) works flawlessly, it prints out as it should but the other one (Update_BTC_Trades) does not want to print out, what did I miss out?

        private void Update_BTC_Ticker(object sender, EventArgs e)
        {
            var client = new RestClient("https://btc-e.com/api");
            var request = new RestRequest("2/btc_usd/ticker", Method.GET);
            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcUsdTicker> response = client.Execute<BtcUsdTicker>(request);

            sellLabel.Text = Convert.ToString(response.Data.ticker.sell);
            buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy);


        }



        private void Update_BTC_Trades(object sender, EventArgs e)
        {
            var client = new RestClient("https://btc-e.com/api");
            var request = new RestRequest("2/btc_usd/trades", Method.GET);
            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcTradeTicker> response = client.Execute<BtcTradeTicker>(request);

            textBox1.Text = Convert.ToString(response.Data.tradeTicker.price);


        }





        public class BtcUsdTicker
        {
            public Ticker ticker { get; set; }
        }

        public class Ticker
        {

            public float high { get; set; }
            public float low { get; set; }
            public float avg { get; set; }
            public float vol { get; set; }
            public float vol_cur { get; set; }
            public float last { get; set; }
            public float buy { get; set; }
            public float sell { get; set; }
            public int updated { get; set; }
            public int server_time { get; set; }



        }


        public class BtcTradeTicker
        {
            public tradeTicker tradeTicker { get; set; }
        }


        public class tradeTicker
        {
            public int date { get; set; }
            public double price { get; set; }
            public double amount { get; set; }
            public int tid { get; set; }
            public string price_currency { get; set; }
            public string item { get; set; }
            public string trade_type { get; set; }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Why is my API not printing out to the textbox?

#2 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 19 April 2016 - 03:55 PM

It was because my timer blocked the method I changed it from

            timer1 = new Timer();
            timer1.Tick += new EventHandler(Update_BTC_Ticker);
            timer1.Interval = 2000;
            timer1.Start();




to


            timer1 = new Timer();
            timer1.Tick += new EventHandler(Update_BTC_Ticker);
            timer1.Tick += new EventHandler(Update_BTC_Trades);
            timer1.Interval = 2000;
            timer1.Start();

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,114
  • Joined: 05-May 12

Re: Why is my API not printing out to the textbox?

Posted 19 April 2016 - 04:00 PM

You do realize that you are making it very hard for us to help you with the lack of information you give us in your various threads. Have you stepped through your code to determine if data is coming back? Can you show us what the data that comes back is supposed to look like?

You have to remember that the majority of the folks on this forum will not start from scratch and figure out how to install and use that library that you are using. So you need to give us as much information as possible as to what the data is supposed to look like as well as what the library you are using is expecting to see.
Was This Post Helpful? 0
  • +
  • -

#4 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 19 April 2016 - 04:06 PM

That didnt really cross my mind since I am in so deep in this project, But I already solved the issue, I used the debugger to determinate that the method didnt get hit which made me look over the code once more just to realize that I forgot to call the method in the timer aswell.

Now im getting thrown an error which looks like this.
Here is a visual refrence
And I did feel like creating another thread just to ask about one error even though I am terrible at error handleing. I know that its probably something that cant be parsed correctly (supposedly textBox1.Text = Convert.ToString(response.Data.tradeTicker.price); ) So I will try to work my way around that.

And for future refrences, when I create new threads Il make sure to provide as much information as I can.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,114
  • Joined: 05-May 12

Re: Why is my API not printing out to the textbox?

Posted 19 April 2016 - 04:07 PM

I see that you solved your issue in the time it took me to try to write something on this stupid phone.

I'm glad you solved it!

Based on your solution, it was not a matter of your timer blocking the other updates, but rather you failing to hook up an event handler to perform an update.
Was This Post Helpful? 0
  • +
  • -

#6 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 19 April 2016 - 04:11 PM

I am not a native english speaker as you have probably already have figured out so from time to time it can be hard for me to get a message from point A to point B without wandering off into the distance with my answers but yes, thats what was wrong, and if you dont mind me asking while im here, if you would guess why its throwing me the error, by just looking at the code that I provided you and the Examples, what would your best guess be? Keep in mind that you dont have to answer this if you think its irrelevant.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,569
  • Joined: 12-December 12

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 12:00 AM

Please copy the details of the error directly here, not just as a screenshot that will very likely disappear at some point. There are options in that dialog to view details and copy to clipboard. Also indicate what line the error refers to in code posted here.
Was This Post Helpful? 0
  • +
  • -

#8 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 12:07 AM

I am at Uni at the moment but I will do that as soon as I get home!
Was This Post Helpful? 0
  • +
  • -

#9 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 03:25 AM

This is the error message

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=BitCoin Evolution
  StackTrace:
       at BitCoin_Evolution.Form1.Update_BTC_Trades(Object sender, EventArgs e) in c:\Users\Developer\Documents\Visual Studio 2015\Projects\BitCoin Evolution\BitCoin Evolution\Form1.cs:line 56
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at BitCoin_Evolution.Program.Main() in c:\Users\Developer\Documents\Visual Studio 2015\Projects\BitCoin Evolution\BitCoin Evolution\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



And here is the code with the line that is giving me an error with a comment beside it


        public Form1()
        {
            InitializeComponent();
            timer1 = new Timer();
            timer1.Tick += new EventHandler(Update_BTC_Ticker);
            timer1.Tick += new EventHandler(Update_BTC_Trades);
            timer1.Interval = 2000;
            timer1.Start();
        }

        private void Update_BTC_Ticker(object sender, EventArgs e)
        {
            var client = new RestClient("https://btc-e.com/api");
            var request = new RestRequest("2/btc_usd/ticker", Method.GET);
            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcUsdTicker> response = client.Execute<BtcUsdTicker>(request);

            sellLabel.Text = Convert.ToString(response.Data.ticker.sell);
            buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy);


        }


        
        private void Update_BTC_Trades(object sender, EventArgs e)
        {
            var client = new RestClient("https://btc-e.com/api");
            var request = new RestRequest("2/btc_usd/trades", Method.GET);
            //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
            request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

            IRestResponse<BtcTradeTicker> response = client.Execute<BtcTradeTicker>(request);

            tradeLabel.Text = Convert.ToString(response.Data.tradeTicker.price); //Here is the line that is giving me an error
            
       
        } 




        public class BtcUsdTicker
        {
            public Ticker ticker { get; set; }
        }

        public class Ticker
        {

            public float high { get; set; }
            public float low { get; set; }
            public float avg { get; set; }
            public float vol { get; set; }
            public float vol_cur { get; set; }
            public float last { get; set; }
            public float buy { get; set; }
            public float sell { get; set; }
            public int updated { get; set; }
            public int server_time { get; set; }



        }

        
        public class BtcTradeTicker
        {
            public tradeTicker tradeTicker { get; set; }
        }


        public class tradeTicker
        {
            public int date { get; set; }
            public double price { get; set; }
            public double amount { get; set; }
            public int tid { get; set; }
            public string price_currency { get; set; }
            public string item { get; set; }
            public string trade_type { get; set; }
        }

Was This Post Helpful? 0
  • +
  • -

#10 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 03:33 AM

Here is a codeshare Might be easier for you to read :-)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,114
  • Joined: 05-May 12

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 04:29 AM

Actually, please keep your code posted here. As nice as the UI is at that site, there are no guarantees that the code will stick around in the appropriate state to maintain the continuity of this thread's discussion for any future folks who may stumble in this thread.

Anyway, the exception was explicit about the problem. It says that something was not set. Use your debugger to determine which field was not set.

In my experience, a lot of JSON deserializers will not set some fields if the data to be parsed does not match the schema of the object to be filled, or if there is no data to be parsed, or if the data is not formatted properly.

Are you sure you got data back? Some web services will throttle the number of calls you can make within a time period?

If you got data back, are you sure it matches the schema of the object you are trying to fill?
Was This Post Helpful? 0
  • +
  • -

#12 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,221
  • Joined: 07-April 08

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 04:33 AM

You'll need to set a breakpoint on that line and look at each point in your object. Object reference means that something is null so hover over response. Does it say it's null? Do the same for Data, tradeTicker and price. Do any of them say they are null?
Was This Post Helpful? 0
  • +
  • -

#13 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 04:35 AM

What do you mean with each point in the object? Also I will do it as soon as I get home :-)
Was This Post Helpful? 0
  • +
  • -

#14 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 12:51 PM

Anyone who figured out a solution because I didnt yet, I've tried debugging but i cant seem to locate whats wrong, I looked over the documentations and the restsharp but I couldnt seem to locate the issue.

Restsharp
BTC API
The documentations
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15806
  • View blog
  • Posts: 63,316
  • Joined: 12-June 08

Re: Why is my API not printing out to the textbox?

Posted 20 April 2016 - 12:55 PM

Debugging is not that complicated. The error says look here 'Update_BTC_Trades'. Put a break point there. Now you can step through each line. You can mouse over to see if a variable is something or is still null. You have, what? Five lines. this should be a walk in the park. Looking at that line what may have a 'null' value? Most likely not the label.. so some part of 'response' or 'response.data' or 'response.data.tradetricker' or 'response.data.tradeticker.price'.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »