CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] Can't decipher errors

  1. #1
    Join Date
    Dec 2008
    Posts
    17

    [RESOLVED] Can't decipher errors

    I'm trying to create an LED stopwatch GUI but am completely stumped. I have the skeleton code but haven't really been able to do anything with it, and ran into some errors while compiliing:
    Code:
    assig5.java:75: reference to Timer is ambiguous, both class javax.swing.Timer in
     javax.swing and class java.util.Timer in java.util match
            private Timer T;
                    ^
    assig5.java:19: non-static variable this cannot be referenced from a static cont
    ext
            new A5Help(sz, rate);
            ^
    assig5.java:102: reference to Timer is ambiguous, both class javax.swing.Timer i
    n javax.swing and class java.util.Timer in java.util match
                    T = new Timer(rate, this);
                            ^
    3 errors
    I'm new to java, but i took visual basic for 3 years in high school so i understand it conceptually. I just can't physically program too well yet.
    Attached Files Attached Files
    Last edited by jenseits; December 1st, 2008 at 11:32 PM.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Can't decipher errors

    There is a Timer class in both the javax.swing package and the java.util package. If you import both, then the compiler doesn't know which one to use.

    You can distinguish between the two by using their qualified names, ex:
    Code:
    private javax.swing.Timer T;
    - petter

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can't decipher errors

    Most people post their code and forget to post the errors, you've done the opposite.

    Having said that:

    The first and third errors are the same error. It is because there are two Timer classes in the jdk and presumably you've included import statements for both of them (or you've included both of the packages they are in) and so the compiler doesn't know which one you want to use.

    The solution is to explicitly import each class you want to use rather than importing the whole package. Alternatively you can specify the package name when declaring the variable ie:
    Code:
    private java.util.Timer T;
    The second error is because you can not access instance variables from static methods. Without seeing the code it's hard to suggest a solution.

    ------------------------------------------
    EDIT: Sorry WildFrog hadn't spotted you'd already answered this
    Last edited by keang; December 1st, 2008 at 06:38 PM.

  4. #4
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Sorry bout that. I wasn't sure how i should post the code whether just copy the text or attach the file. It's rather long but i'll link it below and in the main post. Thank you for the input i'll start there.
    Attached Files Attached Files

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Can't decipher errors

    Quote Originally Posted by jenseits View Post
    Sorry bout that. I wasn't sure how i should post the code whether just copy the text or attach the file.
    Perhaps if you read the 'Before You Post' guide at the top of the forum you'd know...

    It's odd that people asking for help seem unable to make that tiny effort to improve their chances of a good response...

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anonymous
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Quote Originally Posted by dlorde View Post
    Perhaps if you read the 'Before You Post' guide at the top of the forum you'd know...

    It's odd that people asking for help seem unable to make that tiny effort to improve their chances of a good response...

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anonymous
    I didn't want to just throw a wall of code on here and wait for an answer, now that i know i can make it easier

  7. #7
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Quote Originally Posted by keang View Post
    The second error is because you can not access instance variables from static methods. Without seeing the code it's hard to suggest a solution.

    ------------------------------------------
    EDIT: Sorry WildFrog hadn't spotted you'd already answered this
    I managed to resolve the 1st problem, the only error i'm getting is that one, I tried experimenting but i can't figure out how to gain that access. Would a mutator help? I had just thought of that.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can't decipher errors

    As I said before we need to see your code and most of us won't click on links so it needs to be posted inline (remembering to use code tags). If it is several hundered lines then just post the relevant snippets of code and an explanation of what you are trying to achieve.

  9. #9
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Code:
      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; 
    	private JFrame theWindow;
    
            public A5Help(int sz, int rate)
    	{
    		theWindow = new JFrame("Stopwatch");
    	
    		thePanel = new MyPanel(sz, rate);  
    
    		Container c = theWindow.getContentPane();
    		c.add(thePanel, BorderLayout.CENTER);
    
    		theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		theWindow.pack();
    		theWindow.setVisible(true);
    	}
      }
    This is where the error is occuring

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Can't decipher errors

    Quote Originally Posted by jenseits View Post
    This is where the error is occuring
    Where? There's only one static method, and it doesn't reference any instance variables.

    What is the error? Post up the full text of the error message and stack trace, and post the code containing the line that the error specifies.

    The biggest difference between time and space is that you can't reuse time...
    M. Furst
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can't decipher errors

    Quote Originally Posted by dlorde
    Where? There's only one static method, and it doesn't reference any instance variables.
    I think he/she is referring to the commented out line "new A5Help(sz, rate);" in the main method. My guess is the main method is in the class assig5 and A5Help is an inner class of assig5.

  12. #12
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Code:
    assig5.java:19: non-static variable this cannot be referenced from a static context
    
            new A5Help(sz, rate);
            ^
    1 error
    This is the error i get when i try to compile it, which is in reference to the public static void main section of my code:

    Code:
    public static void main(String [] args)
      {
            new assig5();
    	int sz = Integer.parseInt(args[0]);
    	int rate = Integer.parseInt(args[1]);
    	new A5Help(sz, rate); <--------- (was commented out earlier to see what happened
      }
    
      public class A5Help
      {
            private MyPanel thePanel;  
    	private JFrame theWindow;
    
            public A5Help(int sz, int rate)
    	{
    		theWindow = new JFrame("Stopwatch");
    		thePanel = new MyPanel(sz, rate);  
    		Container c = theWindow.getContentPane();
    		c.add(thePanel, BorderLayout.CENTER);
    		theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		theWindow.pack();
    		theWindow.setVisible(true);
    	}
      }
    I tried to compliment it out so i could run the program and see if it'd compile, which it did. I guess i'm trying to find a way to reference this in the "main" code.

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can't decipher errors

    So is A5Help an inner class and if so why?

    Looking at the size of the attached file your code can't be that many lines, so why not just post the whole lot inline so we can see what's going on. And remember to explain what it's supposed to do.

  14. #14
    Join Date
    Dec 2008
    Posts
    17

    Re: Can't decipher errors

    Quote Originally Posted by keang View Post
    So is A5Help an inner class and if so why?

    Looking at the size of the attached file your code can't be that many lines, so why not just post the whole lot inline so we can see what's going on. And remember to explain what it's supposed to do.
    Looking at it now you're right, i don't need both to be declared as an inner class, i'll change it. I'll post the rest of it.
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    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; 
    	private JFrame theWindow;
    
            public A5Help(int sz, int rate)
    	{
    		theWindow = new JFrame("CS 401 A5 Help Program");
    	
    		thePanel = new MyPanel(sz, rate);  
    
    		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;
    	}
    
    	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());
    	}
    
    	public void mutate()
    	{
    		active = (active + 1) % segments.length;
    	}
      }
    
      public class MyPanel extends JPanel implements ActionListener
      {
    	private CrazyLED theLED;  
    	private JButton start, stop, move;  
    	private JPanel buttonPanel;	 
    	private ButtonListener bListener;  
            private javax.swing.Timer T;
    	private int size;
    	private int prefWid, prefHt;  
    
    	public MyPanel(int sz, int rate)
    	{
    		size = sz;
    		prefWid = 5 * size;
    		prefHt = 4 * size;
    
    		Random R = new Random();
    		int x = R.nextInt(prefWid - size);
    		int y = R.nextInt(prefHt - 2*size);
    		theLED = new CrazyLED(x, y, size);
    		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);
    
    		T = new Timer(rate, this);
    		
    	}
    
    	public Dimension getPreferredSize()
    	{
    		return new Dimension(prefWid, prefHt);
    	}
    
    	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);
    			}
    		}
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);	
    		Graphics2D g2 = (Graphics2D) g;		
    		if (theLED != null)	
    			theLED.draw(g2);
    	}
    
    	// The ActionListener that is fired by the Timer
    	public void actionPerformed(ActionEvent e)
    	{
    		theLED.mutate();
    		repaint();
    	}
       }
    
      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);
      }
    }
    The program is supposed to act like an LED stopwatch. You click on a start button and it starts counting. Every "tick" the display changes to show the lines of the digit from 0-9, and reseting at one cycle. Now for the 10 second and minute mark it will only go to 5 then reset. After a full cycle the next digit tallies. I have the skeleton coding here, but i haven't really started doing anything with the buttons. I'm just trying to get it to compile so a panel comes up, but doesn't do anything.

  15. #15
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Can't decipher errors

    It would have helped if the OP posted the code that caused the error, rather than code where the problem line is commented out without explanation...

    In terms of scope, an inner class is much like a class field - you can't access an instance field or class from a static scope. If you want to access class A5Help from a static method, you'll have to declare the class itself static.

    Using inner classes for an exercise of this kind is not recommended. Inner classes have their uses, but there is quite a range of options for them which can make them appear complicated and obscure to a novice coder. They are really best left until there is a clear requirement for them, then treated individually, according to the circumstances.

    If you really must put all your exercise classes into the same file (which, btw, is very bad practice), at least put them outside the main class scope. You won't be able to declare them public, but you'll avoid some of the confusion that inner classes can cause. Best of all, follow Best Practice and put them in their own files until you are clear about why you'd want to do it differently.

    Good judgment comes from experience; experience comes from bad judgment...
    F. Brook
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)