在另一个类中更改int时出现空指针异常

时间:2013-02-16 09:15:41

标签: java android nullpointerexception int

我看了看,但找不到答案。但这是我的问题。我想基于主Game类中的ticks更改另一个类中的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 Snippet

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

}  

和Clock.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。

我希望这是因为。如果需要,我可以提供更多代码。

1 个答案:

答案 0 :(得分:4)

rising为false,因此执行else分支并且clock为null,这会抛出NullPointerException。

顺便说一下,你可以像这样编写tick方法:

public void tick(){
    if (time <= 0) clock = 1;
    else if (time <= 8) clock = time;
}