2

I have code that generates a random number form 0-1 3 times and I need it to be added to a variable so it turns into a binary number.So, in theory, this would run three times and possibly give me 101;

String storage = null;
        int i = 0;
        while (i < 3) {
            int binny = this.giveMeBinary();
            storage.concat(String.valueOf(binny));
            i++;
        }

        int ans = Integer.parseInt(storage);

But when I try and run this I get the NullPointerException error for storage. Is there a way to just "add" a string to the variable?

the method giveMeBinary just returns a 0 or a 1.

3
  • 1
    also note that storage.concat(...) will not change the value of storage. You need to do storage = storage.concat(...). Commented Jan 31, 2012 at 17:18
  • As an aside, if your goal is ans and not storage, you can do this easily with bit arithmetic: while(i<3) {ans <<= 1; ans |= this.giveMeBinary();i++;} or whle(i<3) { ans |= this.giveMeBinary()<<i;i++;} Commented Jan 31, 2012 at 17:19
  • ahhhhh yes....I see what you mean...it doesn't store the new value into storage. Commented Jan 31, 2012 at 17:21

10 Answers 10

10

Your problem is that you are initializing the string to null. Doing something like so should solve your problem:

        String storage = "";
        int i = 0;
        while (i < 3) {
            int binny = this.giveMeBinary();
            storage += (String.valueOf(binny));
            i++;
        }

        int ans = Integer.parseInt(storage);

However, concatenating strings in such a manner is not recommended. What you can do is use a StringBuilder like so:

        StringBuilder storage = new StringBuilder();
        int i = 0;
        while (i < 3) {
            int binny = this.giveMeBinary();
            storage.append(String.valueOf(binny));
            i++;
        }

        int ans = Integer.parseInt(storage.toString());
Sign up to request clarification or add additional context in comments.

Comments

1

You get the NullPointerException because you set the variable storage to null. You should start with

String storage = "";

1 Comment

... and then get an exception in Integer.parseInt because storage still refers to an empty string after the loop?
1

If you want to use String concatenation then just initialise storage to "" (empty string) then use

storage += String.valueOf(binny);

but if you are looping and building Strings you should really use StringBuilder since Strings are immutable

StringBuilder buffer = new StringBuilder();

then

buffer.append(binny);

Comments

1

You have two problems:

  • You're never assigning a non-null value to storage, but you're calling the concat method on it. That will always throw a NullPointerException

  • You're assuming String.concat will modify the existing string value. It doesn't. Strings are immutable in Java.

I would suggest using a StringBuilder instead, and calling append in the loop:

int i = 0;
StringBuilder builder = new StringBuilder();
while (i < 3) {
    int binny = this.giveMeBinary();
    builder.append(binny);
    i++;
}
int ans = Integer.parseInt(builder.toString());

Quite why you're then parsing a binary number such as "011" as if it were a decimal number is a different matter. What do you actually want the result to be? Do you really want the numbers 0, 1, 10, 11, 100, 101, 110 or 111?

Comments

1

If you want a binary number, i.e. a random number between 0 and 7 or 000 and 111 in binary.

int ans = giveMeBinary() * 4 + giveMeBinary() * 2 + giveMeBinary();

however if you want a decimal number which looks like a binary number.

int ans = giveMeBinary() * 100 + giveMeBinary() * 10 + giveMeBinary();

Comments

0

After you correct NullPointerException, try this:

storage = storage + String.valueOf(binny)

...and then read about StringBuilder.

Comments

0

try this:

String storage = "";

Comments

0

It throws a NullPointerException because storage is null. Try the following

StringBuilder storage = new StringBuilder();
        int i = 0;
        while (i < 3) {
            int binny = this.giveMeBinary();
            storage.append(String.valueOf(binny));
            i++;
        }

        int ans = Integer.parseInt(storage.toString());

Comments

0

variable storage is not initialized..Following code will throw NPE

storage.concat(....)

Comments

0

The reason you get a NullPointerException is that the "storage" variable is never initialized. So the first call to "storage.concat" will try to perform an operation on a NullPointer.

For doing string concatenation you'll want to use a mutable class like StringBuilder, the String class is immutable (you cannot modify an instance of it).

Here's an example using StringBuilder:

StringBuilder storage = new StringBuilder();
int i = 0;
while (i < 3) {
   int binny = this.giveMeBinary();
   storage.append(binny);
   i++;
}

int ans = Integer.parseInt(storage.toString());

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.