0

I have two classes,

SymString.java

import java.lang.StringBuffer;

public class SymString {
    private String str;
    private int temp = 0, flag = 0;

    public SymString(String paraStr) {
        str = paraStr;
    }

    StringBuffer strBuffer = new StringBuffer(str);

    public int getLength() {
        return (strBuffer.length());
    }

    public boolean isSymStr() {
        for (temp = 0; temp <= getLength(); temp++) {
            if (strBuffer.charAt(temp) == strBuffer.charAt(getLength() - (temp + 1))) {
                flag++;
            }
        }
        if (flag == getLength()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isSymStr2() {
        if (strBuffer.reverse().equals(strBuffer)) {
            return true;
        } else {
            return false;
        }
    }
}

TestSymStr.java


class TestSymStr {

    public static void main(String[] args) {
        SymString Test1 = new SymString("lmfao let's try it out.");
        System.out.println("Is the string symmetric?");
        System.out.println(Test1.isSymStr2());
        System.out.println(Test1.isSymStr());
    }

}

and it just throws the exception, seems like the str has not be assigned. Why?

Exception in thread "main" java.lang.NullPointerException
    at java.lang.StringBuffer.<init>(StringBuffer.java:139)
    at SymString.<init>(SymString.java:13)
    at TestSymStr.main(TestSymStr.java:5)
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
Paul Zhang
  • 305
  • 2
  • 7
  • 2
    `StringBuffer strBuffer = new StringBuffer(str);` **is** being executed! But it executes before the body of the constructor, and `str` doesn't get it's (non-null) value until the the constructor executes. So you basically end up with `new StringBuffer(null)` and KABOOM! Solution: move the initialization of `strBuffer` from the declaration into the constructor. – Kevin Anderson Sep 11 '19 at 03:46
  • What do you mean by "overflow" @Goion? What can overflow here? – Kevin Anderson Sep 11 '19 at 04:03
  • @KevinAnderson Nvm. I though he is declaring the object of class itself. – Yoshikage Kira Sep 11 '19 at 04:06
  • improved formatting – Reegan Miranda Sep 11 '19 at 06:30

1 Answers1

1

In your code private String str; initializes the String with null. When you call the constructor the it tries to initialize the StringBuffer strBuffer = new StringBuffer(str); as well. Where the constructor of StringBuffer is called. Which has code like below:

/**
     * Constructs a string buffer initialized to the contents of the
     * specified string. The initial capacity of the string buffer is
     * {@code 16} plus the length of the string argument.
     *
     * @param   str   the initial contents of the buffer.
     */
    public StringBuffer(String str) {
        super(str.length() + 16);
        append(str);
    }

Because before executing the constructor code the Class variables are initialized. So till here str is null, which is causing this NullPointerException. So you can achieve it create strBuffer using defaulr constructor and append the str inside the constructor as below.

    private String str;
    private int temp = 0, flag = 0;
    StringBuffer strBuffer = new StringBuffer();

    public SymString(String paraStr) {
        str = paraStr;
        strBuffer.append(str);
    }


    public int getLength() {
        return (strBuffer.length());
    }

But even after that you code will break because in isSymStr() method you are iterating from 0 index to the length, while you need to iterate till length -1 as below.

public boolean isSymStr() {
        for (temp = 0; temp < getLength(); temp++) {
            if (strBuffer.charAt(temp) == strBuffer.charAt(getLength() - (temp + 1))) {
                flag++;
            }
        }
        if (flag == getLength()) {
            return true;
        } else {
            return false;
        }
    }
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47