Results 1 to 3 of 3
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Angry Can't use variables that are declared??

    I have the following beginning of a program that I an trying to start, but stupid NetBeans won't let me use the variables I have declared for some reason. Every time I try to use one of my declared variables, it spits an error at me, and I do not see what in the world is wrong now!!
    Java Code:
    package pkginterface;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    public class Interface implements ActionListener{
    
        JFrame frame = new JFrame("Interface program");
        JTextField txt1 = new JTextField(24);
        JTextArea txt2 = new JTextArea(24, 24);
        JButton b1, b2, b3; //These are all properly declared, but cannot be used without Netbeans putting red lines underneath every line in the code!!
        
               
        
            
        public static void main(String[] args) {
            // TODO code application logic here
        }
    
         public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    For instance, if I try to do this:
    Java Code:
    package pkginterface;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    public class Interface implements ActionListener{
    
        JFrame frame = new JFrame("Interface program");
        JTextField txt1 = new JTextField(24);
        JTextArea txt2 = new JTextArea(24, 24);
        JButton b1, b2, b3;
        
        b1.add // NOPE NOT ALLOWED....
               
        
            
        public static void main(String[] args) {
            // TODO code application logic here
        }
    
         public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    I do not get this, I mean I know Java is a tough programming language to get used to , but are you telling me that it won't even let you use variables now??

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can't use variables that are declared??

    You can't specify statements or expressions outside a method body.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Red face Re: Can't use variables that are declared??

    Quote Originally Posted by JosAH View Post
    You can't specify statements or expressions outside a method body.
    Thanks for explaining this.... I did get it to work finally, by placing all of this inside of a new constructor!!

Similar Threads

  1. Replies: 3
    Last Post: 07-30-2012, 09:34 AM
  2. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 10:17 AM
  3. Replies: 13
    Last Post: 02-02-2011, 03:36 AM
  4. LinkedList declared as an abstract superclass
    By box2box in forum New To Java
    Replies: 2
    Last Post: 04-21-2010, 11:32 PM
  5. Implicit object in jsp declared where?
    By mahendra.athneria in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 08-11-2009, 03:23 PM

Tags for this Thread

Posting Permissions

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