【问题标题】:Java not writing to file correctlyJava没有正确写入文件
【发布时间】:2013-08-14 13:02:41
【问题描述】:

这是我的输出:

/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; }
    }
}

任何帮助都会很棒。

【问题讨论】:

  • irc_channel.java 的第 66 行?
  • @jlordo 它是函数名前的空行
  • out 是抛出空指针
  • @phyrrus9:我做了一个复制和粘贴。第 66 行是out.println(line);...

标签: java linux io runtime-error irc


【解决方案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 --> NullPointerException

【讨论】:

  • 我知道这么多。问题是它为什么抛出那个异常
  • 因为new PrintWriter(String filename) 抛出FileNotFoundException 如果给定字符串不表示现有的可写常规文件并且无法创建该名称的新常规文件,或者在打开时出现其他错误或创建文件。
  • @phyrrus9:要了解实际情况,请将 System.out.println("Error open"); 替换为 e.printStackTrace();
  • 我发现了问题,在 irc_log 函数中,它没有发送频道名称。现在我们需要解决这个问题
【解决方案2】:

我找到的解决方案实际上是日志的放置,它需要从服务器移到 readline 的正下方,因为日志正在寻找一个已经从列表中删除的频道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 2015-07-02
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2019-05-07
    相关资源
    最近更新 更多