开发者

Java Array Indexing

    class anEvent{ 
  String number;
  String dueTime;
 }



public static void main(String args[]) {
      int x = args.length / 2;
      int y = args.length;
      anEvent [] order = new anEvent [x];
      for(int i=0; i<x; i++){
       if(i==0){
        order[i].number = args[0]; //Line(#)
        order[i].dueTime = args[1];
       } else if ( i % 2 == 0){
       order[i].number = args[i];
       order[i].dueTime = args[i];
       } else if ( i % 2 != 0){
        order[i].number = args[i+1];
        orde开发者_如何转开发r[i].dueTime = args[i+1];
       } else if ( i == x -1){
        order[i].number = args[x-1];
        order[i].dueTime = args[x-1];
       }

      }

Java complains that a Null Pointer exceptuion is present at line # in the above snippet.

What's the matter?

ps: I know that the snippet can be cleaned up but there should be no problem at all on line #


When an array is created, all the array elements are null. In your case, you need to fill the array with new anEvent() instances.


Make the first line of your for-loop:

order[i] = new anEvent();

As is, you are not initializing anything in the array (they're all null), so when you try to access the fields you get that exception.


Since you mention that it "can be cleaned up", I took the liberty of so doing:

public class Thing {
    private String number;
    private String dueTime;

    public Thing(String number, String dueTime) {
        this.number = number;
        this.dueTime = dueTime;
    }

    public static void main(String args[]) {
        int x = args.length / 2;
        Thing[] order = new Thing[x];
        for (int i = 0; i < x; i++) {
            if (i == 0) {
                order[i] = new Thing(args[0], args[1]);
            } else if (i % 2 == 0) {
                order[i] = new Thing(args[i], args[i]);
            } else if (i % 2 != 0) {
                order[i] = new Thing(args[i + 1], args[i + 1]);
            }
        }
    }
}

"anEvent" doesn't conform to the capitalized camel-case Java standard, so I renamed it. "Thing" isn't particularly meaningful, but there isn't much to work with here. The final else if clause can never be reached, because i % 2 either is or is not equal to zero, so I dropped it. And, of course, I'm creating new Things, which avoids the problem of the nulls. Enjoy.


NullPointerException means that you attempted to add a value or execute a method to something that turn out to be a null.

In Java object references can have assigned ... well, objects and null

When they have null assigned this exception is thrown:

Object o = null;
o.toString(); // <- NullPointerException ( think of null.toString() )

Arrays, are objects also. When you create an array with a size, all the "boxes" inside the array contain null as reference.

So:

Object[] array = new Object[10];

Creates something similar to the following:

 [null,null,null,null,null,null,null,null,null,null]

That's why, when you execute:

array[0].toString(); // or  order[i].number in your specific example... 

You get that exception, because the effect is exactly the same as:

null.toString();  // or null.number  <-- NullPointerException.

To solve this problem, you have to assign a valid object reference to that position into the array:

for(int i=0; i<x; i++){
    order[i] = new anEvent();
    ...
    ...

I hope this helps.

Final note. In Java classes names start with upper case, so your class should've really be:

class AnEvent {
....

And finally, most of the java source code is indented using 4 spaces.


You hasn't created any anEvent instance, defining an array (order[]) you are not creating default values for it.

and also there are more simple array for your case:

List events = new ArrayList(x);
for(int i=0; i<y; i+=2){
  anEvent event = new anEvent();
  anEvent.number = args[i];
  anEvent.dueTime = args[i+1];
  events.add(event);
}
anEvent[] order = events.toArray();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜