Header Ad

Thursday, August 23, 2018

java.lang.AbstractMethodError

Lets understand what it is:

AbstractMethodError Thrown when an application tries to call an abstract method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

How did I encounter java.lang.AbstractMethodError

Well I was having two spring project

  • UserLib : Lib which having core classes
  • MasterLib : Lib which is having dependency of UserLib
UserLib  had class

RegisterUser.java


public class RegisterUser{
public void register() {
// stuff to register
}
}
MasterLib was using RegisterUser class


public class UserService extends RegisterUser{ public  void doRegister() { UserService  user = new UserService (); user .register();
}
}

I had made build of UserLib  and run Master lib it worked; but for some reason I had make RegisterUser class as abstract class so I made it but, I forgot make build of UserLib and I simple run my masterLib, that is where I encountered this issue.

How to solve java.lang.AbstractMethodError : Either don't go for abstract method approach; or you just have implementation of the abstract class and don't forget to make build :) if you are have project structure like me.




Sunday, August 19, 2018

Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)

Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)

It was happened to be issue while I was using HttpEntity and pass header in resttemplates exchange method
I was adding parameters in entity when I removed it, it worked.

What I was doing

            RestTemplate _restTemplate =new RestTemplate();
MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(mediaType);
headers.add("x-request-id",requestId);
HttpEntity<String> entity = new HttpEntity("parameters", headers); //Issue was here
long startTime = System.currentTimeMillis();
ResponseEntity<Map> result = _restTemplate.exchange("myURL?paramKey={para1}", HttpMethod.GET, entity, Map.class, "paramValue");
  
How did I fix it:

RestTemplate _restTemplate =new RestTemplate();
MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(mediaType);
headers.add("x-request-id",requestId);
HttpEntity<String> entity = new HttpEntity(headers); //Fixed issue
long startTime = System.currentTimeMillis();
ResponseEntity<Map> result = _restTemplate.exchange("myURL?paramKey={para1}", HttpMethod.GET, entity, Map.class, "paramValue");

What was happening:

HttpEntity<String> entity = new HttpEntity("parameters", headers);here parameters were treated as body.



Thursday, August 16, 2018

Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "AttributeStatus": com.pkj.Clazz#setAttributeSource(1 params) vs com.pkj.Clazz#setAttributeStatus(1 params)


Symptoms :

  1. You are using com.fasterxml.jackson.databind ObjectMapper
  2. By using object of ObjectMapper you are possibly trying Convert Json to POJO
  3. All problem lies in your POJO class
Solution:
  1.  Go to your POJO class i.e. Clazz in my case
  2. checkout the property or variable named "AttributeStatus"
  3. In my case  I had added   @JsonProperty("AttributeSource") on two properties or variables
  4. So corrected that and it worked, meaning I had put correct @JsonProperty on respective variables.
i.e. 

 Before
     @JsonProperty("AttributeSource") 
      private String AttributeSource

     @JsonProperty("AttributeSource") 
      private String AttributeDestination

After

     @JsonProperty("AttributeSource") 
      private String AttributeSource

     @JsonProperty("AttributeDestination") 
      private String AttributeDestination

Help more people:- Share this link, comment if you have any suggestion.