Java没有正确写入文件

时间:2013-08-14 13:02:41

标签: java linux io runtime-error irc

这是我的输出:

/channel #blarg123
chanswitch: #blarg123
hello
----->PRIVMSG #blarg123 :hello
----->Logging: -->:phyrrus92 :hai ]
Error open
Exception in thread "main" java.lang.NullPointerException
    at irc_channel_obj.writemsg(irc_channel.java:66)
    at irc_channel.sendmsg(irc_channel.java:24)
    at irc.irc_log(irc.java:28)
    at irc.main(irc.java:91)

我不明白为什么它会抛出这个异常,因为所有代码似乎都是正确的并且文件存在(./#blarg123)

以下是代码:

File: irc.java

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

class irc
{
    static Socket server;
    static BufferedReader in;
    static BufferedReader stdin;
    static PrintWriter out;
    static String channel;
    static irc_channel chanlist;

    public static void irc_log(String line)
    {
            System.out.println("----->Logging: " + line);
            String[] splitted = line.split(" ");
            String channel = "", sendline = "";
            if (splitted[0].indexOf("!") != -1)
                    splitted[0] = splitted[0].substring(0, splitted[0].indexOf("!"));
            for (int i = 0; i < splitted.length; i++)
            {
                    sendline += splitted[i];
                    if (splitted[i].indexOf("#") != -1)
                    {
                            channel = splitted[i];
                    }
            }
            chanlist.sendmsg(channel, sendline);
    }

    public static void main(String args[])
    {
            String user_line;
            channel = "none";
            chanlist = new irc_channel();
            try
            {
                    server = new Socket(args[0], 6667);
                    in = new BufferedReader( new InputStreamReader(server.getInputStream()) );
                    stdin = new BufferedReader( new InputStreamReader(System.in) );
                    out = new PrintWriter(server.getOutputStream());
            }
            catch (UnknownHostException e) {}
            catch (IOException e) {}
            irc_in input = new irc_in(server, out, stdin);
            Thread t = new Thread(input);
            t.start();
            while (true)
            {
                    try {
                            user_line = in.readLine();
                            String[] splitted = user_line.split(" ");
                            if (splitted[0].equals("PING"))
                            {
                                    out.print("PONG " + splitted[1] + "\r\n");
                                    out.flush();
                                    continue;
                            }
                            boolean chan_filtered = false;
                            if (splitted[0].indexOf("!") != -1)
                            {
                                    splitted[0] = "" + splitted[0].substring(0, splitted[0].indexOf("!"));
                            }
                            for (int i = 1; i < splitted.length && !chan_filtered; i++)
                            {
                                    if (splitted[i].equals(channel))
                                    {
                                            chan_filtered = true;
                                            user_line = "\u001B[0;32m-->" + splitted[0] + " ";
                                            for (int x = 3; x < splitted.length; x++)  //x=i
                                            {
                                                    user_line += splitted[x] + " ";
                                            }
                                            user_line += "\u001B[0m]";
                                    }
                                    else if (splitted[i].indexOf("#") != -1)
                                    {
                                            chan_filtered = true;
                                            user_line = "\u001B[0;31m-->" + splitted[0] + " ";
                                            for (int x = 2; x < splitted.length; x++) //x=i
                                            {
                                                    user_line += splitted[x] + " ";
                                            }
                                            user_line += "\u001B[0m]";
                                    }
                            }
                            if (!channel.equals("none"))
                            {
                                    irc_log(user_line);
                                    //chanlist.displayall(channel);
                            }
                            //else
                            {
                                    System.out.println(user_line);
                                    System.out.flush();
                            }
                            //Thread.sleep(2000);
                    } catch (IOException e) {}
            }
    }
}

class irc_in implements Runnable
{
    static Socket server;
    static PrintWriter out;
    static BufferedReader stdin;

    irc_in(Socket a, PrintWriter b, BufferedReader c)
    {
            server = a;
            out = b;
            stdin = c;
    }

    public void run()
    {
            String user_line;
            while (true)
            {
                    try
                    {
                            //Thread.sleep(1000);
                            user_line = stdin.readLine();
                            String[] splitted=user_line.split(" ");
                            if (splitted[0].equals("/channel") || splitted[0].equals("/JOIN"))
                            {
                                    user_line = "";
                                    splitted[0] = splitted[0].substring(1, splitted[0].length());
                                    for (int i = 0; i < splitted.length; i++)
                                            user_line += splitted[i] + " ";
                                    irc.channel = splitted[1];
                                    System.out.println("chanswitch: " + irc.channel);
                                    if (splitted[0].equals("channel"))
                                            continue;
                            }

                            else if (splitted[0].equals("/setname") && splitted.length >= 2)
                            {
                                    String user_name = "";
                                    for (int i = 0; i < splitted.length; i++)
                                            user_name += splitted[i];
                                    out.print("NICK " + splitted[1] + "\r\n");
                                    out.print("USER " + splitted[1] + " * 8 : " + user_name + "\r\n");
                                    out.flush();
                            }

                            else if (!splitted[0].startsWith("/"))
                            {
                                    int startpos = 0;
                                    String send_channel = irc.channel;
                                    if (splitted[0].startsWith("#"))
                                    {
                                            send_channel = splitted[0];
                                            startpos = 1;
                                    }
                                    user_line = "PRIVMSG " + send_channel + " :";
                                    for (int i = startpos; i < splitted.length; i++)
                                            user_line += splitted[i] + " ";
                                    System.out.println("----->" + user_line);
                            }

                            else
                                    user_line = user_line.substring(1, user_line.length());

                            out.print(user_line + "\r\n");
                            out.flush();
                    }
                    catch (IOException e) {}
            }
    }
}

另一个文件

File: irc_channel.java
import java.io.*;
import java.util.*;

public class irc_channel
{
    public static ArrayList<irc_channel_obj> list;

    irc_channel()
    {
            list = new ArrayList<irc_channel_obj>();
    }

    public void sendmsg(String channel, String line)
    {
            for (int i = 0; i < list.size(); i++)
            {
                    if (list.get(i).name.equals(channel))
                    {
                            list.get(i).writemsg(line);
                            return;
                    }
            }
            irc_channel_obj newchan = new irc_channel_obj(channel);
            newchan.writemsg(line);
            list.add(newchan);
    }

    public void displayall(String channel)
    {
            for (int i = 0; i < list.size(); i++)
            {
                    if (list.get(i).name.equals(channel))
                    {
                            list.get(i).displayall();
                            return;
                    }
            }
            System.out.println("No messages for channel " + channel);
    }
}

class irc_channel_obj
{
    public static String name;
    static PrintWriter out;
    static BufferedReader in;

    irc_channel_obj(String chan)
    {
            name = chan;
            try
            {
                    out = new PrintWriter(name);
                    out.println("Channel: " + name);
                    out.flush();
            }
            catch (FileNotFoundException e)
            {
                    System.out.println("Error open");
                    return;
            }
    }

    public void writemsg(String line)
    {
            out.println(line);
            out.flush();
    }

    public void displayall()
    {
            String line = "";
            try
            {
                    in = new BufferedReader( new FileReader(name) );
                    while ((line = in.readLine()) != null)
                    {
                            System.out.println(line);
                    }
            }
            catch (FileNotFoundException e) { return; }
            catch (IOException e) { return; }
    }
}

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

输出的相关部分:

Error open
Exception in thread "main" java.lang.NullPointerException
    at irc_channel_obj.writemsg(irc_channel.java:66)

Error open表示您的irc_channel_obj构造函数投放FileNotFoundException。这就是out保持null的原因。

稍后,在writemsg(String line)中,您致电

out.println(line);

其中out仍为null - &gt; NullPointerException

答案 1 :(得分:0)

我找到的解决方案实际上是日志的放置,需要将其从服务器的readline下移到右侧,因为日志正在查找已从列表中删除的频道。