Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Norm

Thread: Java/Eclipse is reading my constructor commands in wrong order

  1. #1
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Question Java/Eclipse is reading my constructor commands in wrong order

    Hello people,

    I am new to this forum so, I hope im gonna have some fun here, learn and maybe I can help others ;)

    I had a problem with my strategy game im working on...
    And than I thought about something, I have worked with other (easier) programming languages and I loved to be on a forum for fun, to help others and to get help, and I typed in google "Java Forum" and registered at the first one coming up ;)

    So ehm, lets explain my problem now..
    So, like you have read, I am making a strategy game but I walked into a problem...

    I am declaring a class in the Constructor of my JPanel and after the declaration of that class, called createLevel, I declare some instances from the class Slave...
    And there the problem is, It first declares the Slaves and after that the createLevel class, and that gives some problems, because I am using some information from the createLevel class in the slave instances.

    Here the broncode:
    Java Code:
    new createLevel(); // give blocks the good ID's and stuff
    		
    		// player get 3 slaves on start WHY DOES THIS STUPID PROGRAM/LANGUAGE declare THIS BEFORE declaring THE CREATELEVEL???????????????????????????
    		slave[0] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		slave[1] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		slave[2] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		System.out.println(baseXY[0][0]);
    		
    		for(int i = 3; i < slave.length; i++){
    			slave[i] = new Slave(-1, -1, false);
    		}
    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Quote Originally Posted by Herjan View Post
    ... the declaration of that class, called createLevel ...
    Class names should start with an uppercase letter -> Code Conventions for the Java Programming Language: Contents

    And there the problem is, It first declares the Slaves and after that the createLevel class, and that gives some problems, because I am using some information from the createLevel class in the slave instances.
    Somebody else here may have a better crystal ball, but I haven't a clue where your problem might lie (or even exactly what problem you have).

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    It first declares the Slaves and after that the createLevel class,
    How do you know the order the code is executed?
    Sierra likes this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Quote Originally Posted by Herjan View Post
    I am declaring a class in the Constructor of my JPanel and after the declaration of that class, called createLevel, I declare some instances from the class Slave...
    And there the problem is, It first declares the Slaves and after that the createLevel class, and that gives some problems, because I am using some information from the createLevel class in the slave instances.
    I guess the problem is that you do not know the concept of object orientation that is the difference between class, object and method and declaration and reference.
    I can also only guess what your problem might be - and if I am right this advice will helkp you: Do less things in the constructor and more in dedicated methods - the constructor can be tricky as it constructs the object and e.g. constructing others during that process that may reference back this one creates errors. Use a straight way of creating, initializing and then using your objects.

    If you want more specific help I think you have to post the full class or at least method because nobody can work with just a code fragment.
    I like likes!

  5. #5
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Class names should start with an uppercase letter
    Thanks, I have never known that!

    How do you know the order the code is executed?
    I was afraid that, the baseXY variable didn't change at all so I printed everywhere (with System.out.println("Class blabla " + baseXY)) to actually see where it was going wrong, but what I noticed is that the Slave class prints got printed out BEFORE the CreateLevel class prints (I have changed it to an uppercase letter now ;)

    Do less things in the constructor and more in dedicated methods - the constructor can be tricky as it constructs the object and e.g. constructing others during that process that may reference back this one creates errors. Use a straight way of creating, initializing and then using your objects.
    Yeah, you are true, but I have just placed it in the constructor so you can see it in like, 10 seconds, and not have to watch through my define class where im initializing hundreds of images and bad stuff ;)

    Java Code:
    public class GamePanel extends JPanel implements Runnable {
        public GamePanel(){
    		
    		define();
    		
    		//set all baseXY to -1, means unused
    		for(int x = 0; x<baseXY.length; x++){
    			baseXY[x][0] = -1;
    		}
    		
    		new CreateLevel(); // give blocks the good ID's and stuff
    		
    		// player get 3 slaves on start WHY DOES THIS STUPID PROGRAM/LANGUAGE initialize THIS BEFORE initializing THE CREATELEVEL???????????????????????????
    		slave[0] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		slave[1] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		slave[2] = new Slave(baseXY[0][0], baseXY[0][1], true);
    		
    		for(int i = 3; i < slave.length; i++){
    			slave[i] = new Slave(-1, -1, false);
    		}
    	}
    
    public void run(){
    		while(true){
    			
    			try{
    				repaint();
    				Thread.sleep(5);
    			}catch(Exception e){
    				System.out.println("run error!");
    			}
    			
    		}
    	}
    
    }
    There is nothing to show more :S, in the define method the only thing that is happening is initializing of some images...
    But if its helpful to post the whole project I will do that, but I dont know why that should be helpful, its just initializing in the wrong order...

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    How can the posted code be compiled and executed for testing to show the problem?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Aha, okey then,

    Here the full project is:

    Strategy.rar

    Thanks for the fast reply

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    I'm not looking for the full project that has to be downloaded from a foreign site.

    I am looking for a small, complete program that compiles, executes and shows the problem.
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    its just initializing in the wrong order...
    It is 99.9% NOT doing what you say here...

    In the files you provided I do not see any printouts to reproduce the error... sorry. But you know that if you get an Exception, all of your code there will be skipped and you return, right?
    That is most probably what happens...

    EDIT: Wait, I just saw something... you don't dare, do you? You wonder why your baseXY does not change? Because you do never change it anywhere! Show me where it should be changed in CreateLevel()...?
    Last edited by Sierra; 01-11-2013 at 08:01 PM.
    I like likes!

  10. #10
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Quote Originally Posted by Sierra View Post
    It is 99.9% NOT doing what you say here...

    In the files you provided I do not see any printouts to reproduce the error... sorry. But you know that if you get an Exception, all of your code there will be skipped and you return, right?
    That is most probably what happens...

    EDIT: Wait, I just saw something... you don't dare, do you? You wonder why your baseXY does not change? Because you do never change it anywhere! Show me where it should be changed in CreateLevel()...?
    Ofcourse I do know that exception thingy, but i have already deleted that printouts..., but if you just put two simple printouts in the classes, you will see it is first initializing the slaves.. Thanks for checking it btw

  11. #11
    Herjan is offline Member
    Join Date
    Jan 2013
    Location
    FailLand
    Posts
    6
    Rep Power
    0

    Default Re: Java/Eclipse is reading my constructor commands in wrong order

    Wait, I have downloaded my own files and now it's working fine... Thanks for all the help, if I have next time a problem like this Im gonna upload it to the internet and download it for myself lolz.

    To be honest... :S hard to say but I failed... putting a print in the paint method instead of in the constructor... Gonna make new account where I have a better prestation without BIG fails ^^
    Last edited by Herjan; 01-11-2013 at 08:23 PM.

Similar Threads

  1. reading commands from console
    By merik in forum New To Java
    Replies: 9
    Last Post: 11-17-2010, 04:09 PM
  2. Replies: 25
    Last Post: 11-04-2010, 04:26 PM
  3. Reading random order
    By khh717 in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 10:24 PM
  4. Wrong Order in Program Run
    By HeatR216 in forum New To Java
    Replies: 0
    Last Post: 03-25-2010, 08:41 PM
  5. CMD is not reading commands
    By colonial in forum Forum Lobby
    Replies: 1
    Last Post: 03-15-2010, 02:36 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
  •