NullPointerException 但正在编译? [英] NullPointerException but compiling?

查看:13
本文介绍了NullPointerException 但正在编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的命令行游戏.我有很多功能什么的,这里只贴必要的.

I'm writing a simple command line game. I've got many functions and all, and will only post the essential here.

问题:程序可以编译,但是当调用 levelup() 并选择一个数字时,我得到了这个:

Problem: The program compiles but when levelup() is called and a number is chosen, I get this:

        You have 5 skill points to spend.
        What would you like to upgrade?
        [1:]    STR                     [2:]    DEF
1
Exception in thread "main" java.lang.NullPointerException
        at Game.levelup(cmdquest.java:300)
        at Game.start(cmdquest.java:336)
        at Menu.show_menu(cmdquest.java:195)
        at cmdquest.main(cmdquest.java:263)

这是我的代码:

class Player{
    String name;
    int hp;
    int str;
    int def;
    String  eff;

    Player(String n) {
    name = n;
    hp = 100;
    str = 1;
    def = 1;
    eff = "none";
    }
}


class Game{

    static Player p;

    static void levelup(){
        while (points > 0){
            System.out.println("	[1:]	STR			[2:]	DEF");
            int lvlup = kb.nextInt();

            switch (lvlup){
                case 1: p.str++;
                    break;
                case 2: p.def++;
                    break;
            }

            points--;
        }

    //variables
    static Scanner kb = new Scanner(System.in);
    static int points = 5;

    }

static void start(){

        System.out.print("				And so our adventure starts.....");

        System.out.print("	What's your name: ");
        String nome = kb.next();
        Player p = new Player(nome);

        System.out.println("	Hello " + p.name);
        System.out.println("	You have 5 skill points to spend.
	What would you like to upgrade?");
        levelup();

    }



class cmdquest{

public static void main(String args[]) throws Exception{

    Scanner kb = new Scanner(System.in);

    //Importing foes.txt to create objects of foes
    java.io.File file = new java.io.File("foes.txt");
    Scanner imp = new Scanner(file);  

    for(int i =0; i<3; i++){
        foes[i]=foe.leDados(imp);
    }
    //____________________________________________

    Game.start();

}
}

有人能在这里指出我正确的方向吗?我究竟做错了什么?我感觉这是玩家"类和在游戏"类中创建的对象的类问题.

Can anyone point me in the right direction here? What am I doing wrong? I sense it's a class problem with the class "Player" and the object being created in the "Game" class.

推荐答案

你得到一个 NullPointerException 因为 pnull.你在这里做了什么:

You get a NullPointerException because p is null. What you've done here:

Player p = new Player(nome);

是声明一个local变量p.静态类变量 p 未受影响,因此它保持 null.

is declare a local variable p. The static class variable p is untouched, so it remains null.

这称为 阴影(JLS,第 6.4 节.1):

某些声明可能在其范围的一部分被另一个声明遮蔽相同名称的声明,在这种情况下,不能使用简单名称用于指代声明的实体.

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

...

一个名为 n 的类型的声明 d 隐藏了任何类型的声明在 d 出现的点范围内的其他名为 n 的类型整个范围 d.

A declaration d of a type named n shadows the declarations of any other types named n that are in scope at the point where d occurs throughout the scope of d.

去掉Player,这样对静态类变量p的引用就是你想要的:

Remove Player, so the reference to the static class variable p is what you want:

p = new Player(nome);

这篇关于NullPointerException 但正在编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆