0

I am trying to create a Piped I/O mergesort. I have the sorting working, but I am struggling with merging the pipes.

This is my Merger.java

  1 import java.util.*;
  2 import java.io.*;
  3 class Merger implements Runnable {
  4
  5     ObjectInputStream ins[] = new ObjectInputStream[2];
  6     ObjectOutputStream out = null;
  7     int b[] = null, start=0;
  8     //1 level merger, we do other 4 levels
  9     //Only last level need an array to write into it
  10     public Merger ( PipedInputStream in1, PipedInputStream in2,      PipedOutputStream pout, int b[]) {
  11         try {
  12 ins[0] = new ObjectInputStream(in1);
  13             ins[1] = new ObjectInputStream(in2);
  14             if(pout==null) {
  15                 System.out.println("POUT = NULL\n");
  16                 out = null;
  17             }
  18             else{
  19                out = new ObjectOutputStream(pout);
  20             }
  21             this.b = b;
  22         } catch (Exception e) {}
  23     }
  24
  25     public void run() {
  26         int num[] = { -1, -2 },
  27             errCnt=0, // number of pipes caused error
  28             k = -1;   // k will be 0 or 2 indicating the pipe to read from
  29
  30         try {
  31             num[0] = ins[0].readInt();     // read from first pipe
  32             num[1] = ins[1].readInt();     // read from second pipe
  33         } catch (IOException e ) {
  34             System.out.printf("\n\t\tOne or two pipes are not connected.\n");
  35             return;
  36         }
  37
  38         //compares num[0] & num[1], k written to array
  39         while(errCnt < 2) {
  40             try {
  41                 if( errCnt < 1 )  k = num[0] <= num[1] ? 0 : 1;
  42
  43                 if( b != null) b[start++] = num[k];
  44                 else {
  45                     out.write(num[k]);
  46                     out.flush();
  47                 }
  48                 num[k] = ins[k].readInt();
  49             } catch( IOException e ) {
  50                 errCnt++;
  51                 //find source of exception
  52                 k = ( k == 0 ? 1 : 0);
  53             }
  54         }
  55     }
  56
  57 }

I call this function in two different cases. I have 6 different levels of Merging. 0 - 5. On 1-4 I call

  122         for(int i = level-1; i >= 0; i--){
  123             n = (int) Math.pow(2,i-1);
  124             for(int j = 0; j < n; j++){
  125                 if (i == 1){
  126                     mergers[i-1][j] = new Merger(pin[i][j*2], pin[i][j*2+1], null, b);
  127                     System.out.printf("Merger[%d][%d]   (pin[%d][%d], pin[%d][%d], null, b\n", i-1, j, i  , j*2 , i, j*2+1);
  128                 }
  129                 else {
  130                     mergers[i-1][j] = new Merger(pin[i-1][i*2], pin[i-1][i*2+1], pout[i-1][j], null);
  131                     System.out.printf("Merger[%d][%d]   (pin[%d][%d], pin[%d][%d], pout[%d][%d], null\n", i-1, j, i , j*2 , i, j*2+1, i-1, j);
  132                 }
  133
  134             }
  135         }

I get the error on line 126 and 130. I think it's because on the last level I pass null to the Pipedoutput (pout) and the other levels I pass null to array b.

The print statements are just to debug that I'm passing in the correct values

Kevin
  • 1
  • 1
  • I suggest you use the debugger to help debug your code. A NullPointerException means you a reference has a null value so I suggest printing out your references i.e. `mergers` and `pin` – Peter Lawrey Feb 07 '16 at 17:32
  • Only use Object*Stream when you want to pass objects. These stream are really inefficient for plain types like `int`. Use DataInputStream instead and it will be much more efficient. – Peter Lawrey Feb 07 '16 at 17:33

0 Answers0