Learning the JAVA,but i'm in trouble - java

I start learning the JAVA,and I thought that it's more simple than C and more difficult than C#,so I have builded a project which using the JERSEY framework,and there some errors,would you like to help me fixed that errors?
here is my code
https://github.com/abcdef123ghi/jerseytest
I have 5 errors,I dont know what's diffrenet with JAVA & C#,I'm just triing to write a JAVA project,the error is
Description Resource Path Location Type
The method checkAuth(String, String) is undefined for the type myresource myresource.java /slwebservice/src/service line 40 Java Problem
Description Resource Path Location Type
The method inserttherecord(String, List<invdatas>) is undefined for the type myresource myresource.java /slwebservice/src/service line 43 Java Problem
Description Resource Path Location Type
The operator == is undefined for the argument type(s) int, null myresource.java /slwebservice/src/service line 31 Java Problem
Description Resource Path Location Type
This method must return a result of type int invdataManager.java /slwebservice/src/domain line 88 Java Problem
Description Resource Path Location Type
Type mismatch: cannot convert from java.util.Date to java.sql.Date invdataManager.java /slwebservice/src/domain line 223 Java Problem
thanks
regards
Ken

There are few compile errors in your code. I think you better follow some java tutorials and check the code again. Anyway I'll point out the errors and the solutions.
The method checkAuth(String, String) is undefined : There is no checkAuth method in your myresource.java. Please add it.
The method inserttherecord(String, List) is undefined : Same as above
The operator == is undefined for the argument type(s) int, null : int is a primitive value. So the default value for int is 0. If you used Integer instead of int, then your operation is correct. So you need to change the code as this.ivm.getRecordcount()==0
This method must return a result of type int : The method public int inserttherecord(List<invdatas> invdatalist,String clino) should return a int value. Your code does that but inside conditions and blocks. The idea is you should return a int value not you may return it. Al least put return 0; to the end of the method.
Type mismatch: cannot convert from java.util.Date to java.sql.Date : Here your method private Date parseDate(String date) should return java.sql.Date object But you return return format.parse(date);. This will return a java.util.Date. I think the return type of the method should be java.util.Date. It seems you import wrong class. Just import java.util.Date instead of java.sql.Date.
Hope this will help you. Thanks

Related

XSL: Cannot convert argument/return type in call to method

I got XSL file which i'm trying to generate over my app.
The code goes like this:
TransformerFactory factory = TransformerFactory.newInstance();
Source styleSheetLoc = new ResourceSource(styleSheetLocation);
Templates t = factory.newTemplates(styleSheetLoc); // <<< throwing the Exception
return t.newTransformer();
The exception is:
ERROR: 'Cannot find external method 'com.am.caretalks.util.XsltUtils.getResourceString' (must be public).'
FATAL ERROR: 'Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)''
javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)'
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:990)
at com.am.caretalks.sendreport.CreatePDF.buildTransformer(CreatePDF.java:1147)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:608)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:586)
at com.am.caretalks.admin.EngagementReportExportServiceImpl.exportAndUpload(EngagementReportExportServiceImpl.java:131)
at com.am.caretalks.admin.EngagementReportExportServiceImpl$$FastClassBySpringCGLIB$$38327504.invoke(<generated>)
EDIT:
This is the called function from the XSL file
public static String getResourceString(final String messageKey, final String locale, final String customerIndex) {
.
.
}
I searched the web and I found a solution that tells me to use only objects (not primitives) while calling to a function from my XSL file to my java code and due to that i'm getting these error, but nothing did helped me here.
The big issue here that I don't understand what is the error and how to solve it, what do I need to do according the thrown Exception?
Any other suggestions to fix this issue are welcome
The error message suggests that you are passing inappropriate arguments to the function. It appears the method expects (string, string, string) and you are passing (node-set, node-set, int). I don't know Xalan well, but try doing an explicit conversion of the supplied arguments to the required type by calling number() or string().

Error in ColdFusion Javacast array to class from loadPaths

I am trying to use Elasticsearch's Java API.
I am trying to create a RestClientBuilder.
Host=createObject("java", "org.apache.http.HttpHost").init(variables.HostName, variables.Port);
Node=createObject("java", "org.elasticsearch.client.Node").init(Host);
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.elasticsearch.client.Node[]", [Node])).build();
I get the error
Cannot convert the value to Java array because type org.elasticsearch.client.Node is unknown.
Also if I just try to use:
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.apache.http.HttpHost[]", [Host]));
I get the following error
Either there are no methods with the specified method name and
argument types or the builder method is overloaded with argument types
that ColdFusion cannot decipher reliably. ColdFusion found 0 methods
that match the provided arguments. If this is a Java object and you
verified that the method exists, use the javacast function to reduce
ambiguity.
This I assume is because ColdFusion doesn't play nicely with varargs
I found a workaround using this method
https://www.bennadel.com/blog/1980-tojava---a-coldfusion-user-defined-function-for-complex-java-casting.htm
I believe there is a bug with Javacast and javaSettings loadPaths not being used.
coldfusion.runtime.Cast$UnknownTypeException: Cannot convert the value
to Java array because type org.elasticsearch.client.Node is unknown.
at coldfusion.runtime.Cast.toJavaArray(Cast.java:1602)
Additionally if I try to perform the actiuons that the UDF takes
local.javaClass = createObject("java", "org.apache.http.HttpHost");
local.HostArrayReflect = createObject("java", "java.lang.reflect.Array");
local.HostArray = local.HostArrayReflect.newInstance(
local.javaClass.GetClass()
, JavaCast( "int", ArrayLen(local.Hosts))
);
for (i=0; i LT ArrayLen(local.Hosts); i=i+1) {
local.HostArrayReflect.Set(local.HostArray, JavaCast("int", i), local.Hosts[i]);
}
I get the error
An exception occurred while instantiating a Java object. The class
must not be an interface or an abstract class. If the class has a
constructor that accepts an argument, you must call the constructor
explicitly using the init(args) method. Error :
org.apache.http.HttpHost
java.lang.NoSuchMethodException: org.apache.http.HttpHost.() at
java.lang.Class.getConstructor0(Class.java:3082) at
java.lang.Class.newInstance(Class.java:412) at
coldfusion.runtime.java.JavaProxy.createObjectWithDefaultConstructor(JavaProxy.java:209)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:92)
This happens when I try to run getClass(), but in the UDF there is no issue. A coworker tried to run this on Lucee and it seems to have worked, so I believe there is a bug in CF related to this.

MVEL expression : java.lang.ClassCastException: [B cannot be cast to java.lang.Object

I am writing few MVEL expression "$input.get('RESULT').array()[1]" where $input.get('RESULT') return a ByteBuffer, so I am converting it into array using array() and then trying to get value of index 1. where array have 16 attribute.
But when Mvel executing this expression I am getting
java.lang.ClassCastException: [B cannot be cast to java.lang.Object
I am using below code
MVEL.executeExpression(statement, fieldContext.getContext(), fieldContext.getVariables());
I have written other expression as well which is working fine, I am getting this error for Byte one.
Any help here will be appreciated.
Update :
When I use ByteBuffer get method I got below error so I thought to give a try with array.
java.lang.IllegalAccessError: tried to access class java.nio.HeapByteBuffer from class ASMAccessorImpl_2330495751530608772190
at ASMAccessorImpl_2330495751530608772190.getValue(Unknown Source)
at org.mvel2.optimizers.dynamic.DynamicGetAccessor.getValue(DynamicGetAccessor.java:79)
at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:108)
at org.mvel2.MVELRuntime.execute(MVELRuntime.java:86)
at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
at org.mvel2.MVEL.executeExpression(MVEL.java:922)`
You don't have to convert to byte[] with array(), it should be enough to
$input.get('RESULT').get(1)
as per ByteBuffer.get() method docs:
public abstract byte get​(int index)
Absolute get method. Reads the byte at the given index.

Illegal start of expression inside if statement using objects

I'm having a very frustrating issue at the moment and perhaps the answer lies here?
I'm currently having an issue with if statements.
I want my core.java class to contain an if statement which closes the entire program if my variable counter reaches 2.
private int counter = 0;
//located in the class Ending
I implemented that using a seperate method addCounter()
which goes as
public void addCounter(){
this.counter ++;
}
//this will be called in core.java
I also have a getter which is supposed to return the value of counter
public int getCounter(){
return counter;
}
//this will be called in core.java
Decleration of changeState in core.java
Ending changeState = new Ending();
//(As per request)
The real issue is described here:
I can't seem to come up with a fitting if statement which checks if the method getCounter has reached '2' after addCounter();has been invoked several times
My first idea was to use something such as
if(changeState.getCounter().equals(2)){
System.exit(0);
}
//I also tried using:
if(changeState.getCounter() == 2)
//however, that didn't work either
both lines give me numerous errors which I can't wrap my head around:
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: <identifier> expected: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: illegal start of type: if(changeState.getCounter().equals(2)){
.java:476: error: ';' expected: if(changeState.getCounter().equals(2)){
Could anyone elaborate on what is going wrong and what should be done to overcome this issue?
Thank you in advance!
C.C.
.equals(2) is incorrect , the 2 in the equals method is a primitive type int literal not an Object or String type.
.equals() method uses either a type "String"
counter.equals("2")
or it uses a type "object" to compare
.equals(((Object)new String("2")))
If you must use .equals() method then it would be
if(counter.getCounter().equals(new Integer(2).toString())){
System.exit(0);
}
Although that really should be simpler such as
if(counter.getCounter() == 2){
System.exit(0);
}
My answer lied here all along.
If anyone else is having similar kind of issue, it seems that you simply can't invoke an object inside a class unless it's in a method.
I admit that this does solve my issue entirely, but it did show me a valuable lesson.
Good luck!

AST eclipse, adding arguments to a MethodInvocation

Im trying to add arguments to the arguments list of a MethodInvocation and it doesnt seem to work, I can remove objects but I cant see to add them.
My end goal is to take 2 MethodInvocation that invoke the same method with different arguments and convert it to 1 MethodInvocation that has a ConditionalExpression as an argument.
Example:
if (A){
System.out.println("hi");
} else {
System.out.println("hey");
}
Will be converted to:
System.out.println((A ? "hi" : "hey"));
So I would also appreciate it if someone knwos how to convert the argument list to 1 big Expression I can place in the ConditionalExpression.
Thanks!
EDIT: sorry forgot to mention is it a code formatting plug-in for ecplise
EDIT2: the code I am trying to run:
final ExpressionStatement thenStmnt=(ExpressionStatement)((Block)node.getThenStatement()).statements().get(0),
elseStmnt=(ExpressionStatement)((Block)node.getElseStatement()).statements().get(0);
MethodInvocation thenMethod=(MethodInvocation)thenStmnt.getExpression(),
elseMethod=(MethodInvocation)elseStmnt.getExpression();
final MethodInvocation method=ast.newMethodInvocation();
method.setName(ast.newSimpleName("add"));
method.arguments().add(0, elseMethod.arguments().get(0));
ast is a given leagal AST and node is a given leagal IfStatement.
Solved, problem was here:
method.arguments().add(0, elseMethod.arguments().get(0));
If you want to take or copy something that is already part of your original code, meaning already exist in the AST you have to use r.createCopyTarget, like so:
method.arguments().add(0, r.createCopyTarget(elseMethod.arguments().get(0)));

Categories

Resources