gpt4 book ai didi

java - 编码中线程 "main"java.lang.NullPointerException 异常

转载 作者:行者123 更新时间:2023-12-01 23:21:13 30 4
gpt4 key购买 nike

我很确定这些问题已经被问了数百万次,但不幸的是,我找不到答案。

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)

我知道 java.lang.NullPointerException 意味着该变量具有空值,因此我必须更改它,但我真的不知道。

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();
}
}
}

(StateBasedGame 和 AppGameContainer 是名为“Slick”的 .jar 的类,我不知道如何为其提供编码。抱歉!)

希望有人能帮我解决这个问题。

编辑:对此感到非常抱歉。刚刚编辑过。

最佳答案

我不知道Slick2D,但您似乎在构造函数中添加了播放状态而不是菜单状态,并且当您尝试调用菜单状态上的方法时似乎会抛出NPE。我很高兴,但也许你也应该在构造函数中初始化菜单状态。

如果答案有误,请原谅并删除答案。

关于java - 编码中线程 "main"java.lang.NullPointerException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20591140/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com