So I have a class with the following constructors:
public TaxiModule(TaxiData taxiData)
{
this(taxiData, VehicleUtils.getDefaultVehicleType());
}
public TaxiModule(TaxiData taxiData, VehicleType vehicleType)
{
this.taxiData = taxiData;
this.vehicleType = vehicleType;
}
When I try to call:
new TaxiModule(taxiData)
Eclipse gives me an error, saying: "The constructor is undefined." When I click on the little light-bulb thingy for the quick fix, it suggests:
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup).
Add argument to match constructor TaxiModule(TaxiData, TaxiConfigGroup, VehicleType).
Clearly from the constructor details my call is valid and the second suggestion does not fit.
Any help would be great.
Thanks!
Related
I'm creating a text adventure game in Java. I've created an exit, room, creature and an item class, and now I'm instantiating and putting values for them in my main class.
I'm getting this problem though: as I'm creating exits for my room, I'm getting a compiler error saying
Constructor Exit in class Exit cannot be applied to given types
This is the line of code I'm getting the compiler error for:
Exit exit1 = new Exit("Exit1", "Exit description", "Exit transition text");
and in my Exit class I have this constructor method:
public void Exit(String exit, String exitDescription, String exitTransition) {
this.Exit(exit, exitDescription, exitTransition);
}
but every time I tell the IDE (Netbeans) to correct the compiler error, it generates this in my Exit class:
Exit(String exit1, String exit_description, String exit_transition_text) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
So my question is, why doesn't my constructor method work? Also, I had help from my professor and I believe he said that I don't want the generated code. Don't remember exactly though.
This is not a constructor:
public void Exit(String exit, String exitDescription, String exitTransition) {
constructors have no return type.
This is a constructor:
public Exit(String exit, String exitDescription, String exitTransition) {
Of course you know that even if this were a valid constructor the code in its body doesn't make much sense as it appears you're trying to have the constructor call itself recursively:
// void removed
public Exit(String exit, String exitDescription, String exitTransition) {
this.Exit(exit, exitDescription, exitTransition); // this calls itself!
}
Class n.1:
void visualizza(){
System.out.println("Testing")
.....
}
Class n2 (JFrame):
label1.setText(obj1.visualizza());
Netbeans tell me error: "void type not allowed here"
Your void visualizza() method doesn't return any String.
System.out.println("Testing") just prints the String Testing on console.
But for setText() method you need to pass a String as parameter.
Most probably this is what you are trying to achieve:
String visualizza() {
return "Testing";
}
Then-
label1.setText(obj1.visualizza());
PS: Please do your own research at least on the basics before referring to stackoverflow. Good luck!
I have 2 classes, cPuzzlePieces and cShapes. cPuzzlePieces extends cShapes.
Right now I get 2 errors on cPuzzlePeaces.
The first error is on the first line of the def and says:
implacet super constructor cShapes is undefined for default constructor.
The second error on the first line of the construter says
constructor call must be the first staemnt
and it is on the first staement.
Here is my code:
public class cPuzzlePieces extends cShapes{ // first error message is here
int mAmountOfShapes;
Context InContext;
void cPuzzlePieces(Context MyContext) throws IOException
{
super( MyContext); // SECOND ERROR MESSAGE IS HERE
InContext=MyContext;
}
}
public class cShapes
{
cShape[] MyShapes;
public int mAmountOfShapes=0;
boolean AnimationRunning=false;
cShapes(Context InContext) throws IOException
{
}
...
...
}
This
void cPuzzlePieces(Context MyContext) throws IOException
is a method, not a constructor.
Remove the void keyword. Add an appropriate access modifier (if need be). Also check for the IOException. Currently, nothing is throwing it.
Related
"Constructor call must be the first statement in a constructor" issue in Java
Java naming conventions state that class names should start with an uppercase alphanumeric character. Please follow that.
As many questions begin, this is driving me crazy.
I have a homegrown StarTeam java library. I have one static method like this:
public static Label getLatestDeploymentLabel(com.starbase.starteam.File child) {
// blah
}
The method works as expected when I call it from java. When I call it from Groovy, I get:
Caught: groovy.lang.MissingMethodException:
No signature of method: static pkg.starteam.StarTeamUtils.getLatestDeploymentLabel()
is applicable for argument types: (com.starbase.starteam.File)
values: [FILENAME-FOO.sql] at starteam.run(starteam.groovy:54)
I put in a println right before I call that method:
chgset.elements().each() { item ->
println "type of item is ${item.class.getName()}"
def latestlabel = StarTeamUtils.getLatestDeploymentLabel(item)
}
And confirm that, in fact, it's iterating what I expect it's iterating over:
type of item is com.starbase.starteam.File
I've seen a few different similar issues in other posts relating to static methods and the responses are along the lines of "are you sure it's a static method?". I'm sure it's a static method.
There isn't much groovy code to this. What there is of it is all contained in a single script in the default package. The main method is then called implicitly and it's in the body of the script class that the call out to the java library is made. I set the classpath in a DOS batch wrapper script, e.g.:
SET INITIALCLASSPATH=%CLASSPATH%
SET NEWCP=c:/libs/etc.jar;c:/etc/etc.jar
SET GROOVYPATH=c:/groovy.bat
SET CLASSPATH=%NEWCP%
%GROOVYPATH% %*
SET CLASSPATH=%INITIALCLASSPATH%
I created a simple situation which I think emulates my situation.
C:\apps\groovy-1.8.6\scripts>type Other.java
class Other {
private String name = "notset";
public Other(String name) {
this.name = name;
System.out.println("Created an other");
}
public String toString() {
return name;
}
}
C:\apps\groovy-1.8.6\scripts>type ThingList.java
import java.util.ArrayList;
import java.util.Iterator;
class ThingList {
ArrayList ourlist = new ArrayList<Other>();
public ThingList(){}
public ArrayList add(Other thing) {
ourlist.add(thing);
return ourlist;
}
public Iterator iterator(){
return ourlist.iterator();
}
}
C:\apps\groovy-1.8.6\scripts>type JavaLib.java
class JavaLib {
public JavaLib() {}
public static ThingList getThingList(Other thing) {
ThingList tl = new ThingList();
Other one = new Other("extra one");
tl.add(thing);
tl.add(one);
return ThingList;
}
}
C:\apps\groovy-1.8.6\scripts>type testthing.groovy
def myOther = new Other("A new other")
println "type of myOther is ${myOther.class.getName()}"
def myList = getThingList(myOther)
myList.each() {
println it
}
C:\apps\groovy-1.8.6\scripts>type wrapper.bat
#ECHO OFF
SET INITIALCLASSPATH=%CLASSPATH%
SET GROOVY=C:\apps\groovy-1.8.6\bin\groovy.bat
SET CP=.
SET CLASSPATH=%CP%
%GROOVY% %*
SET CLASSPATH=%INITIALCLASSPATH%
C:\apps\groovy-1.8.6\scripts>wrapper.bat testthing.groovy
Created an other
type of myOther is Other
Caught: groovy.lang.MissingMethodException: No signature of method: testthing.ge
tThingList() is applicable for argument types: (Other) values: [A new other]
groovy.lang.MissingMethodException: No signature of method: testthing.getThingLi
st() is applicable for argument types: (Other) values: [A new other]
at testthing.run(testthing.groovy:3)
C:\apps\groovy-1.8.6\scripts>
Any insights or suggestions would be greatly appreciated!
AndyJ
Without a way to reproduce, it's impossible to say for sure what the problem is. One possibility is that it is a class loading problem. Is the Groovy code contained in a regular Groovy class that's sitting on the class path, or does the Groovy code get loaded dynamically (e.g. by using GroovyShell)?
I have this constructor;
public UmlDiagramEntity(ReportElement reportElement, int pageIndex, Controller controller) {
super(reportElement.getX1(), reportElement.getY1(), reportElement.getX2(), reportElement.getY2());
setLayout(null);
this.pageIndex = pageIndex;
this.controller = controller;
reportElements = reportElement.getInternalReportElements();
components = new ArrayList<AbstractEntity>();
changedComponentIndex = -1;
PageListener p = new PageListener();
this.addMouseMotionListener(p);
this.addMouseListener(p);
setPage();
}
And I have an update method in the same class;
#Override
public void update(ReportElement reportElement) {
if (changedComponentIndex == -1) {
super.update(reportElement);
} else {
reportElements = reportElement.getInternalReportElements();
if (components.size() == reportElements.size()) {
if (!isCommitted) {
if (reportElement.getType() == ReportElementType.UmlRelation) {
if (checkInvolvementAndSet(changedComponentIndex)) {
anchorEntity(changedComponentIndex);
} else {
resistChanges(changedComponentIndex);
}
return;
}
}
..................goes on
When I follow the flow from the debugger, I see that when update is called, somewhere in the method, the program goes into the constructor and executes it all over again (super, pageIndex, etc.). Why does it go to the constructor :D I didn't tell it to go there.
I can make a deeper analysis and see where it goes to the constructor if you want. By the way, changedComponentIndex is a static variable.
I would find it far more probable that you are seeing it construct two different objects. You'd have to provide more information like a stack trace; here you haven't even shown the constructor being invoked!
The behaviour you describe is pretty much impossible. Either your code is different from what you've shown or you're not debugging the code you think you're debugging. Without complete code that we can run, that's all we can say.
Are you sure that update is not called indirectly from within the constructor, which would result in a breakpoint in update getting triggered.
Try setting a breakpoint at the start of the constructor and at the end, then one in update. When you hit the first constructor breakpoint, hit 'continue' and see which breakpoint gets triggered next.
Is this multi-threaded? Is it possible that the constructor for a different instance created on another thread is being called?