How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Eric M. Your Own Question

Eric M.
Eric M., Tutor
Category: Homework
Satisfied Customers: 2176
Experience:  9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Type Your Homework Question Here...
Eric M. is online now
A new question is answered every 9 seconds

Hello, Im having a problem developing a simple java program

Resolved Question:

Hello, I'm having a problem developing a simple java program and I need help.

I especially need an expert who can explain things in plain english rather than just technical language.

The program so far consists of 2 classes, which I have called simply Class1.java and Class2.java

Class1 should create a window on the screen with a label and a button.
The button has an actionlistener.

The idea is when the button is clicked, the computer should refer to Class2 and use the method which I have called class2window, which simply generates another window.

This is obviously just practise of using multiple classes.

The problem is when I try to compile Class1 (using Command Prompt) I get error messages saying "cannot find symbol" when I've tried to create an object of Class2.

It also tells me the method newmethod needs to be declared final but I may be doing something wrong there as well.

The code for the 2 classes can be viewed by using the following links;

I hope someone can help.

http://yourhomeentertainment.webs.com/Class1.html
http://yourhomeentertainment.webs.com/Class2.html

Yours faithfully
James McCarty
Submitted: 1 year ago.
Category: Homework
Expert:  Eric M. replied 1 year ago.
Hi!

I'll be happy to help. I've downloaded your code and run it on my local computer, and there are two things to fix.

The first thing is that in order for the two classes to "see" each other, they need to be placed in the same "package". A "package" is basically a group of java files -- everything in a package can see everything else in the package. Right now, these classes aren't in the same package, so they can't see each other. That's why it says "Cannot Find Symbol" in Class1 when you try to create an object of Class2 -- it doesn't "see" Class2 because they aren't in the same package.

To fix this, just add this to the very top of each file:

package MyPackage;

Really, you can change "MyPackage" to almost anything else, as long as it's the same in both files. Then, just make sure you compile Class2 before trying to compile and run Class1. That should resolve the 'cannot find symbol' problem.

The second problem that arises is regarding the following line in Class1:

activate.addActionListener(new ActionListener(newmethod)

I was able to get the program to work by replacing that line with:

activate.addActionListener(new ActionListener()

The nuts and bolts of why that is necessary are a bit complex, but in a nutshell, when you're creating a new method while you're passing it like that, you can't pass it parameters.

I've uploaded my working code below. Note that for mine, I used your username on this site as the package name. You can change that to anything.

Class1
Class2

Thanks! If this answer is acceptable, please remember to click 'Accept' or give positive feedback when prompted. If you need further assistance before this is deemed acceptable, please don't hesitate to ask for more detail. Thanks again!
Customer: replied 1 year ago.


Hello again, sorry to bother you.


 


The plot thickens!


I downloaded the Class1 and Class2 files you adjusted, however I'm still getting the same error message about cannot find symbol.


 


However, I tried removing the package line and they worked! They are stored in the same directory and, as explained in a previous reply, the classpath automatically looks in the current directory.


 


So the question is, why isn't the package line working, yet without it, the 2 files recognise each other fine (now that the classpath is correct)?


 


Sorry again, I hope you can resolve this, you've still done a good job.


James


 

Expert:  Eric M. replied 1 year ago.
Interesting! So just to be sure I understand, the program actually runs for you now and behaves as anticipated, but only when the package line is gone? (Just want to make sure that the program itself is working, that's priority #1!)

My inclination, then, is to say that something about the way in which Java is compiling the code is basically imbuing it with its own classpath based on the folder structure. Basically, I imagine Java is assuming that all classes in the same folder are in the same package, and is adding in a line after the fact to reflect that. Then, when it sees the current package line, it freaks out because a class can't be in more than one package.

That's partially speculation, though. The difficult thing about packages is that most compilers or development environments handle them themselves, and so it differs significantly based on your programming environment. I, for example, use NetBeans, which doesn't know what to do at all if you don't include a package line. In the past, though, I used CodeWarrior, which behaved more like what you're describing: it didn't know what to do if you did include a package line.

Generally, though, the package lines are meant to reflect the folder structure of the code, so in your case, if they're all in the same folder, a package line shouldn't be necessary. My initial response actually wasn't going to include that part (and only describe the second issue, with the parameter), but I couldn't find a way that the parameter issue could have caused the 'cannot find symbol' error. But hey, if it works now, that's all that matters!
Customer: replied 1 year ago.


Hello again,


 


Thank you for being so positive!


I already replied to you before, but it looks like there was a problem with the website because you don't seem to have received it.


 


Put simply, the problem was caused by the classpath - after I first installed java I found when I used Command Prompt and typed javac to compile the program I'd created, it compiled no problem.


But then when I tried to run it by typing "java" followed by the file name, I got an error message saying it could not find the file.


 


This problem I later discovered was with the classpath in environmental variables - it wasn't telling the computer to look in the current directory. I avoided this problem by typing "java -classpath . " followed by the filename and it worked!


 


However, I only had this problem on my laptop.


When I tried it on my main PC, both javac and java worked well with no extra instruction.


 


So tonight I realised when I tried to compile Class1 on my laptop, it was looking for a compiled file called "Class2" but it wasn't looking in the current directory.


 


So I tried copying both files to my main PC and tried compiling them from there and it worked! With the exception of the other error you mentioned with the actionlistener.


 


So I think I should ask you to permanently fix this problem - how can I adjust the classpath on my laptop so it runs like my main PC without needing any further instruction?


 


I hope this isn't too much hassle,


I look forward to hearing from you.


James


 

Expert:  Eric M. replied 1 year ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.
Eric M., Tutor
Category: Homework
Satisfied Customers: 2176
Experience: 9 years of tutoring experience, Bachelor's and Master's in Computing, pursuing PhD
Eric M. and 2 other Homework Specialists are ready to help you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4520
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3121
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3035
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/lanis/2009-4-1_233717_phput9xef_c1pm.jpg Lani S.'s Avatar

    Lani S.

    Tutor

    Satisfied Customers:

    2457
    Registered Nurse, Internet Researcher, Private Tutor
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2341
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1975
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1873
    Bachelors Degree and CPA with Accounting work experience