Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: MalformedURLException error yet code still works

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MalformedURLException error yet code still works

    .
    Last edited by jt183; October 5th, 2014 at 10:36 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MalformedURLException error yet code still works

    Can you add a call to the printStackTrace() method in the catch block for more info?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MalformedURLException error yet code still works

    Hi. I did and got this (I used aapl as the ticker) :

    java.net.MalformedURLException: no protocol: aapl
    	at java.net.URL.<init>(URL.java:586)
    	at java.net.URL.<init>(URL.java:483)
    	at java.net.URL.<init>(URL.java:432)
    	at financecalc.Finance.getStock(Finance.java:25)
    	at financecalc.Window$1.actionPerformed(Window.java:57)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6525)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6290)
    	at java.awt.Container.processEvent(Container.java:2234)
    	at java.awt.Component.dispatchEventImpl(Component.java:4881)
    	at java.awt.Container.dispatchEventImpl(Container.java:2292)
    	at java.awt.Component.dispatchEvent(Component.java:4703)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    	at java.awt.Container.dispatchEventImpl(Container.java:2278)
    	at java.awt.Window.dispatchEventImpl(Window.java:2739)
    	at java.awt.Component.dispatchEvent(Component.java:4703)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    	at java.awt.EventQueue.access$400(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:697)
    	at java.awt.EventQueue$3.run(EventQueue.java:691)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    	at java.awt.EventQueue$4.run(EventQueue.java:719)
    	at java.awt.EventQueue$4.run(EventQueue.java:717)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

    I'm reading more about the no protocol error and it seems to be something with my URL. Thank you for your reply

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MalformedURLException error yet code still works

    Is this the same as what is being executed?
     URL url = new URL("aapl");
    That URL does not have a valid protocol.
    MalformedURLException: no protocol: aapl
    You said: The URL I am using is ...
    and posted what looks like a valid URL.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MalformedURLException error yet code still works

    To use "aapl" as an example, the URL I am passing into my method (which I should have included in the original post, my apologies) is:

    public static String getURL(String ticker) {
     
     
    URL url = new URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + ticker + "%22)&env=store://datatables.org/alltableswithkeys")

    where ticker (the String being passed in) is aapl so the complete URL being passed as a parameter would be:

    URL url = new URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22aapl%22)&env=store://datatables.org/alltableswithkeys")

    I can see the confusion. I named the parameter and the String both as ticker, but they are not the same. I have since changed the name of the parameter to something else.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: MalformedURLException error yet code still works

    What happens when you use that code instead what I posted in post#4?

    The error message says something like the code in post#4 was used. Not the URL you are showing in post#5
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MalformedURLException error yet code still works

    Argh, I see what you mean now. I edited it and used the code in post #4:

    URL url = new URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + yApi+ "%22)&env=store://datatables.org/alltableswithkeys")

    and changed the parameter name to "yApi" avoid confusion. Now when I call the method using getStock(ticker) , I no longer get the error message. And it works! Thank you very much for your help, I appreciate it. I don't know why I was trying to pass that entire URL as a parameter when I could just assign it to URL url ... and pass only the actual ticker as a parameter (aapl for ex.).

    edit: I actually had to revert back to 'ticker' as the parameter name but still works
    Last edited by jt183; October 5th, 2014 at 08:45 PM.

Similar Threads

  1. Replies: 3
    Last Post: May 8th, 2014, 06:25 AM
  2. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  3. how this code works?
    By vigneshwaran in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2012, 09:18 AM
  4. This Works In My Other Code But Not This One?
    By Ooogel in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 5th, 2012, 05:49 PM
  5. dunno how to test this code so i dont know if it works help please
    By jonathanfox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 24th, 2012, 04:43 AM