Why can't Java find this constructor? [closed]
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this questionpackage package.b;
class ClassB {
public ClassB(BaseClass bc, XMLBase obj1) { }
}
import package.b.ClassB;
class A extends BaseClass {
public void function() {
TestXML obj1 = new TestXML();
ClassB bObj = new ClassB(this, obj1);
}
}
When I compile the above code, I get an error "cannot find symbol symbol : constructor ClassB(ClassA, Object1)"
But when I pass "null" for both arguments, it compiles fine.
Why so?
Can anyone help me?
TIA
Your error message doesn't match the constructor call you've shown. You've got:
// In the code
public ClassB(BaseClass bc, XMLBase obj1)
// In the error message
ClassB(ClassA, Object1)
What is Object1
, what's XMLBase
, and what's TestXML
?
Additionally there's the invalid package name as Vinegar pointed out. If you could produce a short but complete example which is failing, that would help. At the moment it looks like you're using cut down versions of a few classes but naming them inconsistently, which makes it much harder to work out what's going on.
Also, is ClassB
public? It's not in your sample code, but you're importing it which presumably means it's in a different package... that won't work, as the default access is restricted to code in the same package. Given your comment about it working if you pass in nulls, I suspect it is public (or the import is irrelevant) - again, a complete example would really help.
Both classes are in different packages and have the (default) package visibility, hence they can't see each other. Make B public and it should be fine:
public class ClassB {
//blabla
}
This should compile unless there are other errors e.g. TestXML is not a valid class or does not extend XMLBase which you say it does. Without the complete class defn it cannot be helped.
精彩评论