my dog learned polymorphism*
The moose likes Beginning Java and the fly likes why won't this compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why won Watch "why won New topic
Author

why won't this compile?

h slater
Greenhorn

Joined: Nov 22, 2002
Posts: 13
when i try to compile this simple skeleton for a start of a program do I get the follwing error:
MyApp.java [21:1] cannot resolve symbol
symbol : constructor MyCode (MyGUI)
location: class MyCode
mc = new MyCode(mg);
why can I not pass MyGui to MyCode?
thanks
Hugh

public class MyApp{

public MyApp(){

MyCode mc = null;
MyGUI mg = new MyGUI();
mc = new MyCode(mg);

}

public static void main(String args[]){
MyApp ma = new MyApp();

}
}

public class MyCode {

private MyGUI mgl;

public MyCode(MyGUI mg) {
mgl=mg;
}
}

public class MyGUI{

private JButton testButton;
private JButton testButton2;
private JPanel panel1;

public MyGUI() {

JFrame f = new JFrame();

testButton = new JButton("Test...");
testButton2 = new JButton("Test2...");

panel1= new JPanel();
panel1.add(testButton);
panel1.add(testButton2);

f.getContentPane().add(panel1);

f.pack();
f.show();

}
}
MyApp.java [21:1] cannot resolve symbol
symbol : constructor MyCode (MyGUI)
location: class MyCode
mc = new MyCode(mg);
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
What compiler are you using? The standard Sun compiler won't let you put more than one public class in a single .java file.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
h slater
Greenhorn

Joined: Nov 22, 2002
Posts: 13
I didn't state this, but it is complied in 3 seperate java files
Hugh
Andrew Fischer
Greenhorn

Joined: Nov 18, 2002
Posts: 21
Are your 3 java files correctly named? The files must match the name of the public class they contain. Are they all in the same directory?
Are you importing swing in MyGUI.java ?
Don't forget the mandatory order:
package (optional)
import (optional, but you do need swing)
public class


Andrew Fischer
SCJP
h slater
Greenhorn

Joined: Nov 22, 2002
Posts: 13
yes all class names correspond to the file names

the class MyGUI and MyCode complile without any problem.
It is only when I compile MyApp that I have the error message shown at the end of the first post.
Sam Zou
Ranch Hand

Joined: Nov 18, 2002
Posts: 40
Hi,
Try another thing, I notice that your MyGUI class name has GUI in capital letter. Just make sure that the filename of this class is MyGUI.java and not MyGui.java
Let us known if it works or not !!
Yves


Sam<p>"Life is difficult"<br /> -Scott Peck-</p>
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
In what order are you compiling these files?
It looks to me like MyGUI.class must exist before MyCode.java will compile, and MyApp.java requires both MyCode.class and MyGUI.class before it will compile.
So try this:
javac MyGUI.java
javac MyCode.java
javac MyApp.java
I did it with your code (after adding "import javax.swing.*;" to MyGUI.java), and it compiled and ran just fine.

Now, if you REALLY wanna have fun, try putting a reference to MyApp in MyGUI.java!
PCS
[ November 22, 2002: Message edited by: Philip Shanks ]

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
For me, it doesn't matter what order I compile in. In fact, I can just say
javac MyApp.java
and it will automatically compile the other two files for me.
One thing to watch out for: if you have a CLASSPATH defined, you must include "." (the current directory) somewhere in it.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I think the point was to localize the problem.

If you don't include the
import javax.swing.*;
statement, the MyGUI class won't compile.

If the MyGUI doesn't compile, MyApp won't compile either. But if someone compiles them all at once, they may not realize where the problem lies.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
h slater
Greenhorn

Joined: Nov 22, 2002
Posts: 13
mystery solved....
I am using Sun One Studio (Forte) and I had several directories "mounted".
In one of the other directories was another class called MyCode which did not have the same constructor arguments.
The IDE was attempting to compile the code with this class and not the one in the directort that I ws working in. Something to look out for, for the future!
many thanks for all the suggestions
Hugh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why won't this compile?
 
Similar Threads
BorderLayout Problem
JFrame
JPanels with different content should have the same size
unable to remove spaces between buttons(making a calculator)
Error messages