0

I'm pretty sure these questions have been asked millions of times but unfortunately, I cannot find the answer.

Exception in thread "main" java.lang.NullPointerException
    at com.bluebitgames.visualnovel.Game.initStatesList(Game.java:30)
    at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
    at com.bluebitgames.visualnovel.Game.main(Game.java:77)

I know that the java.lang.NullPointerException means that the variable has a null value and so I must change it but I really have no idea.

package com.bluebitgames.visualnovel;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame {
        public static final int SCREEN_WIDTH        = 1024;
        public static final int SCREEN_HEIGHT        = 640;
        public static final String title        = "Visual Novel";
        public static final String version        = "";
        public static final String vDate        = "03/07/12";

        // States
        public static final int menu  = 0;
        public static final int play  = 1;

        // Main font
        public static UnicodeFont font;

        public Game(String title) {
                super(title);
                this.addState(new Play(play));
        }

        public void initStatesList(GameContainer gc) throws SlickException {
                this.getState(menu).init(gc, this);     //line 30
                this.getState(play).init(gc, this);
                this.enterState(menu);
        }



        @SuppressWarnings("unchecked")
        public static UnicodeFont mainFont(int size, String name) {
                if(name.equals("default")) {
                        name        = "tempesta";
                }
                UnicodeFont font;
                try {
                        font        = new UnicodeFont("res/fonts/"+name+".ttf", size, false, false);
                        font.addAsciiGlyphs();
                        font.addGlyphs(400, 600);
                        font.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
                        font.loadGlyphs();
                        return font;
                } catch(SlickException e) {
                        System.out.println("Could not load font.");
                        e.printStackTrace();
                        return null;
                }

        }

        public static void main(String[] args) {
                String os        = System.getProperty("os.name");
                if(os.contains("Windows")) {
                        os        = "windows";
                } else if(os.contains("Mac")) {
                        os        = "macosx";
                } else {
                        os        = "linux";
                }

                System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/lib/natives/"+os);
                AppGameContainer agc;
                try {
                        agc        = new AppGameContainer(new Game(title+" v"+version));
                        agc.setDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, false);
                        //agc.setFullscreen(true);
                        agc.setTargetFrameRate(60);
                        agc.setShowFPS(true);
                        agc.setVSync(true);
                        agc.start();      //line 77
                } catch(SlickException e) {
                        e.printStackTrace();
                }
        }
}

(The StateBasedGame and the AppGameContainer are classes of a .jar called 'Slick' and I am not sure how to bring the coding up for it. Sorry!)

I hope someone can help me with this problem.

EDIT: So sorry about that. just edited that.

2
  • 1
    As with any NPE question you must have read, the key is identifying which line is throwing it. So which one is it? Which line is line 30, Game.java:30 as the exception stacktrace tells you? Commented Dec 15, 2013 at 4:17
  • As I thought, then perhaps my answer and that of Elliott are correct. Commented Dec 15, 2013 at 4:23

2 Answers 2

2

I don't know Slick2D, but you appear to be adding a Play State but not a Menu State in your constructor, and it seems the NPE is thrown when you attempt to call a method on the Menu State. I SWAG, but perhaps you should initialize the Menu State as well in the constructor.

1000 pardons and a deleted answer if this is wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you answer. I made a big mistake on my half. Thanks for pointing that out.
@Colby, why everyone knows what SWAG is, a Silly Wild Ass,...essment Guess. Yeah, that;s the tick!
1

You seem to be missing the "Menu" State. I suggest trying to add such a state in your constructor, it should be as easy as something like this

public Game(String title) {
  super(title);
  this.addState(new Play(play));
  this.addState(new Menu(menu)); // Assuming you have a Menu somewhere...
}

1 Comment

Thank you so much for you answer. I'm a real noob when it comes to Java since I just switched from 1 type of coding from another. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.