views:

179

answers:

6

I was reading through some java code in a project I was working on and came along a method with something to this effect in it.

Object[] objs = null;
objs[0] = str1;
objs[1] = obj1;
for(String str: strArray)
{
    objs[objs.length+1] = str;
}

I can't figure out what is going on here. I was hoping someone could explain how this code is working to me, preferably on both a high level and the behind the scenes low level.

+7  A: 

That should not run. objs[0] will give an error.

Are you sure you are running the correct version of the file?

CookieOfFortune
No I just checked and it looks like I'm not running the correct version. Thanks for your help I was afraid I'd missed some core functionality of Java when learning it.
faceless1_14
What IDE are you using? If you're using Notepad... try Eclipse or Netbeans, they take care of a lot of these file issues for you.
CookieOfFortune
I'm currently using Eclipse but being new to it and the CVS set up I probably just pulled down the wrong file.
faceless1_14
+1  A: 

Your first problem is that objs is null rather than allocated as an array. The first line should be:

Object[] objs = new Object[2];  // array 2 elements long

Next, the "array length" is the total allocated size of the array, not the number of elements you inserted. So in this example, objs.length is always 2.

Therefore your loop is wrong too.

The correct code would be this:

Object[] objs = new Object[ strArray.length ];  // array of the right size.
for( int k = 0 ; k < strArray.length ; k++ )
{
    objs[k] = strArray[k];
}
Jason Cohen
He's asking why it's working when it looks like it clearly shouldn't be.
Davy8
+5  A: 

This has no hope of ever working in Java. I suspect that "something to this effect" is subtly different from the real code, if it seemed to be working. A few problems:

  • It should obviously be throwing a NullPointerException on the second line when you try to dereference the variable
  • objs[objs.length+1] doesn't extend an array, which appears to be the intention - arrays are a fixed size after allocation.
  • Even if arrays could be extended, they're 0-based, so you'd actually want to extend them by setting objs[objs.length] - if this code worked, you'd end up with every other element being "missing"
Jon Skeet
A little faster and a lot more detailed than my answer. +1 for this and deleting mine.
Michael Myers
A: 
  • The code is syntactically correct
  • It does not compile standalone, but with some additions, e.g.

    public class Test {
        public static void main(String[] args) {
            String[] strArray = new String[]{"a","b","c","d","e","f"};
            Object obj1 = new Object();
            String str1 = "test";
            Object[] objs = null;
            objs[0] = str1;
            objs[1] = obj1;
            for(String str: strArray)
            {
                objs[objs.length+1] = str;
            }
        }
    }
    

    it will, since it's correct Java then

  • It will not run without errors (NullPointerException will occur on objs[0] = str1)
Johannes Weiß
A: 

The following code, which is your code with a couple fixups to make it comile throws a NullPointerException, as expected.

public class Crap
{
  public static void main (String[] args)
  {
    Object[] objs = null;
    objs[0] = "str1";
    objs[1] = new Object ();
    for(String str: new String [] { "str1", "str2" })
    {
        objs[objs.length+1] = str;
    }
  }
}


Exception in thread "main" java.lang.NullPointerException
    at Crap.main(Crap.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
dbrown0708
A: 

@Jason Cohen

What about the str1 and obj1 members that should also be stored in the objs array?

Martin MacPherson