This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2013/1/5

NullPointerException

1
2
3
erdelf erdelf

2013/1/5

#
I have this code and in the line
bar.setValue(value); 
I get an NullPointerException. The setValue method is executed by an another actor a few seconds after world initiating.
Bar bar;
boolean created;
public ActorL()
{
   bar = new Bar();
   created = false;
   .....
}

public void act()
{
   if(!created)
   {
      getWorld().addObject(bar,0,0);
      created = true;
   }
   true;
}
public void setValue(int value)   
{  
    bar.setValue(value); 
}
danpost danpost

2013/1/5

#
erdelf wrote...
I have this code and in the line
bar.setValue(value); 
I get an NullPointerException. The setValue method is executed by an another actor a few seconds after world initiating.
It would be the code in the other actor that the setValue method is executed that we would need to see (plus in context).
erdelf erdelf

2013/1/5

#
public void act()
{
        ActorL actl= (ActorL)      getOneIntersectingObject(ActorL.class);
        if(actl!=null)
        {
            actl.setValue(20);
        }
}
danpost danpost

2013/1/5

#
Line 4 checks to see if the ActorL object is created, but does not check to see if the bar in the ActorL object is created. This would mean a check to see if 'actl.created' is true. A better way to deal with this, though, is to change the code in the ActorL class to:
Bar bar;

public ActorL()
{
   bar = new Bar();
   .....
}

public void addedToWorld(World world)
{
      world.addObject(bar,0,0);
}

public void setValue(int value)   
{  
    bar.setValue(value); 
}
This ensures that once the ActorL object is in the world, the bar is also created and in the world.
erdelf erdelf

2013/1/5

#
didnt work, same error.
danpost danpost

2013/1/5

#
If there is nothing else in the ActorL class code that deals directly with the bar, maybe we need to look at the code for the bar class itself. But that would not seem right if you are getting the error on the setValue line. There has got to be something more that is being missed.
erdelf erdelf

2013/1/5

#
its a modified version of your bar
// AUTHOR: danpost
// Version: 2
// Last modified: February, 8, 2012
// 
// Bar class for use as a health bar or progress bar as well as for other types of measurements for visual of value in a bar form.
// DO NOT EDIT THIS CLASS as everything can be controlled through method calls.

import greenfoot.*;
import java.awt.Color;
import java.awt.Font;

public class Bar extends Interface
{
    private int barWidth = 15; // the width of the color portion of the bar
    private int barHeight = 5; // the height of the color portion of the bar
    private int breakPercent = 40; // the percentage amount that changes the color of the bar
    private int breakValue = 40; // in tandem with breakPercent
    private boolean usingBreakValue = true;
    private boolean breakLow = true; // when true, with low-percent values bar is dangerColor, else safeColor; reversed when false
    private Color backgroundColor = new Color(0, 0, 0, 0); // the background color of the entire object
    private Color textColor = new Color(0,0,0,0); // the color of all text and the frame of the bar itself
    private Color safeColor = Color.GREEN; // the color of the bar while in the safe range
    private Color dangerColor = Color.RED; // the color of the bar while in the danger range
    //  The color of the bar AT the breakpoint will be the average color between the safe color and the danger color
    private float fontSize = 18.0f; // the size of the text
    private int value = 0; // the current value of the bar
    private int maximumValue = 0; // the maximum value of the bar
    private int minimumValue = 0; // the minimum value of the bar
    private String referenceText = ""; // the title string (who or what the meter/bar is for)
    private String unitOfMeasure = ""; // the unit of measure of the bar (any quantitative standard of measurement) 
    private boolean showTextualUnits = true; // determines whether or not the textual quantity of the bar is to show
    private Actor actor;

    public Bar(Offender offender,String refText, String unitType, int initValue, int maxValue, Color safeColor2, Color dangerColor2)
    {
        this.actor = offender;
        referenceText = refText;
        unitOfMeasure = unitType;
        maximumValue = maxValue;
        safeColor = safeColor2;
        dangerColor = dangerColor2;
        add(initValue);
    }

    public Bar(Turret turret,String referenceText, String unitOfMeasure, int initValue, int maximumValue, Color safeColor, Color dangerColor)
    {
        this.actor = turret;
        this.referenceText = referenceText;
        this.unitOfMeasure = unitOfMeasure;
        this.maximumValue = maximumValue;
        this.safeColor = safeColor;
        this.dangerColor = dangerColor;
        add(initValue);
    }

    private void newImage()
    {
        //         int barValue = (int) (barWidth * value / maximumValue);
        int barValue = (int) (barWidth * (value - minimumValue) / (maximumValue - minimumValue));
        GreenfootImage leftImg = new GreenfootImage(referenceText + " ", (int) fontSize, textColor, backgroundColor);
        GreenfootImage rightImg = (showTextualUnits) ? new GreenfootImage(" " + value + " " + unitOfMeasure, (int) fontSize, textColor, backgroundColor) : new GreenfootImage(1, 1);
        int maxX = (leftImg.getWidth() > rightImg.getWidth()) ? leftImg.getWidth() : rightImg.getWidth();
        GreenfootImage barImg = new GreenfootImage(barWidth + 4, barHeight + 4);
        barImg.setColor(backgroundColor);
        barImg.fill();
        barImg.setColor(textColor);
        barImg.drawRect(0, 0, barImg.getWidth() - 1, barImg.getHeight() - 1);
        if (value > minimumValue)
        {
            if (breakLow)
            {
                if (value > (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue))) barImg.setColor(safeColor);
                else barImg.setColor(dangerColor);
            }
            else
            {
                if (value < (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue))) barImg.setColor(safeColor);
                else barImg.setColor(dangerColor);
            }
            if (value == (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue)))
            {
                int r = (int) ((safeColor.getRed() + dangerColor.getRed()) / 2);
                int g = (int) ((safeColor.getGreen() + dangerColor.getGreen()) / 2);
                int b = (int) ((safeColor.getBlue() + dangerColor.getBlue()) / 2);
                barImg.setColor(new Color(r, g, b));
            }
            barImg.fillRect(2, 2, barValue, barHeight);
        }
        int sumX = 2 * maxX + barImg.getWidth();
        int maxY = 0;
        if (leftImg.getHeight() > maxY) maxY = leftImg.getHeight();
        if (barImg.getHeight() > maxY) maxY = barImg.getHeight();
        if (rightImg.getHeight() > maxY) maxY = rightImg.getHeight();
        GreenfootImage image = new GreenfootImage(sumX, maxY);
        image.setColor(backgroundColor);
        image.fill();
        image.drawImage(leftImg, maxX - leftImg.getWidth(), (image.getHeight() - leftImg.getHeight()) / 2);
        image.drawImage(barImg, maxX, (image.getHeight() - barImg.getHeight()) / 2);
        image.drawImage(rightImg, barImg.getWidth() / 3 * 2, (image.getHeight() - rightImg.getHeight()) / 2);
        setImage(image);
    }

    public void add(int amount)
    {
        value += amount;
        checkValue();
        newImage();
    }

    public void subtract(int amount)
    {
        value -= amount;
        checkValue();
        newImage();
    }

    public void act()
    {
        World world = (AreaOne)getWorld();
        if(actor.getWorld() != null)
        {
            try
            {
                setValue(((Offender)actor).getHealth());
                setLocation(actor.getX(),actor.getY()-15);
            }catch(ClassCastException x)
            {
                setValue(((Turret)actor).getHealth());
                setLocation(actor.getX()-5,actor.getY()-20);
            }
        } else
        {
            getWorld().removeObject(this);
        }
    }

    private void checkValue()
    {
        if (value < minimumValue) value = minimumValue;
        if (value > maximumValue) value = maximumValue;
    }

    public int getValue() { return value; }

    public int getBarWidth() { return barWidth; }

    public int getBarHeight() { return barHeight; }

    public int getBreakPercent() { return breakPercent; } // use boolean getUsingBreakValue() method before calling (if false)

    public int getBreakValue() { return breakValue; } // use boolean getUsingBreakValue() method before calling (if true)

    public boolean getBreakLow() { return breakLow; }

    public Color getBackgroundColor() { return backgroundColor; }

    public Color getTextColor() { return textColor; }

    public Color getSafeColor() { return safeColor; }

    public Color getDangerColor() { return dangerColor; }

    public float getFontSize() { return fontSize; }

    public int getMaximumValue() { return maximumValue; }

    public int getMinimumValue() { return minimumValue; }

    public String getReferenceText() { return referenceText; }

    public String getUnitOfMeasure() { return unitOfMeasure; }

    public boolean getShowTextualUnits() { return showTextualUnits; }

    public void setValue(int val) { value = val; checkValue(); newImage(); }

    public void setBarWidth(int width) { if (width > 9) { barWidth = width; newImage(); } }

    public void setBarHeight(int height) { if (height > 1) { barHeight = height; newImage(); } }

    public void setBreakPercent(int percent) { if (percent >= 0 && percent <= 100) { breakPercent = percent; usingBreakValue = false; newImage(); } }

    public void setBreakValue(int brkVal) { if (brkVal >= minimumValue && brkVal <= maximumValue) {breakValue = brkVal; usingBreakValue = true; newImage(); } }

    public void setBreakLow(boolean lowBreak) { breakLow = lowBreak; newImage(); }

    public void setBackgroundColor(Color color) { backgroundColor = color; newImage(); }

    public void setTextColor(Color color) { textColor = color; newImage(); }

    public void setSafeColor(Color color) { safeColor = color; newImage(); }

    public void setDangerColor(Color color) { dangerColor = color; newImage(); }

    public void setFontSize(float size) { if (size > 7) { fontSize = size; newImage(); } }

    public void setMaximumValue(int maxVal) { if (maxVal > minimumValue) { maximumValue = maxVal; newImage(); } }

    public void setMinimumValue(int minVal) { if (minVal < maximumValue) { minimumValue = minVal; newImage(); } }

    public void setReferenceText(String refText) { referenceText = refText; newImage(); }

    public void setUnitOfMeasure(String uom) { unitOfMeasure = uom; newImage(); }

    public void setShowTextualUnits(boolean show) { showTextualUnits = show; newImage(); }
}
danpost danpost

2013/1/5

#
Sorry, not what I had here. Let me think this through.
erdelf erdelf

2013/1/5

#
I wrote that
danpost danpost

2013/1/5

#
What is the constructor 'Bar()'? is it located in the 'Interface' class? please show.
erdelf erdelf

2013/1/5

#
I didn't wrote the full bar call, sry.
   bar = new Bar(this, "", "", 100, 100, Color.GREEN, Color.RED);
danpost danpost

2013/1/5

#
Please copy/paste the error message you are getting.
erdelf erdelf

2013/1/5

#
java.lang.NullPointerException
	at Turret.damage(Turret.java:58)
	at Missile.act(Missile.java:37)
	at TurretMissile.act(TurretMissile.java:18)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
to explain, TurretMissile, which damages, is a subclass of Missile, turret is the ActorL I was talking about.
danpost danpost

2013/1/5

#
The line 'bar.setValue(value)' is line 58 in the Turret class? Show the whole class please.
erdelf erdelf

2013/1/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
/**
 * Write a description of class Turret here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turret extends Actor
{    
    private int health, range, speed, distance, power, time;
    private double times;
    private String missile;
    Bar bar;
    public Turret(int health, int range, int speed, int distance, String missile, int power)
    {
        time = 0;
        this.health = health;
        this.range = range;
        this.speed = speed;
        this.distance = distance;
        this.missile = missile;
        this.power = power;
    }

    public void addedToWorld(World world)  
    {  
        bar = new Bar(this, "", "", health, health, Color.GREEN, Color.RED);
        world.addObject(bar, 0,0);
    }  

    public Turret() { }

    public void act()
    {
        time--;
        if(time>0)
            return;
        List<Offender> off= getObjectsInRange(range,Offender.class);
        if(off.size()==0)
            return;
        time = speed;
        shoot((Offender)off.get(0));
    }

    public void shoot(Offender off)
    {
        turnTowards(off.getX(),off.getY());
        Actor missiles = new Missile();
        if(missile.equals("TurretMissile")) { missiles = new TurretMissile(getRotation(),power+1,speed, distance,true);}
        getWorld().addObject(missiles,getX(),getY());
    }

    public int getHealth()  {   return health;  }

    public void damage(int value)   { health-=value; bar.setValue(health); } 
}
There are more replies on the next page.
1
2
3