0

I'm creating a text game in a Java Applet so I can display it on my website and have people play it there, however I'm having issues with getting any text to show in my TextArea.

Here is my Main class:

package com.game.main;

import java.applet.*;
import java.awt.*;

public class Main extends Applet {

    private TextField commandInput;
    private TextArea messageDisplay;
    private Button button;
    public Message messages;

    // Initialisation method
    public void init() {
        super.init();

        // Define colours
        setBackground(Color.white);
        setForeground(Color.black);

        Panel appletPanel = new Panel();

        // Use a border layout
        BorderLayout b = new BorderLayout();
        appletPanel.setLayout(b);
        add(appletPanel);

        this.setSize(800, 400);

        // Define UI items
        commandInput = new TextField(20);
        messageDisplay = new TextArea(20, 60); // 20 rows x 60 chars
        button = new Button("Proceed");
        Panel inputPanel = new Panel();

        // Add components to our layout / panels
        inputPanel.add(commandInput);
        inputPanel.add(button);
        appletPanel.add("North", messageDisplay);
        appletPanel.add("South", inputPanel);

        messageDisplay.append(messages.getIntro());
    }
}

And here is my Messages class (this contains all the messages that when the user hits the button it will append the next message using the getWhateverMessage method:

package com.game.main;

public class Message {

    public String currentMessage;

    public String getCurrentMessage() {
        return currentMessage;
    }

    public void setCurrentMessage(String message) {
        currentMessage = message;
    }

    public String getIntro() {
        return "Welcome, This is a text adventure game created by me, Adam Short, as a little project to " +
               "exercise storytelling as well bring a retro style game to you, the player. To play this " +
               "game all you need is a keyboard to type your answers into the input box below. Keep your " +
               "answers relevant or you won't progress through the game at all. Type your answer into the " +
               "input box and hit the Proceed button or enter on your keyboard. Different answers will lead " +
               "to different scenearios and sequences of events. Be careful. Ready to go? Type go in the box " +
               "and hit Proceed!";
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Adam Short
  • 498
  • 7
  • 28
  • 2
    Stacktrace please and point us to that line... – Smit Jun 07 '13 at 18:16
  • 2
    The error in the stack trace is telling you *exactly* where the problem is. `messages` is `null`. – Brian Roach Jun 07 '13 at 18:22
  • 1) For deploying Java desktop apps., the best option is usually to install the app. using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). JWS works on Windows, OS X & *nix. 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jun 09 '13 at 17:26

2 Answers2

5
public Message messages;
...
messageDisplay.append(messages.getIntro());

You define the messages variable which is null, but you never create an instance of the Message class.

Somewhere in your code you need:

messages = new Message();

Either do this when you define the variable or somewhere in your constructor before you use the variable.

appletPanel.add("North", messageDisplay);
appletPanel.add("South", inputPanel);

Also, the above code is wrong. Read theAPI for the add() method. You are recommended to use:

appletPanel.add(messageDisplay, BorderLayout.NORTH);
appletPanel.add(inputPanel, BorderLayout.SOUTH);
camickr
  • 321,443
  • 19
  • 166
  • 288
2

camickr is right,you need to make an object of Message class and the variable messages needs to hold a reference to it:

messages =new Message( );

Then only you can access its instance variables and methods.

user1369975
  • 430
  • 1
  • 6
  • 19