Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
Hello once again!

I fixed the last issue I had with this software without any help but now its telling me something that I have never occurd before.

Let me show you the code so it will be easier toe xplain the situation.

C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using RestSharp;

namespace BitCoin_Evolution
{

    public partial class Form1 : Form
    {
        string con = "Connected";
        string dc = "Disconnected";

        private Timer timer1;
        int togMove;
        int MvalX;
        int MvalY;

        public Form1()
        {
            
            InitializeComponent();
            timer1 = new Timer();
            timer1.Tick += new EventHandler(Update_BTC_Ticker);
            timer1.Tick += new EventHandler(Update_BTC_Trader);
            //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_Trader(object sender, EventArgs e)
        {

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

            IRestResponse<BtcTradeTicker> response = tradeClient.Execute<BtcTradeTicker>(tradeRequest);

            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; }
        }



let me explain what is going on.

I cam calling a webrequest to

C#
var client = new RestClient("https://btc-e.com/api");
var request = new RestRequest("2/btc_usd/ticker", Method.GET);

Which is working perfectly, its connecting with the labels and printing out without any hassle.

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



C#
private void Update_BTC_Ticker(object sender, EventArgs e)
{
    var client = new RestClient("https://btc-e.com/api"); //1
    var request = new RestRequest("2/btc_usd/ticker", Method.GET); //2

    //request.AddHeader("Key", "46G9R9D6-WJ77XOIP-XH9HH5VQ-A3XN3YOZ-8T1R8I8T");
    request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; //3

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

    sellLabel.Text = Convert.ToString(response.Data.ticker.sell); //5
    buyLabelTrue.Text = Convert.ToString(response.Data.ticker.buy); //6
}



As you can see the 1 is the main website its pulling from and then I am giving it the extra information that it needs to be pulling from this exact APi (number 2)

(3)Then it desierializes the code to Json > C#
(4) its calling what it just deserialized to check if there is something there (as you can see in the code and the API there is Data there)

(5) its locating the sell one and returning the value to the label
C#
public float sell { get; set; }


(6) It does the exact same thing as (5) but its pulling .buy instead of .sell


My question is..


How come its not pulling the data from the other json table I created

C#
public class BtcTradeTicker


It has the
C#
public double price { get; set; }

For it to grab and check with the API but instead its saying that .Data is null

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



Sidenote, I am using a third party library called restSharp

RestSharp - Simple REST and HTTP Client for .NET[^]

What I have tried:

The code didnt look like that in the beginning, I've looked over the API and my code multiple times I really cant find whats wrong, I am bascally trying to redo the exact same thing I did for the first webrequest but a second time for a different API but its not letting me get any Data.
Posted
Updated 21-Apr-16 10:52am
Comments
ZurdoDev 21-Apr-16 16:24pm    
Too much code for me to walk through with my eyes. If something is null, trace back to where it is supposed to get set and find out why. It's pretty simple.
BladeLogan 21-Apr-16 16:37pm    
I think its trying to parse an Array through an object, would you mind getting on TeamViewer with me and check it out? I completly understand if you dont want to / have time, its just that I've been at this for about 48 hours now.
ZurdoDev 21-Apr-16 18:52pm    
Again, the first thing to do is find out what is null and why. That is very, very easy for you to do. Put a breakpoint and walk through your code. Very simple. You don't need our help for that.

Once you find the issue, then you may need to ask a more specific question.
BladeLogan 21-Apr-16 18:54pm    
Well, it says that response.Data is null when debugging on this line (which is the line with the issue)

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

It might be because I am trying to parse an Array through an Object
ZurdoDev 21-Apr-16 19:17pm    
So where is response.Data getting set?

1 solution

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
Comments
CPallini 22-Apr-16 2:55am    
5.
Patrice T 22-Apr-16 2:58am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900