简体   繁体   中英

JAVA URI Can't be null - assist with this function

Hi wise sages of the StackOverflow universe. I was hoping you might be able to assist me with an issue I'm having.

The point of the code below is to show whether the symbol will solicit a valid response from the yahoo API.

The function GetSymbol takes a double and returns a string, in the case of GetSymbol(6149) the string returned is "IBM". (Just FYI, in the real thing, the

    symbol = GetSymbol(6149);
    //symbol = "IBM";           // This is to show that it works normally

Code is not really in the function, it is just there for my testing purposes. When the symbol = "IBM"; is not // out, it works fine, but as such it does not. In the real code, the caller thread would look like if(test(GetSymbol(6149)) == 0). (0 means no error, 1 means error).

For some reason, even though the return value of GetSymbol(6149) is equivilent to "IBM", the URL yahootest accepts the latter but not the former. (using GetSymbol(6149) as an input for test() gives me URI cant be null as an exception. Can you think of any reason why?

And just in case you think it helps, I have listed the Get Symbol function below the test function. Its call is

    while(i <= Math.pow(26,4)){
    GetSymbol(i);
    i++;
    }

This is the test function

public int test(String symbol){

    int testnum = 0;
    symbol = GetSymbol(6149);
    //symbol = "IBM";           // This is to show that it works normally

    String url = "http://download.finance.yahoo.com/d/quotes.csv?s="
            + symbol + "&f=nsl1op&e=.csv";


        try{
        URL yahootest = new URL(url);
        URLConnection data = yahootest.openConnection();
        Scanner input = new Scanner(data.getInputStream());


    }catch(Exception e){
        System.out.println(e.getMessage());
        testnum++;
    }

    return testnum;
}

This is the GetSymbol function

 public  String GetSymbol(double i){        

    double parts = Math.floor(i/26);
    double remainder = Math.floor(parts/26);
    double remainder2 = Math.floor(remainder/26);


    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    String e = "%5E";


    if(i <= 26){
        a = (int) (i + 64);
    }
    else if(i <= Math.pow(26,2)){
        a = (int) parts;
        b = (int) (i - (26*a));
        a = a + 64;
        b = b + 64;
        if(b == 64){b++;}
    }
    else if((i <= Math.pow(26,3))){
        a = (int) remainder;
        b = (int) (parts -(remainder*26));
        c = (int) (i - (parts*26));
        a = a + 64;
        b = b + 64;
        c = c + 64;
        if(b == 64){b++;}
        if(c == 64){c++;}
    }
    else if((i <= Math.pow(26,4))){
        a = (int) remainder2;
        b = (int) (remainder - (remainder2*26));
        c = (int) (parts - (remainder * 26));
        d = (int) (i - (parts*26));
        a = a + 64;
        b = b + 64;
        c = c + 64;
        d = d + 64;
        if(b == 64){b++;}
        if(c == 64){c++;}
        if(d == 64){d++;}       
    }

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(String.valueOf((char) a));
    stringBuilder.append(String.valueOf((char) b));
    stringBuilder.append(String.valueOf((char) c));
    stringBuilder.append(String.valueOf((char) d));
    String string = stringBuilder.toString();


    symbol = string;
     return symbol;
 }

This is the entire stack trace

java.lang.IllegalArgumentException: URI can't be null.
at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:147)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1097)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at Stocks.Begin.test(BasicApp.java:185)
at Stocks.Begin.<init>(BasicApp.java:42)
at Stocks.BasicApp.main(BasicApp.java:21)

If I had to take a guess, it's something to do with the fact that you append 4 characters together to create your URL when using GetSymbol, yet IBM is only three characters. Most likely when using GetSymbol instead of just "IBM", there is a hidden symbol, something like "IBM\\0". From what I can tell, what you need to do is modify your GetSymbol function as such:

StringBuilder stringBuilder = new StringBuilder();
if(a > 0) stringBuilder.append(String.valueOf((char) a));
if(b > 0) stringBuilder.append(String.valueOf((char) b));
if(c > 0) stringBuilder.append(String.valueOf((char) c));
if(d > 0) stringBuilder.append(String.valueOf((char) d));

String string = stringBuilder.toString();

What this does is it ensures that each characters is not your default 0 that you set earlier (int a = 0; ...) before it puts it on the string, because sites don't like it when you say "example.com/test\\0", they'd prefer "example.com/test". Not sure if I made this completely clear so just tell me if you have any questions. Simply put, you were checking how many characters you needed and setting a,b,c, and d likewise but then you proceeded to append all 4 no matter how long you knew it was

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM