Java 没有正确写入文件

标签 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; }
    }
}

任何帮助都会很棒。

最佳答案

输出的相关部分:

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 --> NullPointerException

关于Java 没有正确写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232496/

相关文章:

java - 从其他方法的 await() 返回

java - 尝试将 Eclipse 项目从 mac 笔记本电脑导入到 windows 服务器

java - 如何使用 JNA (Java) 获取 Activity 窗口的 X11 WM_CLASS?

c++ - 在 Linux 中对只读文件设置排他锁

java - 从文件 java 读取原始字节(字节序问题)

java - 如何调试黑莓验证错误?

linux - 测量代码性能时的虚拟机或双启动

linux - GDB 在 Eclipse 中不显示某些值

Java:如何从用户指定的文件进行扫描

c - C select() 函数是否已弃用?