-1

I was trying to make a code to answer to another person's problem... and I came out with another problem.

The idea would be a program that asks you to input numbers so it sorts them and prints them out from higher to lower.

This is what I came with:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Input: ");
    Integer input = new Integer(Integer.parseInt(scanner.nextLine()));
    scanner.close();
    Integer[] numbers = new Integer[input.toString().length()];

    for (Integer value : numbers) {
        while (input > 1) {
            value = input % 10;
            input /= 10;
        }
    }

    Arrays.sort(numbers, Collections.reverseOrder());

    int a = 0;
    for (Integer value : numbers) {
        if (a < numbers.toString().length()) {
            System.out.print(value + ", ");
            a++;
        } else {
            System.out.println(value + ".");
        }
    }
}

But, being Input: 292816374 the input, this is what the console says:

Exception in thread "main" java.lang.NullPointerException
at java.util.Collections$ReverseComparator.compare(Unknown Source)
at java.util.Collections$ReverseComparator.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at StartingPoint.main(StartingPoint.java:21)

The problem is that it is trying to sort null integers, but I don't know why it detects numbers as a null array when it's already been initialized. If I change the Integers to ints there aren't exceptions, but it prints out zeroes.

Why does this happen?

SpaceCore186
  • 586
  • 1
  • 8
  • 22
  • 1
    It can't be a duplicate because I did initialize the array. – SpaceCore186 May 11 '16 at 06:24
  • you are iterating number using forecah but you have assigned length to input[] (good part) an your number[] is empty(bad part) – bananas May 11 '16 at 06:54
  • The problem was due to that I was trying to assign each `numbers`'s field by refference and not by explicitly mentioning them. That's why `numbers` was empty. – SpaceCore186 May 11 '16 at 06:56

3 Answers3

6

You have created the array with

Integer[] numbers = new Integer[input.toString().length()];

Integer is an object type, so all the values in the array start off as null. If it were int, then it would be an array of primitives, which would all be initialized to 0.

As you want to update the array value try

 for (int x = 0; x < numbers.length; x++) {

      numbers [x] = value; // whatever
 }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • In that line of code what I'm doing is setting `numbers`s size. I initialize it in the enhanced `for` loop. – SpaceCore186 May 11 '16 at 06:23
  • 7
    @JavaNoob you don´t initialize them in the `for` loop. See [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – SomeJavaGuy May 11 '16 at 06:24
  • 2
    "You can not iterate through values which do not exist" You CAN. You are simply iterating thru an array with `null`s, so `value` is simply `null` for each iteration. Of course the original logic is still flawed, but your explanation is wrong. And even OP has initialized the array with `Integer` instances, his way to populate data by the loop is still wrong and do not give correct result. – Adrian Shum May 11 '16 at 06:29
  • 3
    @ScaryWombat So my problem is due to that I'm trying to assign each `numbers`'s field by reference and not doing this by mentioning each one of them. Thanks! – SpaceCore186 May 11 '16 at 06:31
  • 1
    @AdrianShum Yes, I did not explain that well. – Scary Wombat May 11 '16 at 06:34
  • @JavaNoob remember that `Integer`s are immutable – Scary Wombat May 11 '16 at 06:36
  • @ScaryWombat Situation won't change even Integer is mutable, because OP is doing `value = newValue;` which is simply making `value` (which is an object ref) referring a new object. – Adrian Shum May 11 '16 at 06:38
  • @AdrianShum Thanks, explanations are not coming easy today. – Scary Wombat May 11 '16 at 06:41
2
for (Integer value : numbers) {
    while (input > 1) {
        value = input % 10;
        input /= 10;
    }
}

value = input % 10; is not doing what you are expecting: it will NOT set the value in the array. It is just like:

Integer i = numbers[0];  // assuming numbers[0] is 0 at this moment
i = 100;
// numbers[0] is STILL 0

You have to assign value to the array explicitly.

A suggestion to do what you want is something like:

int i = 0;
while (input > 0) {
    numbers[i++] = (input % 10);
    input /= 10;
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1

You have created the array but you didn't assigned values to the array.

In your enhanced for loop you are assigning the value to the value variable which will store nothing in the array, you need to do it manually to store the value to array.

Try the below code:

int index = 0;
for (Integer value : numbers) {
    while (input > 1) {
        value = input % 10;
        input /= 10;

        // assigning value back to numbers array
        numbers[index] = value;
        index++;
    }
}
SpaceCore186
  • 586
  • 1
  • 8
  • 22
Srinu Chinka
  • 1,471
  • 13
  • 19
  • Thanks. That'd solve my problem by assigning the refference variable `value`'s value to each `numbers`'s field. It is even shorter if I just use a normal `for` loop and assign each `numbers`'s field directly, though. – SpaceCore186 May 11 '16 at 06:45
  • Normal for and assigning it numbers array directly will definitely shorter the code, but here in my answer I used your code, so that you can easily understand what you are missing.. :) – Srinu Chinka May 11 '16 at 06:59
  • Exactly. I just pointed it out in case that a person with the same problem were seeing this; your answer is great. – SpaceCore186 May 11 '16 at 07:01