all groups > vj# > april 2005 >
You're in the

vj#

group:

BufferedReader.readLine() returns null


BufferedReader.readLine() returns null asilter
4/7/2005 5:01:07 AM
vj#:
i've added a watch to reader.readline() and alinanMesaj name. i am debugging
my codes with stepinto option. after below line;
reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
i see the the value of reader.readLine() watch correctly. below line is the
line right after above line.
alinanMesaj=reader.readLine();
when i execute this with stepinto i see
alinanMesaj watch is "null", and
readerReadLine() watch is
"function 'reader.readLine()' evaluated and returned null"

Re: BufferedReader.readLine() returns null Lars-Inge Tønnessen [VJ# MVP]
4/8/2005 12:00:00 AM
Hi Asilter

Please show us more code. I can read code better than text.

What is "alinanMesaj" ?

Maybe this is a textfield you forget to make a new instance of and assigning
the value to the alinanMesaj.Text filed ? I'm only guessing because I don't
know what "alinanMesaj" is from your very short code sample.


[quoted text, click to view]


Best regards,
Lars-Inge Tønnessen


Re: BufferedReader.readLine() returns null Lars-Inge Tønnessen [VJ# MVP]
4/8/2005 12:00:00 AM

Hi Asilter,

Thanks for the code. I could get it to run. =:o)

You are reading more line than what the server is sending. You are reading
twice, I guess your server is only sending one message/line of text.

**********************************************************
String alinanMesaj=reader.readLine();
if(alinanMesaj!=null)
{
txtMessage.set_Text(reader.readLine());
}
**********************************************************


Here is my simple command prompt server:

package ConsoleApplication1;

/**
* Summary description for Class1.
*/
public class Class1
{
public Class1()
{

System.Net.Sockets.TcpListener listener = new
System.Net.Sockets.TcpListener( 1500 );
listener.Start();
System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
System.Net.Sockets.NetworkStream strem = client.GetStream();

// This code sends two lines to the server. Please note the "\r\n"
ubyte[] out = System.Text.Encoding.get_ASCII().GetBytes("Hello\r\nLine2");

strem.Write( out, 0, out.length );
strem.Close();
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}




Here is my (well, that would be your) client:

package J_asilter;

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

import java.io.*;
import java.net.*;

/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private static System.Windows.Forms.TextBox txtMessage;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private static System.Windows.Forms.TextBox txtConnection;


/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

try
{
Socket client = new Socket("localhost",1500);
txtConnection.set_Text("Kuruldu!");
BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));
String alinanMesaj=reader.readLine();

// One extra line
System.Windows.Forms.MessageBox.Show( alinanMesaj );

if(alinanMesaj!=null)
{
txtMessage.set_Text( reader.readLine());
}
}
catch (IOException ioe)
{
System.err.println ("Error " + ioe);
}
}
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtMessage = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtConnection = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.set_Location(new System.Drawing.Point(16, 64));
this.groupBox1.set_Name("groupBox1");
this.groupBox1.set_Size(new System.Drawing.Size(312, 200));
this.groupBox1.set_TabIndex(3);
this.groupBox1.set_TabStop(false);
this.groupBox1.set_Text("Mesaj");
//
// txtMessage
//
this.txtMessage.set_Location(new System.Drawing.Point(24, 88));
this.txtMessage.set_Multiline(true);
this.txtMessage.set_Name("txtMessage");
this.txtMessage.set_Size(new System.Drawing.Size(296, 168));
this.txtMessage.set_TabIndex(4);
this.txtMessage.set_Text("");
//
// groupBox2
//
this.groupBox2.get_Controls().Add(this.label1);
this.groupBox2.get_Controls().Add(this.txtConnection);
this.groupBox2.set_Location(new System.Drawing.Point(16, 8));
this.groupBox2.set_Name("groupBox2");
this.groupBox2.set_Size(new System.Drawing.Size(184, 48));
this.groupBox2.set_TabIndex(5);
this.groupBox2.set_TabStop(false);
this.groupBox2.set_Text("Baglanti");
//
// txtConnection
//
this.txtConnection.set_Location(new System.Drawing.Point(72, 16));
this.txtConnection.set_Name("txtConnection");
this.txtConnection.set_TabIndex(3);
this.txtConnection.set_Text("");
//
// label1
//
this.label1.set_Font(new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((ubyte)(System.Byte)(((ubyte)162)))));
this.label1.set_Location(new System.Drawing.Point(16, 19));
this.label1.set_Name("label1");
this.label1.set_Size(new System.Drawing.Size(56, 16));
this.label1.set_TabIndex(4);
this.label1.set_Text("Durum :");
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(336, 278));
this.get_Controls().Add(this.groupBox2);
this.get_Controls().Add(this.txtMessage);
this.get_Controls().Add(this.groupBox1);
this.set_Name("Form1");
this.set_Text("Sohbet Programi - Client");
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion
public static void main(String[] args)
{
Application.Run(new Form1());
}
}



Best regards,
Lars-Inge Tønnessen

Re: BufferedReader.readLine() returns null Lars-Inge Tønnessen [VJ# MVP]
4/8/2005 12:00:00 AM

I just have to fix my spelling error:
line = lines.

The sentence should be:
"You are reading more lines than what the server is sending."


Cheers
Lars-Inge =:o)

Re: BufferedReader.readLine() returns null Lars-Inge Tønnessen [VJ# MVP]
4/8/2005 12:00:00 AM

Well, one more thing:

The lower 1000 port numbers are usually reserved by standard services.
Please consider using a higher port number as a good coding practice.


Best regards,
Lars-Inge Tønnessen


[quoted text, click to view]

Re: BufferedReader.readLine() returns null asilter
4/8/2005 11:31:11 AM
that is a String type of data and i know that BufferedReader.readLine()
returns String. My code is below:

package ChatVisual2Client;

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

import java.io.*;
import java.net.*;

/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private static System.Windows.Forms.TextBox txtMessage;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private static System.Windows.Forms.TextBox txtConnection;


/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();

try
{
Socket client=new Socket("192.168.0.139",15);
txtConnection.set_Text("Kuruldu!");
BufferedReader reader=new BufferedReader(new
InputStreamReader(client.getInputStream()));
String alinanMesaj=reader.readLine();
if(alinanMesaj!=null)
{
txtMessage.set_Text(reader.readLine());
}
}
catch (IOException ioe)
{
System.err.println ("Error " + ioe);
}
}
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtMessage = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtConnection = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.set_Location(new System.Drawing.Point(16, 64));
this.groupBox1.set_Name("groupBox1");
this.groupBox1.set_Size(new System.Drawing.Size(312, 200));
this.groupBox1.set_TabIndex(3);
this.groupBox1.set_TabStop(false);
this.groupBox1.set_Text("Mesaj");
//
// txtMessage
//
this.txtMessage.set_Location(new System.Drawing.Point(24, 88));
this.txtMessage.set_Multiline(true);
this.txtMessage.set_Name("txtMessage");
this.txtMessage.set_Size(new System.Drawing.Size(296, 168));
this.txtMessage.set_TabIndex(4);
this.txtMessage.set_Text("");
//
// groupBox2
//
this.groupBox2.get_Controls().Add(this.label1);
this.groupBox2.get_Controls().Add(this.txtConnection);
this.groupBox2.set_Location(new System.Drawing.Point(16, 8));
this.groupBox2.set_Name("groupBox2");
this.groupBox2.set_Size(new System.Drawing.Size(184, 48));
this.groupBox2.set_TabIndex(5);
this.groupBox2.set_TabStop(false);
this.groupBox2.set_Text("Baglanti");
//
// txtConnection
//
this.txtConnection.set_Location(new System.Drawing.Point(72, 16));
this.txtConnection.set_Name("txtConnection");
this.txtConnection.set_TabIndex(3);
this.txtConnection.set_Text("");
//
// label1
//
this.label1.set_Font(new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
((ubyte)(System.Byte)(((ubyte)162)))));
this.label1.set_Location(new System.Drawing.Point(16, 19));
this.label1.set_Name("label1");
this.label1.set_Size(new System.Drawing.Size(56, 16));
this.label1.set_TabIndex(4);
this.label1.set_Text("Durum :");
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(336, 278));
this.get_Controls().Add(this.groupBox2);
this.get_Controls().Add(this.txtMessage);
this.get_Controls().Add(this.groupBox1);
this.set_Name("Form1");
this.set_Text("Sohbet Programı - Client");
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion
public static void main(String[] args)
{
Application.Run(new Form1());
}
}

[quoted text, click to view]
AddThis Social Bookmark Button