Results 1 to 6 of 6
  1. #1
    stesteve is offline Member
    Join Date
    Feb 2012
    Location
    Chandler Arizona
    Posts
    3
    Rep Power
    0

    Default Symbol not found - object invisible outside of class

    I removed a lot of my code to isolate the problem. The error message says "newObject" inside tryThis() is not found. What do I need to do to make newObject visible?

    package newpackage;

    public class NewClass {

    public NewClass() {
    }

    public void newMethod() {
    }

    public static void main(String args[]) {
    NewClass newObject = new NewClass();
    OtherClass otherObject = new OtherClass();
    }
    }

    class OtherClass {
    public void OtherClass() {
    }

    public void tryThis() {
    newObject.newMethod();
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Symbol not found - object invisible outside of class

    newObject is only visible inside of the scope with which it was declared in -- the main method.

    Perhaps you might want to tell us what it is exactly you are trying to do as some of these details may be important for us to be able to better guide you to a solution.

  3. #3
    stesteve is offline Member
    Join Date
    Feb 2012
    Location
    Chandler Arizona
    Posts
    3
    Rep Power
    0

    Default Re: Symbol not found - object invisible outside of class

    I am diving into a large project as a way to learn the language, without much hope that it will ever be finished. It includes a fairly complex GUI (using Netbeans) and concurrent threads. Of course the GUI design generated more code than I wanted to paste here so I abbreviated it. In my posted code, NewClass represents the GUI class (extends javax.swing.JFrame) and OtherClass represents an extension to the Thread class. I wanted one of the GUI events in newObject to start the thread in otherObject and I wanted one of the methods in otherObject to update a text field in newObject. I tried several other arrangements in attempt to eliminate the errors - including making the thread class an inner class to the GUI class and declaring newObject outside of main, but it feels like I am just being random, and I can't find any documentation that helps. I could try to paste a portion of the actual code if that would help, but the error message is the same in both cases.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Symbol not found - object invisible outside of class

    Rule Number 1) don't use generated code, especially if you're learning Java and Swing. Instead go through the tutorials to learn to do it on your own first. Later after you understand Swing, then sure go ahead and use a code-generator.

    Yeah, I think that we'll need to see real code to be able to help you. Please don't forget to use [code] [/code] tags when posting code.

  5. #5
    stesteve is offline Member
    Join Date
    Feb 2012
    Location
    Chandler Arizona
    Posts
    3
    Rep Power
    0

    Default Re: Symbol not found - object invisible outside of class

    Here is a more complete code sample with the auto generated code removed. Symbols not found are testExecutionThread in jButtonPush() and tex in displaySampleResults(). Where should the objects be created so they can be seen outside main and outside NewClass? Are you saying that all of the code needs to be inside main? I tried putting the object creation statements in a static initialization block and then in the NewClass constructor, but those ideas didn't seem to work either. I feel like I am stuck on something fundamental, but it seems like there has to be a way to access an object outside the context it was created or else everything would have to be in one large class. What am I missing?

    Java Code:
    package newpackage;
    
    public class NewClass {
    
        public NewClass() {
        }  
    
        //* fake method like one that would be linked to a GUI button *//
        private void jButtonPush() {                                           
            testExecutionThread.start(); // this starts the thread execution
        }                                          
    
        //* update a GUI text field *//
        public void addTestResult(String msg) {
        }
        
        public static void main(String args[]) {
            NewClass tex = new NewClass();
            ExecutionThread testExecutionThread = new ExecutionThread();               
        }
    }
    
    class ExecutionThread extends Thread {
        public void ExecutionThread() {
        }   
        
        public void run() {
            displaySampleResults();
        };
    
        private void displaySampleResults() {
            tex.addTestResult("Starting test...\n");
        }
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default Re: Symbol not found - object invisible outside of class

    If you want more than one method in a class to see a variable, you need to define it at the class level and not inside of a method.
    Move the definition of testExecutionThread outside of the main method.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Method Graphics.drawString() - Symbol not found
    By km0r3 in forum AWT / Swing
    Replies: 1
    Last Post: 08-27-2010, 02:55 PM
  3. Compile: symbol not found
    By zuan in forum New To Java
    Replies: 7
    Last Post: 06-24-2008, 05:17 AM
  4. symbol not found error
    By rmartyce in forum New To Java
    Replies: 1
    Last Post: 05-23-2008, 05:58 AM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •