Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Ambiguity and non-static variable reference error in java

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ambiguity and non-static variable reference error in java

    I'm working on a program where i create an LED stopwatch but i honestly am completely stumped. The base code is done, but i haven't began to fill anything in. I took the code and put it all into one class, with subclasses strewn throughout. When i try to compile the code, i get this error:

    assig5.java:81: reference to Timer is ambiguous, both class javax.swing.Timer i
     javax.swing and class java.util.Timer in java.util match
            private Timer T;  // Timer -- see the API in javax.swing.Timer
                    ^
    assig5.java:19: non-static variable this cannot be referenced from a static con
    ext
            new A5Help(sz, rate);
            ^
    assig5.java:120: reference to Timer is ambiguous, both class javax.swing.Timer
    n javax.swing and class java.util.Timer in java.util match
                    T = new Timer(rate, this);
                            ^
    3 errors
    Right now i'm just working on getting to program to compile and run, doing it each time i make any headway so that i can get errors before they pile up. Any suggestions on what to do about #2 specifically?

    I'm new to this, this is my 1st year doing java, but i've done visual basic high school for 3 years so i understand it conceptually. I just don't know how to do it in java
    Last edited by jenseits; December 1st, 2008 at 05:29 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Can't decipher errors

    Hello jenseits and welcome to the Java Programming Forums.

    Would it be possible for you to post your entire code?
    I personally don't think I could solve this problem without seeing it.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't decipher errors

    By the way,
    how do you send error messages to a text file

    I tried a few like
    javac work.java >> error.txt
    even
    javac work.java 2>error.txt
    someone once told me


    OK never mind I kept plugging away at google and found it
    It goes
    javac work.java -Xstdout error.txt
    Last edited by Eric; December 2nd, 2008 at 12:55 PM. Reason: found answer

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't decipher errors

    I've uploaded the code.


    To the above:

    If you're using the dos prompt when you get the error, right click on the text and you should see an option called mark. Click that, then highlight the text and hit enter. It'll unhighlight and then you can paste it
    Attached Files Attached Files

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Can't decipher errors

    OK im going to paste the code below so people can see it without downloading the txt file.

    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*; 
    import java.awt.geom.*;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class assig5
    {
     
      public static void main(String [] args)
      {
            new assig5();
     int sz = Integer.parseInt(args[0]);
     int rate = Integer.parseInt(args[1]);
     new A5Help(sz, rate);
      }
      public class A5Help
      {
            private MyPanel thePanel;  // StopWatch is a subclass of JPanel
     private JFrame theWindow;
            public A5Help(int sz, int rate)
     {
      theWindow = new JFrame("CS 401 A5 Help Program");
     
      thePanel = new MyPanel(sz, rate);  // The argument to this constructor
      // determines the size of the digits in the stopwatch
      Container c = theWindow.getContentPane();
      c.add(thePanel, BorderLayout.CENTER);
      theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      theWindow.pack();
      theWindow.setVisible(true);
     }
      }
      class CrazyLED extends LED
      {
     private int active;
     public CrazyLED(int X, int Y, int sz)
     {
      super(X, Y, sz);
      active = 0;
     }
     // This method is abstract in LED, so it must be implemented
     // here.  Note what is being done.
     public void draw(Graphics2D g2)
     {
      // I am increasing the stroke width so the LED looks better
      g2.setStroke(new BasicStroke(8));
      g2.setColor(Color.blue);
      g2.draw(segments[active]);
      // Setting the stroke back to the default here -- see what happens
      // if you don't do this.
      g2.setStroke(new BasicStroke());
     }
     // Simple mutator to change the segment that is being
     // displayed
     public void mutate()
     {
      active = (active + 1) % segments.length;
     }
      }
      public class MyPanel extends JPanel implements ActionListener
      {
     // Declare instance variables as shown.
     private CrazyLED theLED;  // Object to be drawn graphically
     private JButton start, stop, move;  // Some buttons
     private JPanel buttonPanel;  // Another panel to hold the buttons
     private ButtonListener bListener;  // listener for the buttons
     private Timer T;  // Timer -- see the API in javax.swing.Timer
     private int size;
     private int prefWid, prefHt;  // see details on these below
     public MyPanel(int sz, int rate)
     {
      size = sz;
      // Set the preferred width and height of the panel based on the
      // size argument that is passed in.  See the getPreferredSize
      // method below for more information on this.
      prefWid = 5 * size;
      prefHt = 4 * size;
      // Randomly place the CrazyLED in the panel
      Random R = new Random();
      int x = R.nextInt(prefWid - size);
      int y = R.nextInt(prefHt - 2*size);
      theLED = new CrazyLED(x, y, size);
      // Make some buttons and put them in a new subpanel.  We separate
      // the panel with the buttons from the main panel so that the
      // graphics don't interfere with the rendering of the components.
      buttonPanel = new JPanel();
      buttonPanel.setLayout(new GridLayout(1, 2));
      bListener = new ButtonListener();
      start = new JButton("Go");
      start.addActionListener(bListener);
      buttonPanel.add(start);
      stop = new JButton("Stop");
      stop.addActionListener(bListener);
      stop.setEnabled(false);
      buttonPanel.add(stop);
      this.add(buttonPanel, BorderLayout.NORTH);
      // Create a new Timer, setting the delay to rate milliseconds.
      // The second argument to the constructor must be an
      // ActionListener.  Since the current object (a MyPanel object)
      // implements ActionListener, we can pass it to the Timer constructor
      // using the "this" keyword.
      T = new Timer(rate, this);
     
     }
     // When a panel is added to a JFrame, the JFrame needs to determine how
     // much space to give it.  The getPreferredSize method allows the panel to
     // "tell" the JFrame that it wants the specified amount of space.  However,
     // it is only preferred -- the JFrame is not obliged to give it this much
     // space.
     public Dimension getPreferredSize()
     {
      return new Dimension(prefWid, prefHt);
     }
     // Handle the buttons but starting and stopping the timer.
     private class ButtonListener implements ActionListener
     {
      public void actionPerformed(ActionEvent e)
      {
       if (e.getSource() == start)
       {
        T.start();
        start.setEnabled(false);
        stop.setEnabled(true);
       }
       else if (e.getSource() == stop)
       {
        T.stop();
        stop.setEnabled(false);
        start.setEnabled(true);
       }
      }
     }
     
     // To draw graphics within a JPanel in Java, we override the
     // paintComponent() method.  Experiment with this method by putting
     // other graphical drawing code in here.
     public void paintComponent(Graphics g)
     {
      super.paintComponent(g); // Make sure you call the super version
             // before anything elses
      Graphics2D g2 = (Graphics2D) g;  // The Graphics2D class is a
         // subclass of Graphics with added functionality.  By
         // casting we get access to this functionality, including
         // the ability to draw simple objects such as
         // Rectangle2D, Ellipse2D and Line2D
      if (theLED != null) // It's a good idea to check for null here
       theLED.draw(g2);
     }
     // The ActionListener that is fired by the Timer
     public void actionPerformed(ActionEvent e)
     {
      theLED.mutate();
      repaint();  // This method call requests that the graphics be redrawn
         // See what happens if it is not done.
     }
       }
      public abstract class LED implements Drawable
      {
     protected Line2D [] segments;
     protected int size;
     public LED(int X, int Y, int sz)
     {
      size = sz;
      segments = new Line2D[7];
      int startX = X, startY = Y;
      int endX = startX + size, endY = startY;
      segments[0] = new Line2D.Double(startX, startY, endX, endY);
      startX = endX; startY = endY; endY = endY + size;
      segments[1] = new Line2D.Double(startX, startY, endX, endY);
      startX = endX; startY = endY; endY = endY + size;
      segments[2] = new Line2D.Double(startX, startY, endX, endY);
      startX = endX; startY = endY; endX = endX - size;
      segments[3] = new Line2D.Double(startX, startY, endX, endY);
      startX = endX; startY = endY; endY = endY - size;
      segments[4] = new Line2D.Double(startX, startY, endX, endY);
      startX = endX; startY = endY; endY = endY - size;
      segments[5] = new Line2D.Double(startX, startY, endX, endY);
      startX = X; startY = Y + size; endX = X + size; endY = startY;
      segments[6] = new Line2D.Double(startX, startY, endX, endY);
     }
       public abstract void draw(Graphics2D g);
      }
    }

    I've had a look myself and there seems to be all sorts of errors!! I cannot get this to compile.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Can't decipher errors

    Quote Originally Posted by JavaPF View Post
    I've had a look myself and there seems to be all sorts of errors!! I cannot get this to compile.
    Yer, I think that's why jenseits asked for help here.

    The first & last error jenseits asked about are due to the way the imports are specified - to import every class in the packages. Since there is a Timer class in both java.util and javax.swing (which, for some reason has been imported twice), the compiler doesn't know which to use. The fix is to either specify the full qualified path to the Timer class when referring to it, or to import the classes individually for those packages so there's no confusion.

    The other error is due to trying to use an instance variable from a static context. The fix is to either declare the variable static or to only reference it from a class instance.

    jenseits has cross-posted this elsewhere, so further details are probably unnecessary.

Similar Threads

  1. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM