如何成功更改此字段的值?我收到一个我不明白的错误

时间:2014-04-10 21:07:35

标签: java swing runtime-error

这是它引用的代码。我正在更改变量的值,每次按下按钮进行更改时都会出现此错误消息。

我在“newclient”所属的类中有一个名为“发送”的私有字段。这是在一个while(true)循环中改变的,这可能有所不同,但我正在使用Thread.sleep(1000)等待。

JButton right = new JButton("Right");
        right.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                newclient.setSending("MOVE E");  <---- this line
                // ######################
            }
        });
        right.setBackground(new Color(100, 149, 237));
        right.setBounds(375, 535, 100, 40);

以下是错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at playerGUI$4.actionPerformed(playerGUI.java:117)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6414)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
        at java.awt.Component.processEvent(Component.java:6179)
        at java.awt.Container.processEvent(Container.java:2084)
        at java.awt.Component.dispatchEventImpl(Component.java:4776)
        at java.awt.Container.dispatchEventImpl(Container.java:2142)
        at java.awt.Component.dispatchEvent(Component.java:4604)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
        at java.awt.Container.dispatchEventImpl(Container.java:2128)
        at java.awt.Window.dispatchEventImpl(Window.java:2492)
        at java.awt.Component.dispatchEvent(Component.java:4604)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
        at java.awt.EventQueue.access$400(EventQueue.java:82)
        at java.awt.EventQueue$2.run(EventQueue.java:676)
        at java.awt.EventQueue$2.run(EventQueue.java:674)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:690)
        at java.awt.EventQueue$3.run(EventQueue.java:688)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

这是更改变量的Client类。

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

public class Client{
    private String sending = "LOOK";

    public void setSending(String text){
        sending = text;
    }

    public Client(playerGUI GUI){   
        try{
            final Socket sock = new Socket("localhost",4444);
            final DataInputStream in = new DataInputStream(sock.getInputStream());
            final PrintStream out = new PrintStream(sock.getOutputStream());
            DataInputStream inputLine = new DataInputStream(new BufferedInputStream(System.in));

            final Thread serverResponse = new Thread(){
                public void run(){
                    System.out.println("DUNGEON OF DOOM HAS STARTED");
                    if(sock != null){
                        if(in != null){
                            try{
                                String response;
                                while((response = in.readLine()) != null){
                                    //GUI.processgrid(response);
                                    //send to GUI to process output!
                                    System.out.println(response);
                                }
                            }catch(UnknownHostException uhe){
                                System.err.println("Unknown host1: " + uhe);
                            }catch(IOException ioe){
                                System.err.println("IOException1: " + ioe);
                            }catch(NullPointerException npe){
                                System.err.println("Null Pointer1: " + npe);
                            }
                        }
                    }
                }
            };
            serverResponse.start();
            if(sock != null){
                if(out != null){
                    try{
                        while(true){
                            //inputbuttons!
                            //String sending = inputLine.readLine();
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            out.println(sending);
                            if(sending.equals("QUIT")) break;
                        }
                    /*}catch(UnknownHostException uhe2){
                        System.err.println("Unknown host2: " + uhe2);
                    }catch(IOException ioe2){
                        System.err.println("IOException2: " + ioe2);*/
                    }catch(NullPointerException npe2){
                        System.err.println("Null Pointer2: " + npe2);
                    }
                }
            }
            out.close();
            in.close();
            sock.close();
        }catch(UnknownHostException uhe3){
            System.err.println("Unknown host3: " + uhe3);
        }catch(IOException ioe3){
            System.err.println("IOException3: " + ioe3);
        }catch(NullPointerException npe3){
            System.err.println("Null Pointer3: " + npe3);
        }   
    }
}

0 个答案:

没有答案