-2

I have bot connected to IRC server and its not crashing all the time the bot is crashing with Exception in thread "main" java.lang.NullPointerException at bot.Report.main(Report.java:47) The 47 line is

if (!(str.split(" ")[1].equals("QUIT") || str.split(" ")[1].equals("PART") || str.split(" ")[1].equals("JOIN")))

Edit more code

String str = null;    
PrintWriter o;
            Socket s;
            BufferedReader i1;
            s = new Socket("", 6667);
            i1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
            o = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
            o.print("PASS " + "" + "\r\n");
            o.print("USER " + "" + " 0 * :" + ""
                    + "\r\n" + "NICK " + "" + "\r\n");
            o.print("JOIN " + "#test" + "\r\n");
            o.flush();
    while (s.isConnected()) {
                str = i1.readLine();
                StringBuffer stringBuffer = null;
    if (!(str.split(" ")[1].equals("QUIT") || str.split(" ")[1].equals("PART") || str.split(" ")[1].equals("JOIN"))) {
    ......

2 Answers2

1

It seems that if(str != null) fixed my problem.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
1

As a good practice, you should always, check for null, before requesting for any of its, properties / methods.

if you could rewrite your if statement

if(str!=null && (!(str.split(" ")[1].equals("QUIT") || str.split(" ")[1].equals("PART") || str.split(" ")[1].equals("JOIN")))) ......

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57