launch(ProjectTaskManagementApp.class, args); ERROR - java

With the following code:
launch(ProjectTaskManagementApp.class, args);
There is the error:
The method launch(Class, String[]) is undefined for the
type ProjectTaskManagementApp
Why is it doing this? Could anyone help resolve the issue.
Thanks!

It's doing that because there's no method launch that takes a Class object and an array of String as arguments in the class ProjectTaskManagementApp. The method you're thinking of may have a different name, or may take different arguments, or it may simply not be there.
Based on the fact that you're calling launch without an object reference, it looks like this call is actually inside the definition of the class ProjectTaskManagementApp. Do you mean to be calling a launch method defined in this class, or it it defined in some other class? Do you mean to be calling launch via an object?

Simply put, the class: ProjectTaskManagementApp does not contain the method with the exact parameters you are calling it with, or does not exist at all.

Related

Java Reflection ArrayList.class.getMethod("get") returns NoSuchMethodException

Why can't I get the "get" method of an ArrayList and invoke it?
I am using reflection to modify within my nested classes. One of my classes has a list of classes so I wanted to be able to use the same logic to get and invoke the get method.
simplified, the line that fails is
ArrayList.class.getClass().getMethod("get")
and it fails, giving me a NoSuchMethodException.
I understand that I could use aList.get() but that's not the point, I need to use reflection since this is in a deeply nested class.
TL;DR
How can I get the "get" method of an array list?
Note that Class#getMethod() has two parameters: a String and an vararg of Class objects. The former is
the list of parameters
that the method declares.
You need to use
ArrayList.class.getMethod("get", int.class);
since the ArrayList#get(int) method has an int parameter.
I initially missed the whole
ArrayList.class.getClass().getMethod("get")
^ ^
| |----------------------------- gets Class<Class>
|----------------------------------- gets Class<ArrayList>
The .class already gets the Class instance for ArrayList. Calling getClass on that will return the Class instance for class Class. You don't want that.
Method methods = ArrayList.class.getMethod("get", int.class);
You do not need to call getClass() method again after .class because when you write .class after a class name, it references the Class object that represents the given class.

Calling instance method in Java

Following this tutorial:
http://developer.android.com/training/notepad/notepad-ex2.html
In Step 2, this method gets called:
registerForContextMenu(getListView());
which is a public method of Activity. Now, I'm a bit of a Java newbie here - I thought if you wanted to call an instance method of a superclass you needed to preface it with this. E.g.
this.registerForContextMenu(getListView());
Is it just a style thing here? Is there any difference between
this.registerForContextMenu
and simply
registerForContextMenu
No, there is no difference.
You don't have to use this., but it is often done anyway to make the code clearer.
For one thing, it makes it easy to tell if a method is static or not if you use the convention of calling instance methods like this:
this.registerForContextMenu() 
and static methods like this:
ClassName.staticRegisterForContextMenu()
you do not have to use this. If you ommit it it is assumed you called method in this scope. One particular example when this may help could be i.e.:
Boolean someVar;
public function setMe( Boolean someVar ) {
this.someVar = someVar;
}
In this case, w/o this you would get the error.
To call a method of superclass either you need object of superclss or keyword super .
eg.
superObject.superclassMethod();
super.superclassMethod();
this is a reference of the current object. this can be used to call method of a class in which it is used. this can never be used to call a superclass method.
As for
this.registerForContextMenu()
and
registerForContextMenu()
no such difference. you can use either of them.
Both ways are correct for calling a method on the current (this) instance of the class. Non private methods are inherited from super classes, so you can use the same syntax to call such methods.

Call a method from another class java

I have multiple classes, and am attempting to call a method in a different class.
This should work, but it gives errors:
TheMethods method = new TheMethods();
Java tells me that the constructor TheMethods() is undefined.
What I am doing wrong?
Edit:
I needed to pass a reference to my main class and initialize it.
In your constructor you have a parameter MCTag m. If you are going to use this constructor you would need to do it like this:
TheMethods method = new TheMethods(MCTag m);
Constructors are like any other method and have to have the correct parameters in order for it to work correctly.
EDIT:
It would look something like this:
MCTag myTag;
TheMethods method = new TheMethods(myTag);
method.selectPlayer();
The only constructor you provide on TheMethods is
public TheMethods(MCTag m) {...
Which takes an MCTag parameter, and you are attempting to call a no-arg constructor which, as the compiler is telling you, does not exist.
Add the default constructor with no args
public TheMethods() {
}
the only ctor i see in your code is:
public TheMethods(MCTag m)
but you don't pass any values in your sample code here
there realy isnt any TheMethods() ctor defined
to reference a public method in another class, call
Class.Method();

Method showing up in getDeclaredMethods(), but can't be found with getDeclaredMethod(), why?

I am trying to use the snippet:
GenericModel.class.getDeclaredMethod("findById");
to get a Method called "findById". I know the method exists, because when I call:
GenericModel.class.getDeclaredMethods();
the method is listed in the array returned.
However, when using the first snippet, I am getting a java.lang.NoSuchMethodException? Why?
Presumably findById actually takes parameters. But you are searching for a method by that name that takes none. Most likely what you want is:
GenericModel.class.getDeclaredMethod("findById", new Class[] { int.class });
This will match a method that has a signature like this:
Object findById(int id) { ... }
getDeclaredMethod() receives parameter types as well, and you didn't give it any, and in the case of findViewById, it's a method that receives an int as parameter.
Judging by the name, findById takes arguments. However, you're not passing any in. You need to specify the findById's arguments as additional parameters for the call to getDeclaredMethod so it knows which method to give you - there could be ten different findById's.
In my project there are several ClassLoaders.
The classes of the method arguments are the same, but the ClassLoaders (or instances of ClassLoaders) that load those classes are different.
For example, ClassA has method methodA with argument argumentA typed com.example.A, loaded by class loader ClassLoaderA.
However, in your java CLASSPATH, you have com.example.A, loaded by class loader ClassLoaderB. When you call ClassA.class.getDeclaredMethod("methodA", new Class[] { A.class });, it will still fail to find the method. Because com.example.A loaded by ClassLoaderA is not the same as com.example.A loaded by ClassLoaderB.
The parent-delegation model / singleton pattern for custom class loaders can be used to solve the problem.
I know it is not related to this case, but it is one possible reason why Method showing up in getDeclaredMethods(), but can't be found with getDeclaredMethod().

Invoking dynamic Method without known Constructor?

If I do this in Java to call a method name from a class dynamically, it works.
MainApp app = new MainApp();
Method meth = app.getClass().getMethod("myMethod", MyParameterType.class);
//call method
meth.invoke(app, new MyParameterType("hello"));
But this worked because I know the constructor in the invoke method. But if I were to pass the Method object as a parameter to some other classes, and I don't know who is the constructor, I cannot invoke the method any more. Even if I know, I may not want to create a different object to just make a call to the method. For eg:
//This is in the class call MainApp.java.
//There is a method in MainApp.java that looks this way: myMethod(MyParameterType param);
MainApp app = new MainApp();
OtherClass myClass = new OtherClass();
Method meth = app.getClass().getMethod("myMethod", MyParameterType.class);
myClass.callMe(meth);
//Inside OtherClass.java
public void callMe(Method meth) {
//call method
meth.invoke(########, new MyParameterType("hello"));
}
In this case, what should I put for the ######## parameter? Within the context of OtherClass.java, the base constructor object wouldn't be known. And why would I need if since meth is already a Method type that I just call like a function?
Thanks
Assuming it's an instance method, you've got to have an instance to call the method on, just like anything else. How you get hold of that instance will depend on what you're trying to do; you could pass in a Constructor, or a reference to an existing object, or some interface which will create the instance when you ask it to... we can't really give you any advice on which approach is the most suitable without knowing what you're trying to do.
If it's a static method, you can pass null for the first argument.
What it seems you are looking for or thinking about is the concept of `lambda functions``. Those can be called in isolation.
A Method type is not a standalone method, but more like a 'path' into an object. Compare this with a relative URL like /subscribe.html. Out of context this is pretty useless, but when bundled with a site like www.example.com it makes sense.
As such, Method can only be used in combination with an instance. (edit: as John mentioned, unless it's a static method of course which do not need instances)
If you can safely invoke a method without providing an instance, it should be a static method, in which case any instance provided is ignored, you can give it null.
If you have to provide an instance of the object, there is no way around this.
If the developer who write the method has labelled it non-static incorrectly, I suggest you discuss with them why they did it.

Categories