0

I know this has been asked many times before, but even by looking at similar posts, I haven't been able to figure this out.

Here is my MainActivity class:

package binchtitsinc.adventuregameamlior;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int compteur;
    private TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        compteur = 0;
        display = (TextView) findViewById(R.id.Afficheur);
        display.setText("Count is: " + compteur);


        Game theGame;

        theGame = new Game();

        theGame.play();

    }


    public void plusOne(View button){
        compteur=compteur+1;
        display.setText("Count is: "+compteur);

    }

    public void toUser(String texte){
        display.setText("Message:"+texte);
    }
}

I already checked, the ID is correct (R.id.Afficheur). What's really weird is that when I call toUser in that class, it works flawlessly. However, when I call that method from another class (here, "Game"), I get the error that I pasted in the title.

Here is the beginning of my class Game:

package binchtitsinc.adventuregameamlior;

/**
 * Created by Leo on 11/04/16.
 */
public class Game {

    //Attributes

    private Parser parser;
    private Energy energy;
    private Room currentRoom;
    private Room goalRoom;
    private MainActivity main;
    private DirectionLexicon dirVocabulary;
    private CommandLexicon commandVocabulary;
    private boolean wantsToQuit;
    private boolean hasTheItem;

    //Methods

    //Public methods

    /**
     * This is a constructor.
     * It creates the game and initialise its internal map.
     */
    public Game() {
        dirVocabulary = new DirectionLexicon();
        commandVocabulary = new CommandLexicon();
        createRooms();


        energy = new Energy();
        main = new MainActivity();

        parser = new Parser(2);  //the parameter 2 here in the call to the constructor of Parser means that in this game
        // sentences typed in by the player will contain at most 2 words.
    }


    /**
     * Main play routine.  Loops until end of play.
     */
    public void play() {
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.

        wantsToQuit = false;
        hasTheItem = false;
        while (!wantsToQuit) {
            if (!currentRoom.isSameRoomAs(goalRoom) || !hasTheItem) {
                // && is the logical operator AND
                // ! is the logical operator NOT
                // currentRoom.isSameRoomAs(goalRoom) checks whether we have reached the goal.
                parser.getAndAnalyzeSentence();
                if (parser.lastSentenceLength() > 0) {// Here we know the player has typed in at list 1 word
                    if (commandVocabulary.isLexiconWord(parser.word(1))) { // here we know the firs word typed in corresponds to a known command
                        if (energy.getEnergy() <= energy.move) { // If the remaining energy is less than the amount required to move, it's over
                            main.toUser("You don't have any more energy. You're dead.");

printwelcome() calls that main.toUser() method, and it's also at the end of the pasted code: main.toUser("You don't have any more energy. You're dead").

Any ideas?

Nelty
  • 79
  • 7
  • Also, if I add the condition: if (display != null) (to avoid the error) My app launches, but I get a white screen and there isn't any error in the logcat. Not sure what's causing that white screen... – Nelty Apr 12 '16 at 14:37
  • This is your problem: `main = new MainActivity();`. It creates a new instance of `MainActivity` in which `display`is null. Instead you need to let the `Game` instance know about the already existing `MainActivity` instance. – Markus Kauppinen Apr 12 '16 at 15:03
  • Thank you! That makes sense. However, I'm not sure on how to do that exactly? – Nelty Apr 12 '16 at 15:22
  • Actually I figured it out. – Nelty Apr 12 '16 at 16:10

0 Answers0