views:

103

answers:

5
    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];
        order[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 #

+6  A: 

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

Chris Jester-Young
Sorry, but: "Give a Man a Fish, Feed Him For a Day. Teach a Man to Fish, Feed Him For a Lifetime" :-) I'm joshing with you but I though it was relevant.
Frank V
I wrote anEvent [] order = new anEvent [x];isn't that enough?
frantic
@frantic: no, that is not enough; as Chris wrote, it creates an array containing only nulls.
Michael Borgwardt
@frantic: No, it is not enough. You just allocated an array with x components in it. All the components in the array are null at this point. You should also allocate instances of anEvent class and assign them into the array components
Lauri
@Frank: I agree, and usually for people with homework questions I go to great lengths to avoid giving explicit answers. But you can see from the OP's responses that they don't even know where to begin. Asking them to try to find it using a debugger would just...not get anywhere useful. :-(
Chris Jester-Young
+1  A: 

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.

Sapph
A: 

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();
splix
+1  A: 

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.

Carl Manaster
+1  A: 

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.

OscarRyz