ConvertAPI / convertapi-library-java

A Java library for the ConvertAPI
https://www.convertapi.com
Other
17 stars 11 forks source link

System.console() is null when using an IDE #5

Open naverus opened 5 years ago

naverus commented 5 years ago

This error is triggered when trying to execute any logic by using any IDE(Idea, Eclipse, etc)

E.G. Execute UserInformation.java by using Idea/Eclipse and the following error will be triggered and no logic is being executed.

Caused by: java.lang.NullPointerException
    at com.convertapi.client.Http.getRequestBuilder(Http.java:62)
    at com.convertapi.client.Param.lambda$upload$1(Param.java:98)

Possible fix:


    static Request.Builder getRequestBuilder() {
        if (System.console() == null) {
            System.out.printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
        } else {
            System.console().printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
        }
        String agent = String.format("ConvertAPI-Java/%.1f (%s)", Http.class.getPackage().getImplementationVersion(), System.getProperty("os.name"));
        return new Request.Builder().header("User-Agent", agent);
    }
JonasJasas commented 5 years ago

Could you please explain more how can I reproduce this error and how this fix work?

On Wed, Jun 5, 2019 at 2:41 AM Navor Nuñez notifications@github.com wrote:

This error is triggered when trying to execute any logic by using any IDE(Idea, Eclipse, etc)

E.G. Execute UserInformation.java by using Idea/Eclipse and the following error will be triggered and no logic is being executed.

Caused by: java.lang.NullPointerException at com.convertapi.client.Http.getRequestBuilder(Http.java:62) at com.convertapi.client.Param.lambda$upload$1(Param.java:98)

Possible fix:

static Request.Builder getRequestBuilder() {
    if (System.console() == null) {
        System.out.printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
    } else {
        System.console().printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
    }
    String agent = String.format("ConvertAPI-Java/%.1f (%s)", Http.class.getPackage().getImplementationVersion(), System.getProperty("os.name"));
    return new Request.Builder().header("User-Agent", agent);
}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ConvertAPI/convertapi-java/issues/5?email_source=notifications&email_token=AD4AOHBHXIUIE3EEMDDB7FLPY34TLA5CNFSM4HTH4PNKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GXU3H5Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AD4AOHDOHNSAVN2D4I4CCGTPY34TLANCNFSM4HTH4PNA .

naverus commented 5 years ago

@JonasJasas Please follow these steps: Preconditions

Scenario 1

Current result in the console is:

Exception in thread "main" java.lang.NullPointerException
    at com.convertapi.client.Http.getRequestBuilder(Http.java:62)
    at com.convertapi.client.ConvertApi.getUser(ConvertApi.java:89)
    at com.convertapi.client.ConvertApi.getUser(ConvertApi.java:84)
    at com.convertapi.client.examples.UserInformation.main(UserInformation.java:20)

Scenario 2 Also you can add an UnitTest and run it, so you will have same issue. Let say:

package com.convertapi.client;
import com.convertapi.client.model.User;
import org.junit.Test;
public class UserInformationTest {
    @Test
    public void verifyUserInformation()  {
        Config.setDefaultSecret("CONVERTAPI_SECRET");    //Get your secret at https://www.convertapi.com/a
        User user = ConvertApi.getUser();

        System.out.println("API Key: " + user.ApiKey);
        System.out.println("Secret: " + user.Secret);
        System.out.println("Email: " + user.Email);
        System.out.println("Name: " + user.FullName);
        System.out.println("Status: " + user.Status);
        System.out.println("Active: " + user.Active);
        System.out.println("Seconds left: " + user.SecondsLeft);
    }
}

And add this dependency:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

How to fix this issue The idea is that Console.istty is false(so System.console() will be null) when you run any logic by using an IDE, so this is why the the following code triggers always NullPointerException on these specified scenarios.

System.console().printf

Possible fix Just remove this line:

        System.console().printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());

or validate null.

        if (System.console() == null) {
            System.out.printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
        } else {
            System.console().printf("VERSIJA: %s", Http.class.getPackage().getImplementationVersion());
        }