Re: java:143: cannot find symbol -----Please help me

From: Andrew Thompson (SeeMySites_at_www.invalid)
Date: 05/26/04


Date: Wed, 26 May 2004 08:41:14 GMT

On Wed, 26 May 2004 16:04:20 +0800, Danny wrote:

I have an answer for you, but first, some comments..

You need to have a close look over this
<http://www.physci.org/codes/sscce.jsp>

It explains how you might have represented this
problem in 91 lines, rather than 391

> import java.lang.*;

..no need to import this

> class MyFrame extends JFrame

Terrible name, it sounds like the 'I, me, mine..'
phase of toddlers a class name should tell basically
what it is, so, for instance..
'RetailServerFrame'

> {
> JFrame frame;
> JButton buttonStart;
> JButton buttonExit;
> String userInput;
> String serverName;
> String userName;
> String password;

These are 'class level' attributes, anything
in the class has access to them.

> public static void main(String args[])
> {
> MyFrame f = new MyFrame();
> }
>
> public MyFrame()
> {
> super("Window Title");
>
> JLabel labelSpace;
> JLabel labelTitle;
> JLabel labelServername;

Whereas this is a local variable declared within
the constructor, nothing outside the constructor
can access it.

> ....
> class ButtonListener implements ActionListener
> {
>
> public void actionPerformed(ActionEvent evt)
> {
> JButton source = (JButton)evt.getSource();
> serverName = textServername.getText();
> System.out.println(serverName);
> }
>
> }
> }

So by this stage, the textServerName label is
'out of scope', or 'not visible'.

Solution, declare it as a class level
attribute, and the code will compile.

-- 
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology


Relevant Pages

  • Re: Whats wrong with this code?
    ... The class below is what is references whenever we need to grab the user data. ... For instance, when the user saves a database record, we attach their username to the record by requesting SecureUsers.su_strUser ... I'm assume this is because these variables are all 'shared', as 'shared' is at the class level rather than instance level, correct? ... Public Shared su_strEmail As String ...
    (microsoft.public.dotnet.framework.aspnet)
  • An object reference is required for the nonstatic field, method, or property
    ... Before I put folowing variable in class level, ... string[] data; ... currentLine = sr.ReadLine; ... string mySelectQuery = "SELECT FundID,Ticker, PeriodicityID FROM ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: property decorator and inheritance
    ... the class level. ... To evaluate to a string (which it will if it's a property as you ... myObj.greeting2# Parentheses ... outside functions accessing them don't "know" that a getter function is ...
    (comp.lang.python)
  • Re: newbe question!
    ... I assume that you mean a string, however that can be as long as you wish. ... It depends with what you fill that what the maximum string is. ... The most simple Array is simple ... How do i do this at a class level in VB.NET? ...
    (microsoft.public.dotnet.languages.vb)