如何更好地调试Java中的nullpointer异常

时间:2013-04-20 21:32:01

标签: java serialization client-server objectoutputstream

我正在创建一个将来回发送可序列化对象的客户端/服务器程序。 我不知道如何发生nullpointer异常它指向btnSave响应Click事件的行,并且OutputStream对象尝试写

现在只实现此功能,以防有人测试代码。

可序列化对象:

import java.io.Serializable;


public class Contact implements Serializable {

private String Name;
private String Phone;   
private Operation operation;

public Contact(String n, String p, Operation op){
    this.operation = op;
    this.Name=n;
    this.Phone = p;
}
public String toString(){
    return this.Name + this.Phone;
}

protected enum Operation{
    save,
    read
}

}

客户代码:

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class Client extends JFrame {

private JTextField txtName = new JTextField();
private JTextField txtPhone = new JTextField();

private JLabel lblName = new JLabel(" Name ");
private JLabel lblPhone = new JLabel(" Phone ");

private JButton btnSave = new JButton(" Save ");
private JButton btnRead = new JButton(" Read ");

private JPanel pan = new JPanel();

private JTextArea jta = new JTextArea(5,5);


private ObjectOutputStream Send;
private ObjectInputStream Receive;

public static void main(String[] args) {
new Client();
}

public Client() {

btnSave.addActionListener(new Listener() );
pan.setLayout(new GridLayout(2,3));

pan.add(lblName);
pan.add(txtName);
pan.add(btnSave);

pan.add(lblPhone);
pan.add(txtPhone);
pan.add(btnRead);

setLayout(new BorderLayout());
add(pan, BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.SOUTH);

setTitle("Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); 

try {
  Socket socket = new Socket("localhost", 8000);
  Receive = new ObjectInputStream(socket.getInputStream());

  Send    = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
  jta.append(ex.toString() + '\n');
}
}

private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e){
    Contact c;
    if (e.getSource()==btnSave){
        c = new Contact(txtName.getText(), txtPhone.getText(), Contact.Operation.save);
    }
    else
    {
        c = new Contact(txtName.getText(), txtPhone.getText(),     Contact.Operation.read);
    }
    try{
    Send.writeObject(c);
    Send.flush();

    /*
     * try{
        Contact x = (Contact)Receive.readObject();
        JOptionPane.showMessageDialog(null, c.toString());
    }
        catch(ClassNotFoundException excee)
    {}
    */
    }
    catch(IOException exc)
    {

    }
}
}
}

服务器是:

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import java.awt.*;
import javax.swing.*;

public class PhoneServer extends JFrame {
// Text area for displaying contents
private JTextArea jta = new JTextArea();

public static void main(String[] args) {
new PhoneServer();
}

public PhoneServer() {

setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);

setTitle("MultiThreadServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); 

try {

  ServerSocket serverSocket = new ServerSocket(8000);
  jta.append("Phone Server started at " + new Date() + '\n');

  int clientNo = 1;

  while (true) {

    Socket socket = serverSocket.accept();
    InetAddress inetAddress = socket.getInetAddress();

    jta.append("New Client: #" + clientNo + " From: "+inetAddress.getHostAddress()+'\n');

    HandleAClient task = new HandleAClient(socket);
    new Thread(task).start();
        clientNo++;
  }
}
catch(IOException ex) {
  System.err.println(ex);
}
}

class HandleAClient implements Runnable {
private Socket socket;

public HandleAClient(Socket socket) {
  this.socket = socket;
}

public void run() {
    Matcher matcher;

    try {

    ObjectInputStream inputFromClient = new ObjectInputStream(socket.getInputStream());
    ObjectOutputStream outputToClient = new ObjectOutputStream(socket.getOutputStream());

    while (true) {
        try{
        Contact x = (Contact)inputFromClient.readObject();
        JOptionPane.showMessageDialog(null, x.toString());
        }
        catch(ClassNotFoundException excr){}

        //outputToClient.write(("Phone is " + phone + " and name is " + name).getBytes());//dataArray);
        //outputToClient.write(data.getBytes());
    }
  }
  catch(IOException e) {
    System.err.println(e);
  }
}
}
}

ERROR:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Client$Listener.actionPerformed(Client.java:76)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

从堆栈跟踪中,它显示错误在第76行。

Send.writeObject(c);

表示Send对象为空。

另外,在第54-62行

try {
  Socket socket = new Socket("localhost", 8000);
  Receive = new ObjectInputStream(socket.getInputStream());

  Send    = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
  jta.append(ex.toString() + '\n');
}

你吞下了例外。因此,即使"发送"对象没有被创建,你继续执行你的程序。

第76行假定已创建发送对象。您可能需要检查null并在此处采取适当的操作。

或者,或者,当存在异常时,您可以在第61行中抛出致命错误。

答案 1 :(得分:1)

我编译了这个并做了一些测试。我假设在调用

时,以某种方式发送对象为空
Send.writeObject(c);


添加:

Send = new ObjectOutputStream(new Socket("localhost", 8000).getOutputStream());

在修复错误之前的行中,一切都按预期工作。