0

I'm having a problem with inner classes. I build an object (let's say a train) with an inner class representing states (let's say the stops of the train).

I'm trying to run this code:

private void CustomObjectBuilder (String [] origin) {

  final int array_dim = origin.length;

  InnerCustomObject[] tmp_bin = new InnerCustomObject[array_dim];

  for (int ii = 0; ii < array_dim; ii++) {

   String debug = extractData(origin[ii]);


   tmp_bin[ii].setData(debug);



  }




 }

It compiles just just fine but at runtime I get a null object exception.

What am I doing wrong?

Here you can finde the original code:

public class CustomObject {



    InnerCustomObject [] stops;

    public class InnerCustomObject {

        String name, station, schedTime, depTime, schedRail, depRail;

        public void setData (String origin) {

            this.station = origin;
        }

    }
}

Edit: I solved by calling

 CustomObject.InnerCustomObject ico = new CustomObject(). new InnerCustomObject(); 

why it needs to be so verbose?

3 Answers 3

2

Well, the most immediate thing I notice is you don't populate tmp_bin[] with any objects after you declare it. When you first create an array, all it contains are nulls.

So when you do this in your loop:

tmp_bin[ii].setData(debug);

There is nothing to invoke setData() on, resulting in the exception.

Re edit: you can just do

InnerCustomObject ico = this.new InnerCustomObject();

since you're creating them within your outer CustomObject class's CustomObjectBuilder() instance method.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought that, but if I add tmp_bin[ii] = new InnerCustomObject(); it doesn't compile... how do I fix this?
You'd have to show more code. What does your InnerCustomObject class look like? What error does the compiler give?
I get a null pointer exception when I try to access the first element of the array...
But NullPointerExceptions are runtime errors, not compile-time errors. I'm asking about what error the compiler gives when you try to compile with tmp_bin[ii] = new InnerCustomObject(); and it fails.
Eclipse doesn't show anything, just a red X with no info. I solved by doing CustomObject.InnerCustomObject ico = new CustomObject(). new InnerCustomObject(); .... why it need to be so verbose?
1
InnerCustomObject[] tmp_bin = new InnerCustomObject[array_dim];

declares an array of array_dim elements but all are null. Then this

tmp_bin[ii].setData(debug);

won't work.

No problem with inner classes only with an object that is null (=NPE) so you cannot call the method setData().

Comments

1

In your loop you have to create new instance of InnerCustomObject. By new InnerCustomObject[size] you do not create new instances.

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.