Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Set array values in a method

  1. #1
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Set array values in a method

    Hi,
    I am currently trying to set values to array integers.
    I want the integers to be interchangeable depending on which method is called.
    However i get errors when i try.
    Here is the code:
    Java Code:
    public class HelloWorld extends JPanel implements Runnable {
    ...
    static boolean golem = false;
    static GamePanel game = new GamePanel();
    		final static JLabel[][] labels = new JLabel[2][2];{
    		labels[0][0]= (new JLabel("Movement " +game.gamestats[0][0]));
    		labels[0][1]= (new JLabel("Jump " + game.gamestats[0][1]));
    		labels[1][0]= (new JLabel("Attack " + game.gamestats[1][0]));
    		labels[1][1]= (new JLabel("Heal " + game.gamestats[1][1]));
    ...
    
    public static void main(String[] args) throws IOException{
    frame.setContentPane(new JPanel() {
    @Override
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    
                                    if (golem){
    						
    						golemStats();
    						frame.add(labels[0][0]);
    						frame.add(labels[0][1]);
    						frame.add(labels[1][0]);
    						frame.add(labels[1][1]);
    						labels[0][0].setLocation(250, 325);
    						labels[0][1].setLocation(250, 350);
    						labels[1][0].setLocation(250, 375);
    						labels[1][1].setLocation(250, 400);
    
                                                 }
                                                   }
    public static void golemStats(){
    		game.gameStats[0][0] = 2;
    		game.gameStats[0][1] = 2;
    		game.gameStats[1][0] = 9;
    		game.gameStats[1][1] = 3;
    	}
    }
    public class GamePanel extends JPanel implements Runnable
    {
    	
    	
    	int[][] gamestats = new int [2][2];{
    	gamestats[0][0] = 0;// move
    	gamestats[0][1] = 0;//jump
    	gamestats[1][0] = 0;//attack
    	gamestats[1][1] = 0;//heal
    						}
          }

  2. #2
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    By the way thanks in advance, and would it be possible to set the font size of the label text?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Set array values in a method

    i get errors
    Please copy the full text of the error messages and paste them here.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    "gameStats cannot be resolved or is not a field"

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Set array values in a method

    Java is case sensitive.

    Additionally, the default value for elements of a numeric type array is 0* so there's no point in initializing all the elements to the value they already have.

    * For reference types, the default is null and for booleans, it is false.

    db
    dd2308 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Set array values in a method

    1. in GamePanel, you never made the constructor.

    it should be like this:

    Java Code:
    public class GamePanel extends JPanel implements Runnable
    {
         
         
        int[][] gamestats = new int [2][2];
    
        public GamePanel(){
        gamestats[0][0] = 0;// move
        gamestats[0][1] = 0;//jump
        gamestats[1][0] = 0;//attack
        gamestats[1][1] = 0;//heal
                            }
          }
    also, if you are implementing runnable, don't you need to include:

    Java Code:
    public void run(){
    
    }
    and are you sure you want to use the subclasses in the main function? You have a lot of nested code, which makes this program a little bit confusing to read.

    I normally create those functions outside of the main function and then call them from the main function.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Set array values in a method

    Quote Originally Posted by AeneasTroy View Post
    1. in GamePanel, you never made the constructor.
    There's an instance initializer block, which in this case does exactly the same thing.

    Quote Originally Posted by AeneasTroy View Post
    it should be like this:
    No, it shouldn't, for the reason I've already posted at #5 in this thread.

    Quote Originally Posted by AeneasTroy View Post
    You have a lot of nested code, which makes this program a little bit confusing to read.
    I see an if { } block inside a paintComponent(...) override in an anonymous subclass of JPanel. I wouldn't call that 'a lot of nested code' and the only thing making the code confusing to read is the woefully inconsistent indenting.

    Quote Originally Posted by AeneasTroy View Post
    and are you sure you want to use the subclasses in the main function? You have a lot of nested code, which makes this program a little bit confusing to read.

    I normally create those functions outside of the main function and then call them from the main function.
    Java has methods. Not functions.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    I changed my case sensitive mistake but my program crashes now, any suggestions?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Set array values in a method

    my program crashes now, any suggestions?
    Post the full text of the error messages so we can see what the problem is.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    Java Code:
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at javax.swing.JFrame.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at HelloWorld.golemStats(HelloWorld.java:437)
    	at HelloWorld$1.paintComponent(HelloWorld.java:148)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$700(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    the exception part loops continuasly

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Set array values in a method

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at HelloWorld.golemStats(HelloWorld.java:437)
    What variable is null on line 437? Find the variable and change the code so that the variable has a non-null value.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Set array values in a method

    The NullPointerException is the least of your problems.

    Quote Originally Posted by dd2308 View Post
    Java Code:
    public class HelloWorld extends JPanel implements Runnable {
    ...
    frame.setContentPane(new JPanel() {
    @Override
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    
                                    if (golem){
    						
    						golemStats();
    						frame.add(labels[0][0]);
    						frame.add(labels[0][1]);
    						frame.add(labels[1][0]);
    						frame.add(labels[1][1]);
    						labels[0][0].setLocation(250, 325);
    						labels[0][1].setLocation(250, 350);
    						labels[1][0].setLocation(250, 375);
    						labels[1][1].setLocation(250, 400);
    
                                                 }
                                                   }
    NEVER add/remove components or in any way change the state of a (J)Component in a painting method override.

    And for heaven's sake learn how to format code for readability: http://www.oracle.com/technetwork/ja...oc-136057.html

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    Thanks, How would i display the change, visibility setting?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Set array values in a method

    How would i display the change, visibility setting?
    What are the " change, visibility setting"?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    (object).setVisisbility(false);

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Set array values in a method

    I guess you need to start here and work your way up: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Set array values in a method

    (object).setVisisbility(false);
    Check your spelling.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    dd2308 is offline Member
    Join Date
    Sep 2012
    Posts
    28
    Rep Power
    0

    Default Re: Set array values in a method

    Thank you everybody for your help.

Similar Threads

  1. Values in Array
    By jazob in forum New To Java
    Replies: 2
    Last Post: 12-10-2011, 01:29 AM
  2. Get method parameter values using AspectJ?
    By gonny in forum New To Java
    Replies: 0
    Last Post: 11-21-2011, 09:16 AM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. Method to return values
    By puchatek in forum New To Java
    Replies: 4
    Last Post: 11-11-2010, 09:27 AM
  5. Cant we have two values returned from a method in a class
    By jaiminparikh in forum Advanced Java
    Replies: 7
    Last Post: 03-21-2009, 03:20 AM

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
  •