Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

array of object turns to null?

807605Aug 11 2007 — edited Aug 12 2007
I am not "advanced" in java, but not a noob either. I have this code which "should" run fine. no runtime errors, no compile errors. but an implemented object becomes null? i've done this many times, so i cross checked this about 1000 times with no conclusions.
package project4;

//ContestManagement.java
//Project 4
//Scott Gauthier
//CMIS 241

import java.io.File;
import java.io.IOException;
import java.util.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;



public class ContestManagement{
	
	private static final int MAX = 20;
	public Competitor[] comps = new Competitor[MAX];
	public Competitor[] marks = new Competitor[MAX];
	public Competitor[] contestant = new Competitor[MAX];
	public String[] info;
	public int id = 0;
	public String name = null;
	public double score = 0.0;
	
	
	
//==blank constructor for accessing non static methods
	public ContestManagement(){
		
	}
//===========================================================
//==method for file input
	public void fileSplitter(String a){
		
		try {
			String fileContents = FileUtils.readFileToString(new File(a), null);
			
			info = StringUtils.split(fileContents, "\n");
			
			for (int i = 0; i < info.length; i++){
				if (info[i] == null)
					break;
				System.out.println("line45 " + info);
}
} catch (IOException e) {

System.out.println("Something went wrong");
e.printStackTrace();
}
}
//===========================================================
//==handles the comp array
public void comp(){

for (int i = 0; i < info.length; i++){
String compInfo = info[i];
System.out.println("line 59 " + compInfo);

if(StringUtils.contains(compInfo, null))
break;

else if (StringUtils.isNotEmpty(compInfo)){
String[] temp = StringUtils.split(compInfo, " ");
id = Integer.parseInt(temp[0]);
name = temp[1];
score = 0.0;

System.out.println("line 70 " + id + " " + name + " " + score);
System.out.println("");

Competitor tempC = new Competitor(id, name, score); //**problem starts here**
System.out.println("line 74 " + tempC + " \n");

comps[i] = tempC;
System.out.println("line 77 " + comps[i]);
}

}

for (int i = 0; i < comps.length; i++){
if (comps[i]== null)
break;

System.out.println("line 86 " + comps[i]);
}
System.out.println("\n");

}
//============================================================
//==handles the marks array
public void mark(){

for (int i = 0; i < info.length; i++){
String compInfo = info[i];

if(StringUtils.contains(compInfo, null))
break;

else if (StringUtils.isNotEmpty(compInfo)){
String[] temp = StringUtils.split(compInfo, " ");
id = Integer.parseInt(temp[0]);
name = null;
score = Double.parseDouble(temp[1]);
}

Competitor x = new Competitor(id, name, score);
marks[i] = x;
}

for (int i = 0; i < marks.length; i++){
if (marks[i]== null)
break;
System.out.println(i + " " + marks[i]);
}
System.out.println("\n");

}
//===========================================================
//==sort the arrays so when they are combined, they will match up (O(n^2))
public static void bubbleSort(Competitor[] a) {
int n = a.length;
int lastSwap = n - 1;
while (lastSwap > 0) {
// Sweep through items 0 .. lastSwap-1, swapping
// any out of order items.
int jSwap = -1;
for (int j = 0; j < lastSwap; j++) {
Competitor bubble = a[j];
if (bubble.compareTo(a[j + 1]) > 0) {
a[j] = a[j + 1];
a[j + 1] = bubble;
jSwap = j;
}
}
lastSwap = jSwap;
}
}
// ============================================================
// ==combine the two sorted arrays
public Competitor[] merge(Competitor[] a, Competitor[] b){

for (int i = 0; i < a.length; i++){
for(int j = 0; j < b.length; j++){
Competitor compInfo = new Competitor(a[i].getCompetitorID(), a[i].getCompetitorName(), b[j].getCompetitorMark());
contestant[j] = compInfo;
}
}
return contestant;
}
//===========================================================
//==method for printing files
public static void printContest(Competitor[] a){
for (int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
}


//main
//F:\Workspace\CMIS241\project4\competitors.txt
//F:\Workspace\CMIS241\project4\marks.txt
public static void main (String[] args) {

ContestManagement contest = new ContestManagement();

System.out.println("Contest Management v1.0 \n");

Scanner conIn = new Scanner(System.in);
System.out.print("Enter the location of the Competitors text file: ");
String compFileName = conIn.nextLine();

/*System.out.print("\nEnter the location of the Marks text file: ");
String markFileName = conIn.nextLine();*/


System.out.println("The competitor file, not sorted-");
contest.fileSplitter(compFileName);
contest.comp();

/*System.out.println("The marks file, not sorted-");
contest.fileSplitter(markFileName);
contest.mark();

System.out.println("The competitor file sorted-");
bubbleSort(comps);
printContest(comps);

System.out.println("The marks file sorted-");
bubbleSort(marks);
printContest(marks);

System.out.println("The files combined & sorted-");
ContestManagement combine = new ContestManagement();
combine.merge(comps, marks);
printContest(contestant);*/


}
}


in the comp() method, i put system outs to track the progress. it seems that where i implement the new object, it turns all the input to 0's and null. here the out put of this code:
Contest Management v1.0

Enter the location of the Competitors text file: F:\Workspace\CMIS241\project4\competitors.txt
The competitor file, not sorted-
line45 6241 Cathy

line45 3792 Patrick

line45 2910 John

line45 8371 Mary

line45 9372 Tony

line45 3532 Jane
line 59 6241 Cathy

line 70 6241 Cathy
0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 59 3792 Patrick

line 70 3792 Patrick
0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 59 2910 John

line 70 2910 John
0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 59 8371 Mary

line 70 8371 Mary
0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 59 9372 Tony

line 70 9372 Tony
0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 59 3532 Jane
line 70 3532 Jane 0.0

line 74 0 null 0.0

line 77 0 null 0.0
line 86 0
line 86 0
line 86 0
line 86 0
line 86 0
line 86 0
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2007
Added on Aug 11 2007
17 comments
283 views