1

i have an array of strings which i want to convert to int, pretty simple and straightforward here is the code :

public static void main(String[] args) {
    String myarray[]=readfile("[pathtothefile]");

     int mynums[] = new int[myarray.length];
    for (int i=0;i<myarray.length;i++){
        mynums[i]=Integer.parseInt(myarray[i]);
    }
    System.out.print(Arrays.toString(mynums));  
}

But the Problem here is, if i initialize "mynums" like this: mynums[]=null; i get NullPointerException on the following line:

"mynums[i]=Integer.parseInt(myarray[i]);" 

what i have to do to solve it is

int mynums[] = new int[myarray.length]; 

here someone explained why it happens but i dont know how to initialize now! i mean sometimes i dont know how big my array can get and i just want to initialize it. is it even possible?

Community
  • 1
  • 1
Iamyer
  • 61
  • 1
  • 9
  • 3
    Look at the inverse: Why would it make sense to define the indexes of a null array? – FThompson Jun 05 '13 at 19:37
  • 1
    How do you suppose to access a particular item in the array without the array itself being initialized? – Bernard Jun 05 '13 at 19:38
  • 3
    Another way to think of it is as if you are filling a cup. If your cup (array) is non-existent, how could you possibly add water (ints) to it? First, you must have a definitive cup in order to add water to it. – FThompson Jun 05 '13 at 19:39
  • when you say int mynums[] = null, then you declare a variable, that can refer to an array of integer values, but at that point you say it refers to nothing So if it refers to nothing, then the expression mynums[i] have no sense. – Quirin Jun 05 '13 at 19:41
  • 2
    *"i mean sometimes i dont know how big my array can get and i just want to initialize it. is it even possible"* [`java.util.ArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) – NullUserException Jun 05 '13 at 19:47
  • "sometimes i dont know how big my array can get" This is why [`java.util.List`](http://docs.oracle.com/javase/7/docs/api/java/util/List.html) and its various implementations exist (such as [`java.util.ArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html)). – ajp15243 Jun 05 '13 at 19:48

5 Answers5

1

In Java everything is a pointer behind the scenes. So when you do mynums[]=null, you are pointing to a null. So what is null[i]? That is where your NPE comes from. Alternatively when you point it to an array, then you are actually accessing the i'th element of the array.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • Thanks , So how can i fix this when i dont exactly know how big my initial array can get? – Iamyer Jun 05 '13 at 19:43
  • @user1972584 You can use [Collection in java](http://docs.oracle.com/javase/tutorial/collections/). look for List – Smit Jun 05 '13 at 19:43
  • Either that or you have to read your file twice. Once to get the size and once to fill in the array. Of course a List is probably a much better choice (or ArrayList or Vector). You can also do List.ToArray after you are done reading in. – David says Reinstate Monica Jun 05 '13 at 19:45
  • @Smit I saw somewhere that ADT suggested me to use SparseIntArray for integers. – Miro Markaravanes Jun 05 '13 at 19:45
  • @MiroMarkarian Yeah, but thats for ADT. This guy just wants something basic. SparseIntArray is a big optimization tool and isnt part of the regular Java libraries, only in android libraries. – David says Reinstate Monica Jun 05 '13 at 19:47
1

You have to first initialize the array because it allocates memory depending on the array size. When you want to add for example an integer to an array it writes the int into previously allocated memory.

The memory size won't grow bigger as you add more items.( Unless you use Lists or Hashmaps, ... but it's not true for generic arrays)

If you don't know how big your array will be, consider using SparseIntArray. which is like Lists and will grow bigger as you add items.

Miro Markaravanes
  • 3,285
  • 25
  • 32
0

Briefly, in java an array is an object, thus you need to treat it like an object and initialize it prior to doing anything with it.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35
0

What if you don't use an array but an ArrayList? It grows dynamically as you add elements.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0

Here's an idea. When you're initializing something as null, you're simply declaring that it exists. For example ... if I told you that there is a dog, but I told you nothing about it ... I didn't tell you where it was, how tall it was, how old, male/female, etc ... I told you none of its properties or how to access it, and all I told you was that there IS a dog (whose name is Array, for sake of argument), then that would be all you know. There's a dog whose name is Array and that is it.

Typically, arrays are used when the size is already known and generally the data is meant to be immutable. For data that are meant to be changed, you should use things like ArrayList. These are intended to be changed at will; you can add/remove elements at a whim. For more information about ArrayList, read up on the links posted above.

Now, as for your code:

public static void main(String[] args) {

    ArrayList<int> myInts = new ArrayList<int>();
      // define a new null arraylist of integers.

    // I'm going to assume that readfile() is a way for you get the file
    // into myarray. I'm not quite sure why you would need the [], but I'll
    // leave it.

    String myarray[] = readfile("[pathtothefile]");

    for (int i = 0; i < myarray.length; i++) {
             //adds the value you've specifed as an integer to the arraylist.
        myInts.add(Integer.parseInt(myarray[i])); 
 }

    for (int i = 0; i < myInts.size(); i++) {
            //print the integers
        System.out.print(Integer.toString(myInts.get(i)));
    }

}
Vasu
  • 1,090
  • 3
  • 18
  • 35