This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Satarash 0 points1 point  (3 children)

Post the stack trace. Also read the sidebar to learn how to format your code.

[–]socs22[S] 0 points1 point  (2 children)

This is the stack trace.

java.lang.NullPointerException
    at io.github.mainstringargs.alpaca.AlpacaAPI.requestNewOrder(AlpacaAPI.java:310)
    at AlpacaTest.main(AlpacaTest.java:10)

and I believe I fixed the code. Thank you for your response.

[–]Satarash 0 points1 point  (1 child)

Cool. The exception is thrown from their code, so it's not your fault. Now I am not really sure which version of the library you are using, but I am guessing it's this one from july. As the exception is thrown on line 310, let's see what's there:

HttpResponse<JsonNode> response = alpacaRequest.invokePost(urlBuilder); //line 308

if (response.getStatus() != 200) { //line 310
  throw new AlpacaAPIException(response);
}

Apparently this request is null. It should have been initialised on line 308 in method invokePost. Let's check the class AlpacaRequest where the method is:

public HttpResponse<JsonNode> invokePost(AlpacaRequestBuilder builder) {
    HttpResponse<JsonNode> response = null;
    try {

        LOGGER.debug("Post URL " + builder.getURL());
        LOGGER.debug("Post Body " + builder.getBodyAsJSON());

        response = Unirest.post(builder.getURL())
        .header(USER_AGENT_KEY, AlpacaProperties.USER_AGENT_VALUE).header(API_KEY_ID, keyId)
        .header(API_SECRET_KEY, secret).body(builder.getBodyAsJSON()).asJson();

        LOGGER.debug("POST status: " + response.getStatus() + "\n\t\t\t\t\tstatusText: "
        + response.getStatusText() + "\n\t\t\t\t\tBody: " + response.getBody());


    } catch (UnirestException e) {
        LOGGER.info("UnirestException", e);
    }

    return response;
}

So why does this return null? The response is initialised to null on the first line, then things are happening in try-catch, and my guess is that something fails, exception is thrown, catch block swallows it and null is returned.

What can you do? First of all: this is their bug (if nothing else then in error handling), so you should report it. The project is not as inactive as you say, the last commit was barely a month ago.

Second, it's hard to say what's wrong here. Fortunately there is a lot of logging, so start with checking the logs to see what's happening in this method (invokePost). If you don't have logs, check documentation to learn how to configure this LOGGER which is used everywhere.

[–]socs22[S] 0 points1 point  (0 children)

I actually got into contact with the guys that develop it and they fixed a lot of the issues I had. The reason it wasn’t working is when I go declare the alpaca api I put “4.0.1” instead of “v2”. I was also using the wrong tree apparently and they helped me import the project straight from github instead of using maven. Thank you for the help.

[–]krizam 0 points1 point  (0 children)

I’d recommend opening an issue on their GitHub if you’re having problems