| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 295 人关注过本帖
标题:求助程序调试出错提示 java.lang.NullPointerException
收藏  订阅  推荐  打印
lbqxl0122
Rank: 1
等级:新手上路
帖子:2
积分:130
注册:2008-5-1
求助程序调试出错提示 java.lang.NullPointerException

要求是服务器的Server2.java同过套接字将两个文件压缩后发送给客户端,客户端接受.

下面程序是调试服务端有问题,客户端没问题.

Server2.java
import java.io.*;
import java.net.*;
import java.util.zip.*;

public class Server2
{
    public static void main(String args[])
    {
        ServerSocket server = null;
        ServerThread thread;
        Socket you = null;
        while(true)
        {
            try
            {
                server = new ServerSocket(4331);
            }
            catch(IOException e1)
            {
                System.out.println("正在监听");
            }
            try
            {
                System.out.println("等待客户呼叫");
                you = server.accept();
                System.out.println("客户地址:"+you.getInetAddress());
            }
            catch(IOException e)
            {
                System.out.println("正在等待客户");
            }
            if(you!=null)
            {
                new ServerThread(you).start();
            }
        }
    }
}

class ServerThread extends Thread
{
    Socket socket;
    ZipOutputStream out;
    String s = null;
    ServerThread(Socket t)
    {
        socket = t;
        try
        {
            out = new ZipOutputStream(socket.getOutputStream());
        }
        catch(IOException e){}
    }
    public void run()
    {
        try
        {
            out.putNextEntry(new ZipEntry("Example.java"));
            FileInputStream reader = new FileInputStream("Example.java");
            byte b[]=new byte[1024];
            int n = -1;
            while((n=reader.read(b,0,1024))!=-1)
            {
                out.write(b, 0, n);
            }
            out.putNextEntry(new ZipEntry("E.java"));
            reader = new FileInputStream("E.java");
            n = -1;
            while((n = reader.read(b, 0, 1024))!=-1)
            {
                out.write(b, 0, n);
            }
            reader.close();
            out.close();
        }
        catch(IOException e){}
    }
}

总是提示:
java.lang.NullPointerException
    at Server2.main(Server2.java:25)
Exception in thread "main"

查了好久了,就是不知错在哪,请大家帮忙看看.
2008-7-30 21:43
msi110
Rank: 1
等级:新手上路
帖子:7
积分:174
注册:2008-4-15

应该是
server = new ServerSocket(4331);
这句有错吧。
ServerSocket的参数应该是Socket类型的,不能直接写成4331。把它转换成Socket应该就可以了吧。
你试试·····
2008-8-16 18:57
共有 294 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.052335 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved