File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can Java class object be null ?

 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6564
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47310
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6564
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic