Problem with passing array of objects to method
Hi all,
I encounter a problem when I try to pass an array of objects (from class "jobs") to a method.
The program throws me:
"Exception in thread "main" java.lang.NullPointerException" in line #41 ("if...") inside the mergeSort.
I've tried to take out the arrays I pass to the main class, tried also with getters/setters, without success.
Any ideas?
Thanks in advance.
Code:
package tiful;
import java.io.*;
public class Tiful {
static class jobs{
public float value;
public int index;
public jobs(float val,int ind)
{
value=val;
index=ind;
}
/*
float getValue()
{
return value;
}
int getIndex()
{
return index;
} */
}
public static void mergeSort_srt(jobs array[],int lo, int n){
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort_srt(array, low, middle);
mergeSort_srt(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low].value < array[start_high].value) {
low++;
} else {
jobs Temp = array[start_high];
for (int k = start_high- 1; k >= low; k--) {
array[k+1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
public static void main(String[] args)
{
int len=5;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Arr1[]=new String [len];
String Arr2[]=new String [len];
String Arr3[]=new String [len];
String Arr4[]=new String [len];
String Arr5[]=new String [len];
int Order[]=new int[len];
int t[]=new int[len];
int w1[]=new int[len];
int w2[]=new int[len];
int r[]=new int[len];
int type[]=new int[len];
jobs Atzuv[]=new jobs[len];
jobs Meushar[]=new jobs[len];
try{
Arr1 = (br.readLine()).split(",");
Arr2 = (br.readLine()).split(",");
Arr3 = (br.readLine()).split(",");
Arr4 = (br.readLine()).split(",");
Arr5 = (br.readLine()).split(",");
}
catch (Exception e){
System.out.println("err");
}
for (int i = 0;i < len;i++){
t[i]=Integer.parseInt(Arr1[i]);
w1[i]=Integer.parseInt(Arr2[i]);
w2[i]=Integer.parseInt(Arr3[i]);
r[i]=Integer.parseInt(Arr4[i]);
type[i]=Integer.parseInt(Arr5[i]);
}
for (int j = 0;j < len;j++){
if (type[j] ==0) // Atzuv
{
Atzuv[j]=new jobs(((float)t[j]/w1[j]) + -((float)t[j]/w2[j]),j);
}
if (type[j] ==1) // Meushar
{
Meushar[j]=new jobs( ((float)t[j]/(w1[j]+w2[j])),j);
}
}
mergeSort_srt(Atzuv,0 , len-1);
mergeSort_srt(Meushar,0 , len-1);
for(int i = 0;i < len;i++){
System.out.print(Meushar[i] + ", ");
}
System.out.println();
for(int i = 0;i < len;i++){
System.out.print(Atzuv[i] + ", ");
}
System.out.println();
}
}
Re: Problem with passing array of objects to method
The lack of formatting of the code from lines 29 to 55 makes that part of the code hard to read.
Does the array variable named array contain null elements?
Add a println statement just before the statement where the exception occurs to print out the index and the element's contents so you see what is null.
Please post the full text of the error message showing the call stack.
Re: Problem with passing array of objects to method
Quote:
Originally Posted by
Norm
The lack of formatting of the code from lines 29 to 55 makes that part of the code hard to read.
Does the array variable named array contain null elements?
Add a println statement just before the statement where the exception occurs to print out the index and the element's contents so you see what is null.
Please post the full text of the error message showing the call stack.
Thanks.
I added a println command and I got from it the same null exception.
I suppose it is something to do with the objects passed to the method by value.
Not sure what you wanted me to attach.
Re: Problem with passing array of objects to method
Quote:
what you wanted me to attach.
The full text of the error message.
Did you add lots of printlns to show the values of all the variables that are used by the code?
To print the contents of an array, use the Arrays toString() method to format the array for printing:
System.out.println("arrName=" + Arrays.toString(arrName));
Re: Problem with passing array of objects to method
Quote:
Originally Posted by
Norm
The full text of the error message.
Did you add lots of printlns to show the values of all the variables that are used by the code?
To print the contents of an array, use the Arrays toString() method to format the array for printing:
System.out.println("arrName=" + Arrays.toString(arrName));
Solved. thanks.