• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Arrays

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class T052
{
static int a[];

public static void main(String[] args)
{
System.out.println( a[0] );//1
}
}
Can somebody tell me why it throws a null pointer exception at //1. Does the elements of a static array are not intialized automatically.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in town but I think that java arrays are objects so you have to use the new operator to create them. The new operator is needed to create a new array object.
static int a[]; // Only declaration
a[] = new int[10]; //Creation, dimension is neccesary too

P.D. English is not my mother tongue!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the reasons that, in Java, a variable referring to an array is usually defined like this:


Here it is clearer that a is an uninitialized reference to an array of int.
[ August 06, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Enrique Villamizar:
I'm new in town but I think that java arrays are objects so you have to use the new operator to create them. The new operator is needed to create a new array object.
static int a[]; // Only declaration
a[] = new int[10]; //Creation, dimension is neccesary too



No problem with the English, Enrique ,
but it should be:
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it an array is an object. Objects declared but not initialized will return null.

Pls correct me if I'm wrong

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal,
well, and have totally different meanings.
means that the array a is an array of integers.
means that a is an array of integers where each element in a is itself an array.

That's why the NullPointerException as a does not have its elements as arrays.

Hope that clears your doubts ?
[ August 08, 2005: Message edited by: Sherry Jacob ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sherry Jacob:
Hi Vishal,
well, and have totally different meanings.
means that the array a is an array of integers.
means that a is an array of integers where each element in a is itself an array.

That's why the NullPointerException as a does not have its elements as arrays.

Hope that clears your doubts ?

[ August 08, 2005: Message edited by: Sherry Jacob ]





Pardon me, but in Java:

and:


mean the same thing. That is, a is a reference to an array of ints.
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a[],b; // here a is an reference to an array of integers
// b is an int variable b=10;

int[] a,b; // here both a and b are reference variable.
 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal,
There are three things related to an Array....
1) Declaration of an Array
2) Construction of an Array
3) last one is initializing...

All these things happen in sequence, on after another.
Now your question was that you are trying to access element of a int array on some index 0(you can take any one). Your code is given below

public class T052
{
static int a[];

public static void main(String[] args)
{
System.out.println( a[0] );//1
}
}

It means you are trying to access the element of an array which is declared but not constructed.
Above two steps should be done, otherwise you can access the elements.
So if you will construct it like
static int a[] = new int[size];
then third step will be done implicitly (as this array is a member variable).
output will be null if you will not initialize the array explicitly.
 
I like tacos! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!