0

In my code I'm trying to accept the arguments and put it in the the array of objects called cl. It gives me NullPointerException.

//In my main class
public static void main(String args[]) {
    int n = Integer.parseInt(args[0]);
    cl[] Array1;  
    Array1 = new cl[n];
    cl[0].a1 = Integer.parseInt(args[1]);
    cl[0].a2 = Integer.parseInt(args[2]);
}

and

//another class declaration
public class cl {
    int a1;
    int a2;
}

I'm getting a NullPointerException, I'm able to print the command line arguments. I'm not sure what the problem is.

4
  • 1
    what is arr[] Array1;? is your code not give compile time error? Commented Apr 17, 2014 at 3:09
  • cl[0].a1 should be Array1[0].a1. Similarly for the other field access. Commented Apr 17, 2014 at 3:15
  • Please, please start using proper naming conventions. Classes start uppercase, variables start lowercase. Commented Apr 17, 2014 at 3:41
  • possible duplicate of java - Why is this for-loop giving a nullpointer exception? among numerous others. Commented Apr 17, 2014 at 3:43

3 Answers 3

2
 cl[]  Array1 = new cl[n];

but your object is null because you haven't initialized your object of cl[]

for example.

Array1[0]= new cl();

and than access the variables.

Array1[0].a1 and Array1[0].a2

As you are doing this

cl[0].a1 = Integer.parseInt(args[1]);

cl  is not your array variable your variable is `Array1`
Sign up to request clarification or add additional context in comments.

Comments

2

Initializing an array does not initialize its elements.

This

 Array1 = new cl[n];

creates an array of type cl with n elements, but those are all initialized to null since cl is a reference type.

You need to first initialize the element before accessing its field

Array1[0] = new cl();
Array1[0].a1 = ...

Try to follow Java naming conventions. Always start variable names with a lower case character. Always start class names with an upper case character.

3 Comments

I think you mean Array1[0] = new c1(); and Array1[0].a1 = ...
@Jason Yeah, copy paste error from OP's snippet. Thanks.
I had the same problem
1

In the following lines:

c1[] Array1;  
Array1 = new cl[n];

you are creating an array in which to store references to c1.

Then you start dereferencing c1[0] when you really mean Array1[0], but you never created an instance of c1 and put it in Array1[0].

What you need to do is:

public static void main(String args[]) 
{
    int n = Integer.parseInt(args[0]);

    cl[] Array1;  

    Array1 = new cl[n];

    // don't forget to create an instance of c1 and put in array at index 0!
    Array1[0] = new c1();

    Array1[0].a1 = Integer.parseInt(args[1]);
    Array1[0].a2 = Integer.parseInt(args[2]);
}

1 Comment

Jason I think it must be Array1[0] = new c1();

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.