The Artima Developer Community
Sponsored Link

Java Answers Forum
Why won't my array work?

1 reply on 1 page. Most recent reply: Jan 15, 2003 2:38 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Chris Skardon

Posts: 2
Nickname: wintermute
Registered: Jan, 2003

Why won't my array work? Posted: Jan 15, 2003 6:36 AM
Reply to this message Reply
Advertisement
I've written the code (below) and the class courseCombinations works fine when created as a single entity, however as soon as it is made into an array I get an error:
Exception in thread "main" java.lang.NullPointerException
at genome.test(genome.java:9)
at GATTer.main(GATTer.java:9)

What am I doing wrong??

Cheers

Chris

(code follows)
===========================================================

GATTer class:
public class GATTer{
	public void GATTer(){}
	
	public static void main(String args[]){
		System.out.println("BEGIN");
		genome combi = new genome();
		combi.test2();
		System.out.println("---------------------END OF TEST2");
		combi.test();
		System.out.println("END");
		}
}

==========================================================
genome class:
public class genome{
	
	public courseCombinations search2 = new courseCombinations();
	public courseCombinations search[] = new courseCombinations[10];
		
	public void genome(){};			
	
	public void test(){		
		System.out.println("numberOf [0]: " + search[0].numberOf);
		search[0].numberOf = 123;
		System.out.println("numberOf [0]: " + search[0].numberOf);
		search[0].printOut();
		for(int i = 0; i < 15; i++)
			search[0].combinationsAre[i] = "TEST";
		search[0].printOut();
	}
 
	public void test2(){
		System.out.println("numberOf: " + search2.numberOf);
		search2.numberOf = 123;
		System.out.println("numberOf: " + search2.numberOf);
		search2.printOut();
		for(int i = 0; i < 15; i++)
			search2.combinationsAre[i] = "TEST";
		search2.printOut();
	}	
 
}

===========================================================
courseCombinations class:
public int numberOf;
 
public void courseCombinations(){
	numberOf = 0;
}// End of courseCombinations
 
public void printOut(){
	for(int i = 0; i < 15; i++)
		System.out.println("combinationsAre[" + i + "] = " + combinationsAre[i]);
}// End of printOut
 
} //End of class


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Why won't my array work? Posted: Jan 15, 2003 2:38 PM
Reply to this message Reply
When you do this:

public courseCombinations search[] = new courseCombinations[10];

then an array is created with 10 elements. The name of the array is search and it may contain 10 references of courseCombinations object. However, the above statement just creates the array named "search" and not 10 objects of courseCombinations class. All 10 elements of array have null reference after above statement is executed.

When you are using this statement:
System.out.println("numberOf [0]: " + search[0].numberOf);

then all elements in search array are still null. YOU need a for loop to instantiate and store 10 ( or may be fewer depending on your needs). Since all elements are null serach[0] is null (it is first element) and you are getting null pointer execption. To correct this problem you need to add some code in test() constructor as:
public void test() {
int total = 10 ;
for ( int i = 0; i < total; i++ ) {
  // Populate search array with 10 objects
  this.search[i] = new courseCombinations() ;
}
// other code goes here
}


Now you won't get null pointer exception.

Remember:
1. All array elements are initialized when you create the array.
2. Even local array is initialized.
3. For array of primitive type all elements have default value for that primitive type for example boolean false, numeric 0 etc.
4. All reference type array elements are initialized to null.
5. If you have a reference type array (in your case you do) then you must all created each element of array and assign the real reference.

Flat View: This topic has 1 reply on 1 page
Topic: javaCOMM API with WindowsXP Previous Topic   Next Topic Topic: comparing 2 sorting algorithms

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use