Results 1 to 5 of 5
- 06-15-2012, 02:31 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
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.
Java 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(); } }Last edited by Onca; 06-15-2012 at 02:41 AM.
- 06-15-2012, 03:46 AM #2
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.Last edited by Norm; 06-15-2012 at 03:50 AM.
If you don't understand my response, don't ignore it, ask a question.
- 06-15-2012, 02:44 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
- 06-15-2012, 02:50 PM #4
Re: Problem with passing array of objects to method
The full text of the error message.what you wanted me to attach.
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));If you don't understand my response, don't ignore it, ask a question.
- 06-15-2012, 10:59 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Creating an Array of Objects Method
By DBaskov in forum New To JavaReplies: 14Last Post: 07-18-2011, 01:33 PM -
Problem passing objects as parameters to remote methods via RMI
By nicoeschpiko in forum New To JavaReplies: 1Last Post: 02-03-2011, 05:43 PM -
Passing an array to a method.
By twcast in forum New To JavaReplies: 9Last Post: 02-10-2010, 09:13 AM -
Passing array objects
By drymsza1234 in forum New To JavaReplies: 1Last Post: 12-03-2009, 02:40 PM -
[SOLVED] passing array between main and method,vice-versa
By blueyan in forum New To JavaReplies: 5Last Post: 10-04-2008, 11:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks