0

I've a Jersey based REST Client and a GET call defined as this :

The code doesn't come out after the client.get() call. A null pointer exception is hit on the client.get() call , this is the initialization logic from the class which calls the REST client :

Student.java :

public class Student {

    private static RestClient client;

    @SuppressWarnings("static-access")
    public Student(Client client) {
        this.client = new RestClient(client, "xyz");
    }

    public static RestClient getClient() {
        return client;
    }

    public static void addStudent() throws Exception {

        try {       
            String addStudentUri = "https://www.zebapi.com/api/v1/market/ticker-new/btc/inr";     
            ClientResponse js_response = getClient().get(URI.create(addStudentUri));
            String s = js_response.getEntity(String.class);
            System.out.println(s);
        } catch (Exception e) {
            System.out.println(e);          
        }
    }

    public static void main(String[] args) {
        try {
            addStudent();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

RestClient.java

public class RestClient {

    private Client client;
    private String randomHeader = "";

    public RestClient(Client jerseyClient) {
        this.client = jerseyClient;
    }

    public RestClient(Client jerseyClient, String random) {
        this.client = jerseyClient;
        this.randomHeader = random;
    }

    public String getRandomHeader() {
        return randomHeader;
    }

    public void setRandomHeader(String randomHeader) {
        this.randomHeader = randomHeader;
    }

    public RestClient() {
    }

    public ClientResponse get(URI uri)
    {
        return client.resource(uri)
                .get(ClientResponse.class);
    }

    public ClientResponse post(URI uri)
    {
        return client.resource(uri)
                .type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class);
    }

}

Can you please help?

pep8
  • 371
  • 3
  • 18
  • are you sure that you added `application/json` header in postman request? – dehasi Aug 05 '18 at 00:00
  • In Postman request, I used "application/json" for "Content-Type" header. Also, the authentication was set to "No auth" in Postman and set authorization header to user:password without encoding it to base64. Someone please help! – pep8 Aug 05 '18 at 20:37
  • What/s the result of the request? We need more information. Status code? Response body? etc. – Paul Samsotha Aug 06 '18 at 07:40
  • Code hits the exception : "Exception....null" , after the client.post() line, it goes to exception path.. – pep8 Aug 06 '18 at 09:46
  • @PaulSamsotha Updated the question. Im hitting NPE on the getClient.post() call line. – pep8 Aug 07 '18 at 01:05
  • @PaulSamsotha I updated the description of the question before your comment! - public RestClient getClient() { return client; } – pep8 Aug 07 '18 at 01:16
  • Updated with complete code, could you help? – pep8 Aug 07 '18 at 02:20

1 Answers1

0

You never initialized the client, that's why you are getting an NPE; you are trying to use an object that has not been initialized.

What you should do is remove all the static modifiers from all the methods and the client field; all but the main method. Then in the main method, create a new Student passing in a new Client. Then use that Student instance to call the addStudent() method.

public class Student {
    private RestClient client;

    public Student(Client client) {
        this.client = new RestClient(client, "xyz");
    }

    public RestClient getClient() {
        return this.client;
    }

    public void addStudent() {}

    public static void main(String...args) {
        new Student(Client.create()).addStudent();

        // or a more verbose way
        Client jerseyClient = Client.create();
        Student student = new Student(jerseyClient);
        student.addStudent();
    }
}

I marked this as Community Wiki because really it should be marked as duplicate of What is a NullPointerException, and how do I fix it?. Is has become standard practice here in Stack Overflow, when a question is about an easily fixable NullPointerException, we should just close the question as a duplicate of that linked question.

NullPointerExceptions are generally easy to fix if it is caused by code that we the developer wrote. Being able to detect and fix this is something that should be trivial. So please review the linked question so you can handle this type of problem on your own next time.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720