Question

i can't seem to figure out what is going on with this code (i didn't write it). it looks like str3 = localBufferedReader1.readLine() is null and hence an exception is thrown. The output i see after running the application is as follows:

  >> Transmitting File : C:/some_folder/another_folder/folder/bin/msg/somefile.soa (32 bytes)
     >> Segment read from file (segLength=15 bytes, 16 bytes remaining to send)
     >> Segment read from file (segLength=13 bytes, 2 bytes remaining to send)
  >> Message sent ...
Exception in Transmitting Data : null

I can't seem to understand why there is nothing to read when the connection did not fail and the file was found..? what other reasons could this fail for? i've ensured that the server is really listening on the port number i enter. i'm also running on localhost - so this should work :S i believe that i should be getting a message back (looking at the code) from the socket letting me know if everything is OK or NOT-OK ... any ideas? any help would be appreciated!

public class Client
{

  public static void main(String[] paramArrayOfString)
    throws IOException
  {
    Socket localSocket = null;
    PrintWriter localPrintWriter = null;
    BufferedReader localBufferedReader1 = null;




    String str1 = JOptionPane.showInputDialog(null, "Host to Talk to ?", "Specify the HOST", 1);



    String str2 = JOptionPane.showInputDialog(null, "What Port is it Listening on ?", "Specify the PORT", 1);
    int i;
    try
    {
      i = Integer.parseInt(str2);
    }
    catch (Exception localException1)
    {
      i = 3128;
    }
    char[] arrayOfChar1 = { '\013' };
    char[] arrayOfChar2 = { '\034' };
    char[] arrayOfChar3 = { '\r' };

    int k = 1;
    int m = 0;
    int n = 0;


    String str3 = "";
    String str5 = JOptionPane.showInputDialog(null, "Filename Containing HL7 Message", "Test Filename", 1);
    while (str5.length() > 0)
    {
      try
      {
        localSocket = new Socket(str1, i);
        localPrintWriter = new PrintWriter(localSocket.getOutputStream(), true);
        System.out.println(localSocket.getInputStream().available()); 

        localBufferedReader1 = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
      }
      catch (UnknownHostException localUnknownHostException)
      {
        System.out.println("Don't know about host: " + str1);
        System.exit(1);
      }
      catch (IOException localIOException)
      {
        System.out.println("Couldn't get I/O for the Connection to " + str1 + " on Port (" + str2 + ")");
        System.exit(1);
      }
      try
      {
        File localFile = new File(str5);
        BufferedReader localBufferedReader2 = new BufferedReader(new FileReader(localFile));

        long l = localFile.length();
        System.out.println("  >> Transmitting File : " + str5 + " (" + l + " bytes)");

        String str7 = new String(arrayOfChar1);
        String str6;
        while ((str6 = localBufferedReader2.readLine()) != null)
        {
          l = l - str6.length() - 1L;
          System.out.println("     >> Segment read from file (segLength=" + str6.length() + " bytes, " + l + " bytes remaining to send)");
          if (k != 0) {
            k = 0;
          }
          str7 = str7 + str6 + new String(arrayOfChar3);
          if (l < 6L)
          {
            if ((str7.indexOf("READY") >= 0) || (str7.indexOf("BYE") >= 0)) {
              str7 = str7.substring(0, str7.length() - 1);
            } else {
              str7 = str7 + new String(arrayOfChar2);
            }
            m = 1;
          }
          if (m != 0) {
            break;
          }
        }
        if (m == 0)
        {
          str7 = str7 + new String(arrayOfChar2);
          m = 1;
        }
        localPrintWriter.println(str7);
        localPrintWriter.flush();
        localBufferedReader2.close();
        System.out.println("  >> Message sent ...");


        str3 = localBufferedReader1.readLine();
        if (arrayOfChar3[0] == '\r')
        {
          n = 1;
          if (str3.indexOf("SOA|OK|") >= 0) {
            if (str3.indexOf("SOA|OK|||") >= 0)
            {
              if (str3.indexOf("SOA|OK||||") >= 0) {
                n = 0;
              }
            }
            else {
              n = 0;
            }
          }
          if (str3.indexOf("SOA|NOT-OK|") >= 0) {
            n = 0;
          }
          str3 = str3.substring(1) + "\n";
          while (n != 0)
          {
            String str4 = localBufferedReader1.readLine();
            int j = str4.length() - 1;
            if (str4.charAt(j) == arrayOfChar2[0]) {
              n = 0;
            } else {
              str3 = str3 + str4 + "\n";
            }
          }
        }
        System.out.println("  >> Server Responds : " + str3);
      }
      catch (Exception localException2)
      {
        System.out.println("Exception in Transmitting Data : " + localException2.getMessage());
        localPrintWriter.close();
        localBufferedReader1.close();
        localSocket.close();
        System.exit(1);
      }
      localPrintWriter.close();
      localBufferedReader1.close();
      localSocket.close();
      if (str3.indexOf("AE") >= 0) {
        break;
      }
      str5 = JOptionPane.showInputDialog(null, "Filename Containing HL7 Message", "Test Filename", 1);
      if (str5 == null) {
        System.exit(0);
      }
      k = 1;
      m = 0;
    }
    System.exit(0);
  }
}
Was it helpful?

Solution

From the javadoc of BufferedReader.readLine:

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

The code where str3 is used:

str3 = localBufferedReader1.readLine();
if (arrayOfChar3[0] == '\r')
{
  n = 1;
  if (str3.indexOf("SOA|OK|") >= 0) {

So your code reaches end of stream, and null is assigned to str3. However the code doesn't seem to check it and tries to invoke indexOf on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top