Jay
Jay

Reputation: 39

Regarding the "Null Pointer" monster

At the risk of sounding like a first-time programmer, I am asking the all-too-ubiquitous question regarding the "Null Pointer" monster. Here is a code segment:

public class pixel
{
    private String type = "empty";

    private void Type(String t){type = t;}
    private String Type(){if(type!=null)return type; else return "empty";}
}

Realise this: at no point in my entire code do I pass null in to Type. Why does type not return a value? (As a side note, I am calling this method in my extension of paintComponent. To rule out the possibility of a null array (in which the pixel objects are stored), I made the appropriate check)

Upvotes: 2

Views: 186

Answers (6)

Roland Illig
Roland Illig

Reputation: 41617

If you know that you never pass null for the type, use this pattern:

public final class Pixel {

  private String type;

  private Pixel(String type) {
    setType(type);
  }

  private void setType(String type) {
    type.getClass();
    this.type = type;
  }

  private String getType() {
    return type;
  }
}

Before each assignment to the type field, the code checks that the assigned value is non-null. This, in combination with the getter and setter ensures that this invariant will hold true. Unless of course this is an inner class and you modify it from the enclosing code.

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

I agree that Type() will never return null. It won't return "empty" unless Type(String) has never been called or the most recent call passed null.

As a note, your class design is confusing (e.g. two methods with same name but different purposes, no constructor).

Upvotes: 3

Adeel Ansari
Adeel Ansari

Reputation: 39887

First of all, say it explicitly i.e. setType(String t) and getType(). Now check for null in your setter, instead.

public class pixel {

    private String type = "empty";

    private void setType(String t){if (t!=null) type = t;}
    private String getType(){return type;}
}

Aha! I just noticed that your method are private. Are you serious about that? Your type variable is never going to change, loosely speaking. My suggested code, without any knowledge of your application,

public class pixel {

    private String type = "";

    public void setType(String t){
        if (t != null) 
            type = t;
    }

    public String getType(){
        return type;
    }
}

Upvotes: 1

zengr
zengr

Reputation: 38899

Maybe I am missing the point here, but isn't it obvious (not specific to java), let's postmortem the code:

public class pixel
{
    private String type = "empty"; //type string with a value of "empty" is available everywhere in the class --- (1)

    private void Type(String t)
    {
        type = t;
    }
    private String Type()
    {
        if(type!=null) //How can type is null or anything else, you have defined it in (1)
        {
            return type; 
        }

        else
        { 
            return "empty"; //Type RETURNS "empty" as a value.
        }
    }

    public static void main(String[] args)
    {
        pixel obj = new pixel();
        System.out.println(obj.Type()); //You call Type() here
    }
}

If you use a global variable anywhere in the scope of the class, if will be available to you.

Upvotes: 0

darioo
darioo

Reputation: 47183

In this case, type will never be null, but it's a very simple case. And by the way, if it does become null in some case, you should return an empty string "", or just null, depending on your design. "empty" might be tolerated in this case, but don't use stuff like this for real.

Upvotes: 0

leppie
leppie

Reputation: 117220

Follow the logic.

From your code, type can never be null, so there is no point checking for that.

Upvotes: 0

Related Questions