[Solved] Help Me and My Poor Constructor

Discussion in 'Programming' started by Joedon, May 30, 2016.

Thread Status:
Not open for further replies.
  1. Why isn't this working:

    Code (Text):
    private Game game;

    public KeyInput(Game game){
        this.game = game;
    }

    public void doCrap(){
        game.doCrap();
    }
    It's always null on game.doCrap(). Yes the class name is actually named "Game"
     
  2. Well... what value are you passing to the constructor? What is the game.doCrap() method definition?
     
  3. It's actually .resetPlayers() but here it is:
    Code (Text):
        public void resetPlayers(){
            final int p = handler.object.size();
            for(int i = 0; i < p; i++){
                handler.object.remove();
            }
            handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player));
        }
     
  4. What are you passing into the constructor?
     
  5. And here's the whole KeyInput class:
    Code (Text):
    package com.TutorialGame.main;

    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;

    public class KeyInput extends KeyAdapter{

        private Handler handler;
        private Game game;
       
        public KeyInput(Handler handler){
            this.handler = handler;
        }
       
        public KeyInput(Game game){
            this.game = game;
        }
       
        public void keyPressed(KeyEvent event){
            int key = event.getKeyCode();
            for(int i = 0; i < handler.object.size(); i++){
                GameObject tempObject = handler.object.get(i);
                if(tempObject.getId() == ID.Player){
                    if(key == KeyEvent.VK_W) tempObject.setVelY(-3);
                    if(key == KeyEvent.VK_A) tempObject.setVelX(-3);
                    if(key == KeyEvent.VK_S) tempObject.setVelY(3);
                    if(key == KeyEvent.VK_D) tempObject.setVelX(3);
                    }
                }
        }
       
        public void keyReleased(KeyEvent event){
            int key = event.getKeyCode();
            for(int i = 0; i < handler.object.size(); i++){
                GameObject tempObject = handler.object.get(i);
                if(tempObject.getId() == ID.Player){
                    if(key == KeyEvent.VK_W) tempObject.setVelY(0);
                    if(key == KeyEvent.VK_A) tempObject.setVelX(0);
                    if(key == KeyEvent.VK_S) tempObject.setVelY(0);
                    if(key == KeyEvent.VK_D) tempObject.setVelX(0);
                }
            }
            if(key == KeyEvent.VK_SPACE) game.resetPlayers();
        }
    }
     
     
  6. Hmm... Just that doesn't say much. Where is the exception being thrown in your code?

    To aid in debugging, try this: If using Java 8, in the KeyInput constructor make this.game = Objects.requireNonNull(game, "Oh noes!")
    If not, this.game = Preconditions.checkNotNull(game, "Oh noes!")

    This will make it far more clear if the constructor parameter you're passing is non-null
     
  7. Where are you instantiating the KeyInput class?
     
  8. In the Game class:
    Code (Text):
        public Game(){
            handler = new Handler();
            new Window(WIDTH, HEIGHT, "Bootay", this);
            this.addKeyListener(new KeyInput(handler));
                handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player));
        }
    Yes enjoy the game's name
     
  9. When you pass handler to it, you never pass the Game instance itself. You should consider making the constructor as follows:
    Code (Text):
        public KeyInput(Handler handler, Game game){
            this.handler = handler;
            this.game = game;
        }
    And passing the game's instance into the constructor.
     
    • Like Like x 1
  10. God damn I'm stupid sometimes xD Thanks for the tip with that and all I had to do was pass it using 'this'. Thanks guys for your help :)
     
    • Like Like x 1
Thread Status:
Not open for further replies.