Results 1 to 8 of 8
  1. #1
    Ceecar12 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default HandlerClass problem. Please help

    Hello

    I have a problem with a HandlerClass. I use Eclipse and what ever i do, it still wont work. I am not the greatest at Java, but i really cant se the fault.

    Will someone please tell me what is wrong with this programming. The problem is between is at line 72. The program says:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    No enclosing instance of type Quizzen is accessible. Must qualify the allocation with an enclosing instance of type Quizzen (e.g. x.new A() where x is an instance of Quizzen).

    at Quizzen.main(Quizzen.java:72)


    PS. I am from Denmark, so some of the "println" statements has danish writing in them, but that should not be a problem, because they work fine.

    Java Code:
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.util.Scanner;
    
    public class Quizzen extends JFrame {
    
    	public static void main(String[] args) 
    	{
    		
    			Design Design = new Design();
    			Design.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			Design.setSize(500, 500);
    			Design.setVisible(true);
    
    			JPanel Regler = new JPanel(new GridBagLayout());
    			Design.add(Regler);
    			Design.getContentPane().add(Regler);
    			GridBagConstraints Grit = new GridBagConstraints();
    
    			JLabel Regel1 = new JLabel(
    					"Der bliver stillet et spørgsmål, hvor du får fire svarmuligheder til spørgsmålet.");
    			Grit.insets = new Insets(10, 10, 10, 10);
    			Grit.gridx = 0;
    			Grit.gridy = 5;
    			Regler.add(Regel1, Grit);
    
    			JLabel Regel2 = new JLabel(
    					"Du sætter et flueben i det svar du mener er rigtigt");
    			Grit.gridx = 0;
    			Grit.gridy = 10;
    			Regler.add(Regel2, Grit);
    
    			JLabel Regel3 = new JLabel(
    					"Hvis svaret er rigtigt står der ''Korrekt'' eller ''Forkert''");
    			Grit.gridx = 0;
    			Grit.gridy = 15;
    			Regler.add(Regel3, Grit);
    
    			JLabel Regel4 = new JLabel(
    					"I slutningen af quizzen får du en opgørelse af hvor mange rigtige du har i procent (%)");
    			Grit.gridx = 0;
    			Grit.gridy = 20;
    			Regler.add(Regel4, Grit);
    
    			JLabel Regel5 = new JLabel("Forstår du reglerne?");
    			Grit.gridx = 0;
    			Grit.gridy = 25;
    			Regler.add(Regel5, Grit);
    			
    			JButton Yes;
    			JButton No;
    			
    			Yes = new JButton("Yes");
    			Grit.insets = new Insets(10,10,10,10);
    			Grit.gridx = 0;
    			Grit.gridy = 100;
    			Regler.add(Yes, Grit);
    		
    			No = new JButton("No");
    			Grit.gridx = 0;
    			Grit.gridy = 50;
    			Regler.add(No, Grit);
    			
    			
    			Yes.setActionCommand("Yes");
    			No.setActionCommand("No");
    			
    			HandlerClass handler = new HandlerClass();
    			
    			Yes.addActionListener(handler);
    			No.addActionListener(handler);
    		}
    	
    		public class HandlerClass implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    				{
    					if( "Yes".equals( event.getActionCommand()))
    						JOptionPane.showMessageDialog(null, String.format("Ja"));
    					else if( "No".equals( event.getActionCommand()))
    						JOptionPane.showMessageDialog(null, String.format("Hvis du ikke forstår reglerne, så er du nød til at læse dem igen"));
    				}
    
    			{
    		Scanner scan = new Scanner(System.in);
    
    		System.out.println("Skurken Two-Face fra Batman universet rigtige navn er?");
    
    		System.out.println("Bruce Wayne");
    		System.out.println("Alfred Pennyworth");
    		System.out.println("Harvey Dent");
    		System.out.println("Ra´s al Ghul");
    
    		float rigtige = 0;
    
    		String Spørgsmål1;
    		Spørgsmål1 = scan.nextLine();
    
    		if (Spørgsmål1.equalsIgnoreCase("Harvey Dent")) {
    			System.out.println("Korrekt");
    			rigtige += 1;
    		} else {
    			System.out.println("Forkert");
    		}
    		System.out.println("\t");
    
    		System.out
    				.println("Hvilke af disse superhelte kommer ikke til at være med i superhelte filmen The Avengers?");
    
    		System.out.println("Iron Man");
    		System.out.println("Superman");
    		System.out.println("Captain America");
    		System.out.println("Black Widow");
    
    		String Spørgsmål2;
    		Spørgsmål2 = scan.nextLine();
    
    		if (Spørgsmål2.equalsIgnoreCase("Superman")) {
    			System.out.println("Korrekt");
    			rigtige += 1;
    		} else {
    			System.out.println("Forkert");
    		}
    
    		System.out.println("\t");
    
    		System.out.println("Hvad hedder hovedrolle karakteren i filmen ¨Alien¨?");
    
    		System.out.println("Ellen Ripley");
    		System.out.println("HAL-9000");
    		System.out.println("Darth Vader");
    		System.out.println("Tony Stark");
    
    		String Spørgsmål3;
    		Spørgsmål3 = scan.nextLine();
    
    		if (Spørgsmål3.equalsIgnoreCase("Ellen Ripley")) {
    			System.out.println("Korrekt");
    			rigtige += 1;
    		} else {
    			System.out.println("Forkert");
    		}
    		{
    			System.out.println("Du har svaret " + ((rigtige / 3) * 100)
    					+ "% rigtigt.");
    		}
    	}
    
    }
    }
    Last edited by Ceecar12; 04-05-2012 at 04:31 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: HandlerClass problem. Please help

    You cannot instantiate a non-static innner class outside of an instance of the class in which it is declared.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ceecar12 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: HandlerClass problem. Please help

    Quote Originally Posted by Tolls View Post
    You cannot instantiate a non-static innner class outside of an instance of the class in which it is declared.
    But how do i solve the problem?

  4. #4
    Ceecar12 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: HandlerClass problem. Please help

    What do i have to do for make an action. I need that handlerclass, but i dont where to put it, so the program works.

  5. #5
    Ceecar12 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: HandlerClass problem. Please help

    I am still lost in this problem. Can someone help?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: HandlerClass problem. Please help

    I'd rewrite it, personally, but for an instant "fix" just make that Handler class static.
    Java Code:
    public static class HandlerClass ... etc etc
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Ceecar12 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: HandlerClass problem. Please help

    Quote Originally Posted by Tolls View Post
    I'd rewrite it, personally, but for an instant "fix" just make that Handler class static.
    Java Code:
    public static class HandlerClass ... etc etc
    Thank you so much Tolls

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

Posting Permissions

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