NullPointer exception while calling a SOAP function - java

I am trying the following code to run a SOAP method from a web service.
public de.externalwebservices.worms.aphiav1_0.AphiaRecord[] getAphiaChildrenByID(int aphiaID, int offset, boolean marine_only) throws java.rmi.RemoteException{
if (aphiaNameServicePortType == null)
_initAphiaNameServicePortTypeProxy();
return aphiaNameServicePortType.getAphiaChildrenByID(aphiaID, offset, marine_only);}
That I call in a second class called WORMSWSDAO as:
public AphiaRecord[] getAphiaChildrenByID(int AphiaID){
AphiaRecord[] result = new AphiaRecord[0];
try {
result = port.getAphiaChildrenByID(AphiaID, 1, false);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
Then I run it in the main as follows:
public static void main(String[] args) throws RemoteException {
WORMSWSDAO wDAO = new WORMSWSDAO();
AphiaRecord [] result = wDAO.getAphiaChildrenByID(219275);
System.out.println(result[1].getFamily());
}
but after running that all I get is a null pointer exception..

Related

Thrift Async Client will not stop

I wonder why without the code remarked as 1, even the result is not null, the application keeps running and never stop, and the code remarked as 2 doesn't print the expected result.
But with the code remarked as 1, the application finishes when the result is not null,and prints the code which remarked as 2.
AsyncMethodCallback
private Object response=null;
public Object getResponse() {
return response;
}
#Override
public void onComplete(Object response) {
this.response=response;
}
#Override
public void onError(Exception exception) {
exception.printStackTrace();
}
Async Server
public static void main(String[] args) {
try {
TNonblockingServerSocket serverSocket=new TNonblockingServerSocket(10005);
Hello.Processor<Hello.Iface> processor=new Hello.Processor<>(new HelloServiceImpl());
TNonblockingServer.Args serverArgs=new TNonblockingServer.Args(serverSocket);
TProtocolFactory factory=new TBinaryProtocol.Factory();
serverArgs.transportFactory(new TFramedTransport.Factory());
serverArgs.processor(processor);
serverArgs.protocolFactory(factory);
TServer server=new TNonblockingServer(serverArgs);
System.out.println("server start....");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
Async Client
public static void main(String[] args) {
try {
TAsyncClientManager clientManager=new TAsyncClientManager();
TNonblockingSocket socket=new TNonblockingSocket("localhost",10005);
TProtocolFactory protocolFactory=new TBinaryProtocol.Factory();
Hello.AsyncClient asyncClient=new Hello.AsyncClient(protocolFactory,clientManager,socket);
System.out.println("Client calls....");
MethodCallback<Integer> methodCallback=new MethodCallback<>();
asyncClient.helloInt(14,methodCallback);
Object result=methodCallback.getResponse();
while (result==null){
result=methodCallback.getResponse();
// System.out.println("result is "+result); //1
}
System.out.println(result); //2
} catch (IOException | TException e) {
e.printStackTrace();
}
}

Mismatch of return datatype

i am facing a problem regrading specifying the return data type. I have the FOComp class which implements callabale, the call() method of the 'FOComp' returns data type List<ArrayList<Mat>> as shown in the code of 'FOComp' class below.
and the method 'getResults()' returns data of type ArrayList<Mat> as shown in the code below. and currently, at run time, when I execute the code, I receive the folowing error:
Multiple markers at this line
The return type is incompatible with Callable<ArrayList<Mat>>.call()
The return type is incompatible with Callable<List<Mat>>.call()
kindly please let me know how to fix it.
'FOComp' class:
static class FOComp implements Callable<List<Mat>> {//should return list contains 4 mats(0,45,90,135)
private ArrayList<Mat> gaussianMatList = null;
private List<ArrayList<Mat>> results_4OrientAngles_List = null;
public FOComp(ArrayList<Mat> gaussianMatList) {
// TODO Auto-generated constructor stub
this.gaussianMatList = gaussianMatList;
this.results_4OrientAngles_List = new ArrayList<ArrayList<Mat>>();
}
public List<ArrayList<Mat>> call() throws Exception {
// TODO Auto-generated method stub
try {
featOrient = new FeatOrientation(this.gaussianMatList);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
featOrient.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.results_4OrientAngles_List.add(featOrient.getResults());
return results_4OrientAngles_List;
}
}
'getResults':
public ArrayList<Mat> getResults() {
if (this.crossAddOrientMapsList != null) {
if (!this.crossAddOrientMapsList.isEmpty()) {
if (this.crossAddOrientMapsList.size() == 4) {
double[] theta = new double[4];
theta[0] = 0;
theta[1] = 45;
theta[2] = 90;
theta[3] = 135;
for (int i = 0; i < this.crossAddOrientMapsList.size(); i++) {
MatFactory.writeMat(FilePathUtils.newOutputPath("FinalCrossAdd_" + theta[i]+"_degs"), this.crossAddOrientMapsList.get(i));
//ImageUtils.showMat(this.crossAddOrientMapsList.get(i), "OrientMap_" + theta[i] + " degs");
}
return this.crossAddOrientMapsList;
} else {
Log.WTF(TAG, "getResults", "crossAddOrientMapsList != 4 !!");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is empty.");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is null");
return null;
}
}
class FOComp implements Callable<List<Mat>>
and
public List<ArrayList<Mat>> call()
aren't really compatible... Your call() method should be
#Override public List<Mat> call()
Also, it is good practice to avoid implementation classes in method signatures, use the interfaces instead (in this case, use List rather than ArrayList). That will also fix your problem with one of the "multiple markers" :-)
Cheers,
You class declaration says that you are going to return a List of Mat (FOComp implements Callable<List<Mat>>), but your call method signature says you are going to return a List of ArrayList of Mat (List<ArrayList<Mat>>).
You will need to make them consistent.

NoSuchMethodException loading Build.getRadioVersion() using reflection

I'm trying to load the radio version of the Android device using reflection. I need to do this because my SDK supports back to API 7, but Build.RADIO was added in API 8, and Build.getRadioVersion() was added in API 14.
// This line executes fine, but is deprecated in API 14
String radioVersion = Build.RADIO;
// This line executes fine, but is deprecated in API 14
String radioVersion = (String) Build.class.getField("RADIO").get(null);
// This line executes fine.
String radioVersion = Build.getRadioVersion();
// This line throws a MethodNotFoundException.
Method method = Build.class.getMethod("getRadioVersion", String.class);
// The rest of the attempt to call getRadioVersion().
String radioVersion = method.invoke(null).toString();
I'm probably doing something wrong here. Any ideas?
Try this:
try {
Method getRadioVersion = Build.class.getMethod("getRadioVersion");
if (getRadioVersion != null) {
try {
String version = (String) getRadioVersion.invoke(Build.class);
// Add your implementation here
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.wtf(TAG, "getMethod returned null");
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What Build.getRadioVersion() actually does is return the value of gsm.version.baseband system property. Check Build and TelephonyProperties sources:
static final String PROPERTY_BASEBAND_VERSION = "gsm.version.baseband";
public static String getRadioVersion() {
return SystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION, null);
}
According to AndroidXref this property is available even in API 4. Thus you may get it on any version of Android through SystemProperties using the reflection:
public static String getRadioVersion() {
return getSystemProperty("gsm.version.baseband");
}
// reflection helper methods
static String getSystemProperty(String propName) {
Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties");
Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
return tryInvoke(mtdGet, null, propName);
}
static Class<?> tryClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
static Method tryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) {
try {
return cls.getDeclaredMethod(name, parameterTypes);
} catch (Exception e) {
return null;
}
}
static <T> T tryInvoke(Method m, Object object, Object... args) {
try {
return (T) m.invoke(object, args);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());
} catch (Exception e) {
return null;
}
}

Creating a Custom Exception

I'm trying to create a method f1(x) that throws an exception when x equals 5. After that I will try to call that method from another method f2() to invoke that exception. Then I have to have f2() recover by calling f1(x+1). I tried coding something, but I'm stuck. Here is the code:
public class FiveException extends Exception {
public void f1(int x) throws FiveException {
if (x == 5) {
throw new FiveException();
}
}
public void f2() {
int x = 5;
try {
f1(x);
}catch (FiveException e) {
System.out.println("x is 5");
}
}
public static void main(String[] args) {
FiveException x5 = new FiveException();
x5.f2();
}
}
The print statement works, but I'm not sure how to call f(x+1). Any help on how to fix this and any techniques to write exceptions is appreciated.
Because f1 throws FiveException, wherever you call f1 you must either catch the exception or throw it to the method calling the method that raises the exception. For example:
public static void main(String[] args) throws FiveException {
FiveException x5 = new FiveException();
x5.f1(1);
}
Or:
public static void main(String[] args) {
FiveException x5 = new FiveException();
try {
x5.f1(1);
} catch (FiveException e) {
e.printStackTrace();
}
}
But your code is confusing... normally, it isn't the exception class that throws itself, you have other classes that throw the exception class.
If it's being invoked inside a catch statement, you must surround it with another try-catch, 'cause the code inside catch isn't protected, like this:
public void f2() {
int x = 5;
try {
f1(x);
}catch (FiveException e) {
System.out.println("x is 5");
try {
f1(x + 1);
} catch (FiveException e) {
e.printStackTrace();
}
}
}
But this code is ugly, you can write the following:
public void f2() {
int x = 5;
fProtected(x);
fProtected(x + 1);
}
private void fProtected(int x) {
try {
f1(x);
}catch (FiveException e) {
System.out.println("x is 5");
}
}

Databinding in AXIS2

I have created a web service for the following method using AXIS 1.4
public class SoapTest {
public String test(String param) {
System.out.println("soap activity check "+param);
return param+" return from soap";
}
}
I am calling it using AXIS2 Wsdl2java utility. The client i am using is:
public static void main(String argv[]) {
try {
SoapTestServiceStub obj = new SoapTestServiceStub();
SoapTestServiceStub.Test obj2 = new SoapTestServiceStub.Test();
obj2.setParam("hello");
try {
SoapTestServiceStub.TestResponse res = obj.test(obj2);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
On running the client SOP is working fine but then Getting the following error:
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement testReturn
at org.apache.axis2.AxisFault.makeFault

Categories

Resources