tags:

views:

402

answers:

5

I'm a little lost (still working with Ron Jeffries's book). Here's a simple class:

public class Model{
    private String[] lines;

    public void myMethod(){
        String[] newLines = new String[lines.length + 2];
        for (i = 0, i <= lines.length, i++) {
            newLines[i] = lines[i];
        }
    }
}

I have another class that initializes Model, and an empty array, by setting myModel = new String[0]. When I invoke myModel.myMethod(), I get a subscript out of range error. Looking at the debugger, what I see is that myModel.lines has zero dimensions and zero length. Shouldn't it have a dimension and length of 1? Granted the value of lines[0] is null, but the array itself shouldn't be, should it?

Any thoughts truly appreciated.

Randy

+1  A: 

lines will be null, so lines.length will throw an exception.

I believe your other class initializing "Model" won't help since Lines itself is private. In fact, whatever you are doing to Model is probably illegal in at least 30 states.

Bill K
+1  A: 

lines is initalized to null, check for null or initialize it in this way :

private String[] lines = new String[0];
FerranB
+1  A: 

You cannot initialize an instance of Model by setting it equal to a String array. I'm actually surprised that the compiler will let you even try.

If you really want Model to be initializable with an external array, you should make a Constructor for the Model class that will take as an argument the array. Then in the body of your constructor, set the value of lines to that value.

Example:

public class Model {
    private String []lines;

    public Model(String [] inLines)
       {
       lines = inLines;
       }
}

Usage:

myStringArray = new String[0];
myModel = new Model(myStringArray);
McWafflestix
+1  A: 

I think your example is probably not the same as your actual code based on your description. I think the problem is that arrays are zero-based and thus an array initialized as:

string[] lines = new string[0];

has no elements.

You need to change your loop so that you check that the index is strictly less than the length of the array. As others have indicated you also need to make sure that the array itself is not null before trying to reference it.

My take on your code:

public class Model{
    private String[] lines = new string[0];

    public Model( string[] lines ) {
        this.lines = lines;
    }

    public void myMethod(){
        int len = 2;
        if (lines != null) {
            len = len + lines.length;
        }
        String[] newLines = new String[len];
        for (i = 0, i < lines.length, i++) {
            newLines[i] = lines[i];
        }
    }
}
tvanfosson
You're right, my sample completely blows. Ron Jeffries's structure is a notepad class and a text manipulation class that he calls "Model" in the early part of the book. Notepad has the textbox, instantiates Model with an array, and slams newlines into model.
EoRaptor013
A: 

Take a look at my answer here - I think this will get you the background you are looking for on the differences between array initialization in Java and C/C++.

Kevin Day