JAVA Retrive current bitcoin price with JSON

0 votes

I want to get the current/historical bitcoin price by using JSON..

However, the code shows a following ERROR:-

Exception in thread "main" java.lang.NullPointerException at RwithJlab.Basic.main(Basic.java:19)


package RwithJlab; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.nio.charset.Charset; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Basic 
{ 

        public static void main(String[] args) throws MalformedURLException, IOException, JSONException 
{ 

JSONObject data = getJSONfromURL("https://blockchain.info/charts/market-price?format=json"); JSONArray data_array = data.getJSONArray("values"); 

for (int i = 0; i < ((CharSequence) data_array).length(); i++) 
{ 

      JSONObject price_point = (JSONObject) data_array.get(i); 

        // Unix time 
        int x = price_point.getInt("1364062505"); 

      // Bitcoin price at that time 
      double y = price_point.getDouble("y"); 

      // Do something with x and y. 
      System.out.println(x); 
    } 
} 

public static JSONObject getJSONfromURL(String URL) throws JSONException 
{ 

    try 
    { 

        URLConnection uc; 
          URL url = new URL(URL); 
          uc = url.openConnection(); 
          uc.setConnectTimeout(10000); 
          uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); uc.connect(); 

BufferedReader rd = new BufferedReader( 
                    new InputStreamReader(uc.getInputStream(), 
                    Charset.forName("UTF-8"))); 

StringBuilder sb = new StringBuilder(); 
int cp; 
while ((cp = rd.read()) != -1) 
{ 
    sb.append((char)cp); 
} 

String jsonText = (sb.toString()); 

return new JSONObject(jsonText.toString()); 
} catch (IOException ex) 
{ 

          return null; 
        } 
    } 
}

Could anyone please help me with this?

Feb 28, 2022 in Blockchain by Aditya
• 7,680 points
800 views

1 answer to this question.

0 votes

You should start log Exceptions when you catch them, otherwise you will not be clear once the exception occurs.

In your case you are catching an IOException and returning null. This results in a NullPointerException later, but you cannot see the root cause.

Use Log that IOException (at least call ex.printStackTrace()) and you will see the real reason.

answered Feb 28, 2022 by Soham
• 9,700 points

Related Questions In Blockchain

0 votes
1 answer

Using json rpc with curl on java?

Quoting different pieces the way you're doing ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,210 points
2,410 views
0 votes
1 answer

How to get price from bitcoin to USD with api

If I'm not wrong, is this what ...READ MORE

answered Apr 7, 2022 in Blockchain by Aditya
• 7,680 points
2,503 views
0 votes
1 answer

How can I simulate private bitcoin network with random peer discovery?

You can model the whatever network you ...READ MORE

answered May 29, 2018 in Blockchain by Johnathon
• 9,090 points
534 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,148 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,706 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
0 votes
1 answer

Bitcoin Price that updates in specific Seconds

The better way to do so is ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,700 points
335 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP