郁闷!Netc我在一台机器上运行成功了,可是到了另外一台机器却又运行时出Exception in Thread "main" java.lang.NullpointerException at Netc.main(Netc.java:59) 而两台机器上我对BAT文件中的CLASSPATH和path设置都是一样的,真搞不清为什么??
那很明显是路径没有设置对啊 set classpath 试一试啊
应该是路径不对,但我在两台机器上设置的是一样的啊,都是: SET CLASSPATH=.;C:\JDK\LIB;C:\JDK\LIB\TOOLS.JAR;C:\JDK\JRE\LIB\RT.JAR;C:\JDK\LIB\DT.JAR 可为什么会出现一台可以运行,一台出错呢?
最关键的一点,你看哪个东西为null.
????sock[i]=new Socket(Serveraddr[i],1237);???? i think it is as : ...... ...................... ServerSocket serverSocket=new ServerSocket(PORT); sock[i]=serverScoket.accept(); ....................... .....................
完整的服务器端程序是:
import java.net.*; /*1*/
import java.io.*;
import java.awt.*;
import java.lang.*;public class Netc
{
static Socket sock[];
static InetAddress Serveraddr[];
static DataInputStream datain[]; /*10*/
static DataOutputStream dataout[];
static int NumServers;
static String Servernames[];
public static void main(String args[]) throws IOException
{
int i;
DataInputStream ServerConfigFile;
String IntString=null,Severname[];
ServerConfigFile=new DataInputStream(new FileInputStream("srvrcfg.txt"));
try{
IntString=ServerConfigFile.readLine(); /*20*/
}
catch(IOException ioe){
System.out.println("Error reading the # servers");
System.exit(1);
}
try{
NumServers=Integer.parseInt(IntString);
}
catch(NumberFormatException nfe){
System.out.println("r servers is not an integer."); /*30*/
System.exit(1);
}
Servernames=new String[NumServers];
sock=new Socket[NumServers];
datain=new DataInputStream[NumServers];
dataout=new DataOutputStream[NumServers];
for(i=0;i<NumServers;i++){
try{
Servernames[i]=ServerConfigFile.readLine();
} /*40*/
catch(IOException e){
System.out.println("Error reading server names");
System.exit(1);
}
Servernames[i]=Servernames[i].trim();
}
try{
ServerConfigFile.close();
}
catch(IOException e){ /*50*/
System.out.println("I/O error while closing file");
System.exit(1);
}
try{
for(i=0;i<NumServers;i++)
{Serveraddr[i]=InetAddress.getByName(Servernames[i]);
sock[i]=new Socket(Serveraddr[i],1237);
datain[i]=new DataInputStream(new BufferedInputStream(sock[i].getInputStream()));
dataout[i]=new DataOutputStream(new BufferedOutputStream(sock[i].getOutputStream()));
}
}
catch(IOException E){
System.out.println("I/O error opening streams sockets.");
System.exit(1);
}
ClientBody();
try{ /*70*/
for(i=0;i<NumServers;i++)
{dataout[i].close();
datain[i].close();
sock[i].close();
}
}
catch(IOException E){
System.out.println("Error closing streams and sockets");
System.exit(1);
}
}
public static void ClientBody() throws IOException
{
int i,j,k;
int TotNum=0,NumRows=0;
int A[][],B[][],C[][];
DataInputStream ClientConfigFile;
String IntString=null;
ClientConfigFile=new DataInputStream(new FileInputStream("clicfg.txt"));
try{
IntString=ClientConfigFile.readLine();
}
catch(IOException ioe){
System.out.println("Error reading N from file.");
System.exit(1);
}
try{
TotNum=Integer.parseInt(IntString);
}
catch(NumberFormatException nfe){
System.out.println("The value for N is not an integer.");
System.exit(1);
}
try{
ClientConfigFile.close();
}
catch(IOException e){
System.out.println("I/O error closing client config file.");
System.exit(1);
}
NumRows=TotNum/NumServers;
A=new int[TotNum][TotNum];
B=new int[TotNum][TotNum];
C=new int[TotNum][TotNum];
for(i=0;i<TotNum;i++)
for(j=0;j<TotNum;j++)
{A[i][j]=i;
B[i][j]=j;
C[i][j]=0;}
System.out.println("Sending information to servers."); /*122*/
try{
for(i=0;i<NumServers;i++)
{dataout[i].write(TotNum);
dataout[i].write(NumRows);
dataout[i].flush();}
for(j=NumRows*i;j<NumRows*(i+1);j++)
for(k=0;k<TotNum;k++)
dataout[i].writeInt(A[j][k]);
dataout[i].flush();
for(j=0;j<TotNum;j++)
for(k=0;k<TotNum;k++)
dataout[i].writeInt(B[j][k]);
dataout[i].flush();
}
catch(IOException ioe){
System.out.println("I/O error sending matrix to server.");
System.exit(1);
}
try{
for(i=0;i<NumServers;i++)
for(j=NumRows*i;j<NumRows*(i+1);j++)
for(k=0;k<TotNum;k++)
C[j][k]=datain[i].readInt();
}
catch(IOException ioe){
System.out.println("I/O error receiving result from server");
System.exit(1);
}
System.out.println();
System.out.println("Resultant Matrix");
System.out.println();
for(i=0;i<TotNum;i++){
for(j=0;j<TotNum;j++)
System.out.print(C[i][j]+" ");
System.out.println();
}
}
}
59到底是哪一行?是下面这行吗?
{Serveraddr[i]=InetAddress.getByName(Servernames[i]);
把具体的异常全打出来.
Serveraddr[i]=InetAddress.getByName(Servernames[i]);
具体的异常怎么看啊?
我有srvrcfg.txt文件啊。
try{
}catch(Exception ex) {
ex.printStackTrace();
}
当然,首先你的classpath设置是正确的。
java -classpath Netc
试试!
而两台机器上我对BAT文件中的CLASSPATH和path设置都是一样的,真搞不清为什么??
set classpath 试一试啊
SET CLASSPATH=.;C:\JDK\LIB;C:\JDK\LIB\TOOLS.JAR;C:\JDK\JRE\LIB\RT.JAR;C:\JDK\LIB\DT.JAR
可为什么会出现一台可以运行,一台出错呢?
i think it is as :
......
......................
ServerSocket serverSocket=new ServerSocket(PORT);
sock[i]=serverScoket.accept();
.......................
.....................