1

I am trying to compare a string to a string value. Seems rather simple, however, the comparison is returning null. All I want is to output the matched value and ignore the null return. But the output is showing the null value as well. I have tried this in various ways, but it keeps showing the null value.

   class ActionMovie extends cdinventoryprogram {

    private String Atitle;
    private double Avalue;
    private double Rstock;
    private String Ctitle;

    public ActionMovie(String title, int itemNumber, int numberofUnits, double unitPrice ){
        Atitle = title;
        Avalue = numberofUnits * unitPrice;
        Rstock = unitPrice * .05;}

    public String getActionTitle(){

        if (Atitle.equals("Matrix")){
        Ctitle = Atitle;
        }else if (!Atitle.equals("Matrix")){

        }
        return Ctitle;
    }

}

public class cdinventoryprogram {

public static void main(String[] args) {

        ActionMovie myAction[] = new ActionMovie[DEFAULT_LENGTH];

        myAction[0] = new ActionMovie ("The Illusionist", 1, 5, 15.99);
        myAction[1] = new ActionMovie ("Matrix", 2, 3, 14.99);
        myAction[2] = new ActionMovie ("Old School", 3, 6, 12.99);

            for ( ActionMovie currentActionMovie : myAction ){
            CAction = currentActionMovie.getActionTitle();
            JOptionPane.showMessageDialog( null, "Your action movie is: " + CAction);

}} }

1
  • Ctitle not initialized. Plus Java variables start with lowercase letter. Commented Jan 14, 2011 at 22:12

5 Answers 5

2

So you want to return an empty string instead of null? Then just do that so.

if (Atitle.equals("Matrix")){
    Ctitle = Atitle;
} else {
    Ctitle = "";
}
return Ctitle;

Note that the second if is pretty redundant, so I removed it as well.

Be careful with potential NullPointerException whenever Atitle is actually null. To prevent from that, you'd like to do it as follows since "Matrix" is never null.

if ("Matrix".equals(Atitle)){
    Ctitle = Atitle;
} else {
    Ctitle = "";
}
return Ctitle;

Unrelated to the problem, I'd suggest to get yourself through the standard Java naming conventions. Class names ought to start with uppercase and variable names with lowercase. This way the code is better readable for every other Java developer (including yourself in the future).


Update as per the comment:

No, I don't want to return anything if the strings don't match. It is returning a value if not equal. – user569127 3 mins ago

Then just return null. This only shifts the problem to displaying the value. If it is null then just don't display it. E.g.

if (CAction != null) {
    JOptionPane.showMessageDialog(null, "Your action movie is: " + CAction);
}

The confusion in this question is likely caused by your confusion of the terms "returning" and "displaying".

Sign up to request clarification or add additional context in comments.

3 Comments

plus one for none invasive suggestion
No, I don't want to return anything if the strings don't match. It is returning a value if not equal.
Ah, ok. I see what you are saying. Thank you for your help.
2

Change:

private String Ctitle;

To:

private String Ctitle = ""

3 Comments

Thanks that got rid of the null value. However, on output it is still seeing the null and giving me 3 outputs. There are 3 items in the array so it is outputting the unmatched strings and the matched one.
I consider direct initialization of non-final variables a poor practice however, especially if it's an empty string. Plus this doesn't solve the problem whenever the getActionTitle() is called on the same instance after that Atitle has been changed by possibly a setter.
Maybe I am going about this the wrong way. I don't want anything returned if the value is null. I want it to display the matched string only, and ignore the nulls.
0

If Atitle is not equal to Matrix, Ctitle will always be null. Set a default value for Ctitle so it never returns null.

Comments

0

The problem is that your member variable CTitle is initialized (by default) to null and your getActionTitle() method only updates its value if it equals the string "Matrix" (otherwise there is no change to its value). So that method will return its default value (null) which prints as the string "null".

If you don't want to see "null" when you are printing the return value somewhere then you should check for null and act accordingly, or change the default value of CTitle to, say, the empty string ("").

Comments

0

Are you certain that Atitle is getting set properly and that it at one point is equal to "Matrix"? I would suggest setting a break point in your code to check the value of Atitle before it hits the comparison. You might also initialize Ctitle in your constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.