wood burning stoves*
The moose likes Java in General and the fly likes Can Java class object be  null ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Java class object be  null ?" Watch "Can Java class object be  null ?" New topic
Author

Can Java class object be null ?

Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

Can we initialize an object of a class as null ? I'm trying to develop Binary tree in java. Actually I have two classes in a package.
One is Sort and another is List.


class List
{ List left,right,down;
int data;
int f=1;

public List()
{
f=1;
left=null;
right=null;
down=null;
data=0;
}

}


I have created array of List in Sort class

class Sort
{
List array[];
// has more code here
}


Here when i create array[0]; it shows NullPointerException . i know the reason here array[0] contains another List objects left,right and down which is initialized as null but if i initialize those objects then those left,right and down will have another left,right and down objects it will keep going on... so want these object either to be null or not initialized at beginning like we do in data structure .... is it possible ? need guidance
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Instance variables that are references to objects are implicitly initialized to null; you don't have to do it explicitly because Java already does it for you.

Classes should have nouns or noun phrases for names. 'Sort' is a verb and is more appropriate as a method name, not a class name. Use 'Sorter' instead if you must have this class. 'List' should be avoided because there is already a commonly used 'List' interface in the standard Java class library.


Junilu - [How to Ask Questions] [How to Answer Questions]
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Junilu Lacar wrote:
'List' should be avoided because there is already a commonly used 'List' interface in the standard Java class library.


And a less frequently used java.awt.List class too.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
Ganesh Pat wrote: . . .
I have created array of List in Sort class

class Sort
{
List array[];
// has more code here
}
. . .
No, you haven’t. All you have done there is allocate space and a name for it, which is called declaring it. You have not put any array objects into the space, so it automatically defaults to null.
By the way: the correct style for declaring arrays is
List[] array; with the [] as part of the type.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

Note that when it comes to declaring arrays, the compiler doesn't care too much about where the brackets are. Each of the declarations below is syntactically correct and equivalent but the last one is the most commonly used style:

Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

Thank you all of you for help and confess for late reply. ya actually i want to implement Nary tree in java and i didn't know about java collection frameworks a pal of mine told me about this yesterday so can you please tell me which is best in java collection framework for nary tree ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Java class object be null ?
 
Similar Threads
Sorting Asciibetically
Sun Cirtification
Two dimensional array
Array
why a local array which is an array initializes to 0 ?