Results 1 to 5 of 5
  1. #1
    Onca is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Question 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  3. #3
    Onca is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with passing array of objects to method

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Problem with passing array of objects to method

    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));
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Onca is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with passing array of objects to method

    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. Creating an Array of Objects Method
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-18-2011, 01:33 PM
  2. Replies: 1
    Last Post: 02-03-2011, 05:43 PM
  3. Passing an array to a method.
    By twcast in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 09:13 AM
  4. Passing array objects
    By drymsza1234 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 02:40 PM
  5. Replies: 5
    Last Post: 10-04-2008, 11:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •