0

我看了又看,找不到答案。但这是我的问题。我想根据主游戏类中的刻度来更改另一个类中的 int 。这是代码:

痕迹:

    02-16 02:10:58.605: W/dalvikvm(1578): threadid=13: thread exiting with uncaught                 exception (group=0x40c5ca68)
    02-16 02:10:58.615: E/AndroidRuntime(1578): FATAL EXCEPTION: Thread-16720
    02-16 02:10:58.615: E/AndroidRuntime(1578): java.lang.NullPointerException
    02-16 02:10:58.615: E/AndroidRuntime(1578):     at    com.mojang.ld22.Game.tick(Game.java:374)
    02-16 02:10:58.615: E/AndroidRuntime(1578):     at   com.mojang.ld22.Game.iterate(Game.java:300)
    02-16 02:10:58.615: E/AndroidRuntime(1578):     at com.mojang.ld22.GameActivity$4.run(GameActivity.java:136)
    02-16 02:10:58.615: E/AndroidRuntime(1578):     at java.lang.Thread.run(Thread.java:856)

Game.java 片段

    public void tick() {
    tickCount++;

    ...
    ...
    ...

        if (tickCount % 1000 == 0) {
            if (rising) {
                lightlvl++;
Would error here too        clock.time++;    <-----------

                if (lightlvl >= 8) {
                    rising = false;
                }

            } else {
                lightlvl--;
    LINE 374 ---------->        clock.time--;    <------------

                if (lightlvl <= -2) 
                    rising = true;
            }
        }
    }

}  

和时钟.java

    package com.mojang.ld22.entity;

    import com.mojang.ld22.crafting.Crafting;
    import com.mojang.ld22.entity.particle.TextParticle;
    import com.mojang.ld22.entity.particle.TimeClock;
    import com.mojang.ld22.gfx.Color;
    import com.mojang.ld22.gfx.Screen;
    import com.mojang.ld22.screen.CraftingMenu;
    import com.mojang.ld22.item.FurnitureItem;

    public class Clock extends Furniture {


/**
 * 
 */
private static final long serialVersionUID = 3146189783597003582L;

Player player;
public int time = 0;
public int clock = 1;
public boolean rising = false;

public Clock() {
    super("Clock");
    col = Color.get(-1, 100, 321, 431);
    sprite = 10;
    xr = 3;
    yr = 2;

}

@Override
public void tick(){
    if (time <= 0)
        clock = 1;
    if (time == 1)
        clock = 1;
    if (time == 2)
        clock = 2;
    if (time == 3)
        clock = 3;
    if (time == 4)
        clock = 4;
    if (time == 5)
        clock = 5;
    if (time == 6)
        clock = 6;
    if (time == 7)
        clock = 7;
    if (time == 8);
        clock = 8;
}



@Override
public void render(Screen screen){
    int col = Color.get(-1, 531, 000, 534);
    //incorrect times TODO
    if (clock == 8)
        screen.render(x - 8, y - 8, 7 + 11 * 32, col, 0);
    if (clock == 6)
        screen.render(x - 8, y - 8, 8 + 11 * 32, col, 0);
    //noon midnight
    if (clock == 5)
        screen.render(x - 8, y - 8, 9 + 11 * 32, col, 0);
    if (clock == 4)
        screen.render(x - 8, y - 8, 10 + 11 * 32, col, 0);
    //Before noon after noon
    if (clock == 3)
        screen.render(x - 8, y - 8, 11 + 11 * 32, col, 0);
    if (clock == 2)
        screen.render(x - 8, y - 8, 12 + 11 * 32, col, 0);
    //730pm 530pm
    if (clock == 1)
        screen.render(x - 8, y - 8, 13 + 11 * 32, col, 0);
    if (clock == -1)
        screen.render(x - 8, y - 8, 14 + 11 * 32, col, 0);
}
}

是的,这不漂亮。当我无法将任何 int 从 game.java 拉到 clock.java 时,这基本上是我尝试的所以这是为了测试发送它。我不能使用单独的刻度,因为在玩家从他/她的库存中拉出它之前不会创建clock.java。

我希望这是从那时起。如果您需要,我可以提供更多代码。

4

1 回答 1

4

rising为 false,因此执行 else 分支并且clock为 null,这将引发 NullPointerException。

顺便说一句,您可以这样编写 tick 方法:

public void tick(){
    if (time <= 0) clock = 1;
    else if (time <= 8) clock = time;
}
于 2013-02-16T09:17:50.307 回答