Results 1 to 13 of 13
  1. #1
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default need help with code

    Hi,
    I've been working on trying to solve this for like 8 hours and no matter what I do I can't get the button to start the game.
    Java Code:
    public class Main extends JFrame implements ActionListener{
    
    	JFrame frame;
    	JButton singlePlayer;
    	JButton versus;
    	final Container contain = getContentPane();
    	
    	public Main() {
    		
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setTitle("R - Type");
            setResizable(false);
            setVisible(true);
    
    		singlePlayer  = new JButton("Single Player");
    		versus = new JButton("Versus");
    		
    		singlePlayer.setActionCommand("single");
    		singlePlayer.setEnabled(true);
    		singlePlayer.addActionListener(this);	
    	
    		
    		versus.setActionCommand("versus");
    		versus.setEnabled(true);
    		versus.addActionListener(this);
    		
    		
    		contain.setLayout(new FlowLayout());
    		contain.add(singlePlayer);
    		contain.add(versus);
    		contain.setBackground(Color.BLACK);			 
    	
    		
    	}
    
    
    	public static void main(String[] args) {
    		new Main();
    	}
    
    
    	public void actionPerformed(ActionEvent e) {
    		if ("single".equals(e.getActionCommand())) {
    			add(new Board());
    		}
    		
    	}
    
    }
    I've looked up so many tutorials on using button but the action doesn't seem to be getting passed on. help aprreciated. Thanks

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

    Default

    How about adding some debug statements to see if the actionPerformed is actually being called, something like:

    Java Code:
    	public void actionPerformed(ActionEvent e) {
    		System.out.println("in actionPerformed");
    		if ("single".equals(e.getActionCommand())) {
    			System.out.println("in if single equals block");
    			add(new Board());
    		}

  3. #3
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    kk i added and it displays that so its getting to it.
    this is the display of the eclispe console when i click the button:

    in if single equals block
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:155)
    at Spacesships.Ship.<init>(Ship.java:17)
    at Spacesships.Board.<init>(Board.java:28)
    at Spacesships.Main.actionPerformed(Main.java:58)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6268)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 3)
    at java.awt.Container.processEvent(Container.java:204 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4629)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2103)
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517 )
    at java.awt.Component.dispatchEvent(Component.java:44 55)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)

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

    Default

    That probably should have been in your initial post as it's critically important information. Looks like your ship's ImageIcon is null. Are you sure that you're reading in an image file properly? Time to do some more debugging it seems.

  5. #5
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    yea sry about it shouldve been the first. Heres my reading in the image part:
    Java Code:
    ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png"));
            image = ii.getImage();
    And my image is saved into a resources folder thats project level and isn't located in the package. Thanks for the help

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

    Default

    It looks like it should be in a subdirectory off of the class files. Should ("/resources/craft.png") have the leading backslash? What if you do instead "(resources/craft.png)"?

  7. #7
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    i've tried tons of different paths for the image but that error always shows up. Is there something wrong with using imageIcon, should i use a different method of reading in an image?

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

    Default

    I usually use the ImageIO class to read in images. Then if I need an ImageIcon, I create one with the BufferedImage obtained.

  9. #9
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    ok i switched to using buffered image and ImageIO:
    Java Code:
    try {
    			BufferedImage bi = ImageIO.read(new File("/resources/craft.png"));
    			image = bi;
    		} catch (IOException e) {
    			System.out.println("BufferedImage error:" + e);
    			e.printStackTrace();
    		}
    I'm getting a can't read input file error. Does this mean I still have the wrong file path or that theirs something wrong with the image?

  10. #10
    MichaelT is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    bumppppppp

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

    Default

    Make the simplest of programs that does nothing but read in an image, create in ImageIcon with it, place that ImageIcon in a JLabel and display the JLabel in a JOptionPane and let's see what happens.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    How are you running that code? Command line? IDE? Which IDE?

    db

  13. #13
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    yea sry about it shouldve been the first. Heres my reading in the image part:

    Java Code:
    ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png"));
            image = ii.getImage();
    And my image is saved into a resources folder thats project level and isn't located in the package. Thanks for the help
    Hey did you tried atleast ones by giving manual path to get the image from that file???

    Also, check the path by taking it as a output on console......

Similar Threads

  1. look @ code and give ideas! Install new code....
    By turbowhat in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 03:08 AM
  2. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  3. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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