[Lỗi] DispatchEvent

lonelyboy26

New Member
2/1/14
2
0
1
28
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.i:confused:utputStream;
import java.i:confused:utputStreamWriter;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
//import javax.swing.SwingUtilities;
import javax.swing.SwingUtilities;
import com.intel.bluetooth.RemoteDeviceHelper;

import javax.swing.JTextField;
import javax.swing.JSplitPane;
import javax.swing.JDesktopPane;

import java.awt.Button;
public class Blu_sw {
BluetoothModel bluetoothModel = new BluetoothModel();
private JFrame frame;
public JLabel lblStatus = new JLabel("Status");
public final JButton btnConnect = new JButton("Connect");
public JButton btnSearchDevice = new JButton("Search");
public JComboBox<String> comboBox = new JComboBox();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Blu_sw window = new Blu_sw();
window.frame.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}


});

// Blu_sw window = new Blu_sw();
// window.Initialize();
//
}
public boolean flagPaircode = true;
private JTextField tfPairCode;
public void Initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 996, 712);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
btnSearchDevice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnSearchDevice.setEnabled(false);
comboBox.removeAllItems();
bluetoothModel.clearVecDevices();
SearchThread searchThread = new SearchThread();
searchThread.startThread();
}
});
btnSearchDevice.setBounds(88, 11, 120, 32);
frame.getContentPane().add(btnSearchDevice);
comboBox.setBounds(232, 11, 319, 32);
frame.getContentPane().add(comboBox);
btnConnect.setEnabled(false);
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (btnConnect.getText().equals("Connect")) {
btnConnect.setText("Connecting...");
ConnectThread connectThread = new ConnectThread();
connectThread.startThread();

} else {

try {
bluetoothModel.disConnect();
btnConnect.setText("Connect");

// lblStatus.setText("Disconnected");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
lblStatus.setText("Can not Disconnect");
}
}
}
});
btnConnect.setBounds(575, 11, 146, 32);
frame.getContentPane().add(btnConnect);
lblStatus.setBounds(10, 659, 588, 14);
frame.getContentPane().add(lblStatus);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setBounds(858, 616, 99, 32);
btnExit.setFont(new java.awt.Font(null, 1, 15));
frame.getContentPane().add(btnExit);
JButton btnCancer = new JButton("Cancer");
btnCancer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
bluetoothModel.cancelConnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnCancer.setBounds(737, 11, 89, 32);
frame.getContentPane().add(btnCancer);tfPairCode = new JTextField();
tfPairCode.setText("sumo20");
;
tfPairCode.setBounds(10, 624, 86, 20);
frame.getContentPane().add(tfPairCode);
tfPairCode.setColumns(10);

// comboBox.setBounds(232, 11, 319, 32);
// frame.getContentPane().add(comboBox);

JLabel lblPairingCode = new JLabel("Pairing Code");
lblPairingCode.setBounds(10, 599, 86, 14);
frame.getContentPane().add(lblPairingCode);

}
public class BluetoothModel implements DiscoveryListener {
// BluetoothTotal a = new BluetoothTotal();

// object used for waiting
private Object lock = new Object();
private StreamConnection streamConnection = null;
// vector containing the devices discovered
private Vector<RemoteDevice> vecDevices = new Vector<RemoteDevice>();
private Vector<String> vecNameDevices = new Vector<String>();
private RemoteDevice remoteDevice;
// device connection address
private String connectionURL = null;
private RemoteDevice device;
private LocalDevice localDevice;
private DiscoveryAgent agent;
private UUID[] uuidSet = new UUID[1];
private InputStream inStream;
private OutputStream outStream;
private PrintWriter pWriter;
private BufferedReader bReader2;
private String lineRead = "123 123 123";
private Boolean connected = false;
public void runClient() throws IOException {

// display local device address and name
localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: " + localDevice.getBluetoothAddress());
System.out.println("Name: " + localDevice.getFriendlyName());

// find devices
agent = localDevice.getDiscoveryAgent();
lblStatus.setText("Starting device inquiry...");
agent.startInquiry(DiscoveryAgent.GIAC, this);

// avoid callback conflicts
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}

}
public RemoteDevice getRemoteDeviceInCombo() {
return (RemoteDevice) (bluetoothModel.listDevice()
.elementAt((comboBox.getSelectedIndex())));

}

public Vector listDevice() {
return this.vecDevices;
}
public void connect(RemoteDevice remoteDevice) throws IOException {
connectionURL = null;
// JFrame a = new JFrame();
// a.setVisible(true);
if (!remoteDevice.isAuthenticated()) {
//
// PairCodeThread pairCodeThread = new PairCodeThread();
// pairCodeThread.start();
// while(flagPaircode);

lblStatus.setText("Try to pair with "
+ remoteDevice.getFriendlyName(false) + " PIN: "
+ tfPairCode.getText());
boolean paired = RemoteDeviceHelper.authenticate(remoteDevice,
tfPairCode.getText());
lblStatus.setText("Pair with "
+ remoteDevice.getFriendlyName(false)
+ (paired ? " succesfull" : " failed"));
}


uuidSet[0] = new UUID("1101", true); // serial, SPP

lblStatus.setText("\nSearching for services...");
uuidSet[0] = new UUID("1101", true); // serial, SPP
lblStatus.setText("\nSearching for services...");
agent.searchServices(null, uuidSet, remoteDevice, this);
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}

// check
if (connectionURL == null) {
lblStatus.setText("Device does not support Service.");
// System.exit(0);
}
try {
streamConnection = (StreamConnection) Connector
.open(connectionURL);
// streamConnection.close();
connected = true;
outStream = streamConnection.openOutputStream();
pWriter = new PrintWriter(new OutputStreamWriter(outStream));

inStream = streamConnection.openInputStream();
bReader2 = new BufferedReader(new InputStreamReader(inStream));

} catch (Exception e) {
e.printStackTrace();
lblStatus.setText("Can not connect to device");
}

}
public void cancelConnect() throws IOException {
synchronized (lock) {
lock.notify();
}
}

public void disConnect() throws IOException {
if (streamConnection != null) {
outStream.close();
inStream.close();
streamConnection.close();
connected = false;
lblStatus.setText("Disconnect...");
}

}
public void clearVecDevices() {
vecDevices.removeAllElements();
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
// add the device to the vector
lblStatus.setText("Device Discovered...");
if (!vecDevices.contains(btDevice)) {
vecDevices.addElement(btDevice);
try {
if (btDevice.getFriendlyName(false).equals(""))
comboBox.addItem("No Name" + " --> " + btDevice);

else
comboBox.addItem(btDevice.getFriendlyName(false)
+ " --> " + btDevice);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
if (servRecord != null && servRecord.length > 0) {
connectionURL = servRecord[0].getConnectionURL(0, false);
lblStatus.setText(connectionURL);

}
synchronized (lock) {
lock.notify();
}
}
public void serviceSearchCompleted(int transID, int respCode) {
lblStatus.setText("Service Search is Completed...");
synchronized (lock) {
lock.notify();
}
}
public void inquiryCompleted(int discType) {
lblStatus.setText("Inquiry is Completed...");
btnSearchDevice.setEnabled(true);
// searchThread.
// searchThread.setIsTerminating(true); // tell the thread to stop
// searchThread.join(); // wait for the thread to stop
if (vecDevices.isEmpty() == true) {
lblStatus.setText("Can not Find device");
} else {
btnConnect.setEnabled(true);

}
synchronized (lock) {
lock.notify();
}

}// end method




}
public class SearchThread extends Thread {
public void run() {
try {

bluetoothModel.runClient();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void startThread() { // this.stop();
this.start();

}

@SuppressWarnings("deprecation")
public void stopThread() {
this.interrupt();
// this.start();

}
}
public class ConnectThread extends Thread {
public void run() {
try {
bluetoothModel.connect(bluetoothModel.getRemoteDeviceInCombo());
btnConnect.setText("Disconnect");
lblStatus.setText("Connected");

System.out.println("hai.....");
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
lblStatus.setText("Can not Connect");
}
}

public void startThread() { // this.stop();
this.start();

}

}
public class PairCodeThread extends Thread {
private JFrame jfPairCode;

public void run() {


jfPairCode = new JFrame();
jfPairCode.setBounds(100, 100, 300, 300);
jfPairCode.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfPairCode.getContentPane().setLayout(null);
jfPairCode.setVisible(true);
}
}


}
ở trên là code của mình viết giao tiếp bluetooth, khi save thì k có lỗi nhưng khi build thì hiện ra lỗi thế này:
java.lang.NullPointerException
at Blu_sw$1.run(Blu_sw.java:61)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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.ProtectionDomain$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)
Mình có google nhưng vì mới học Java nên k hiểu, bạn nào biết chỉ fix lỗi giúp mình, cảm ơn
 

Joe

Thành viên VIP
21/1/13
2,971
1,312
113
Boy,
who he can read is at an advantage. Learn English and scrutinize these lines:
PHP:
java.lang.NullPointerException
at Blu_sw$1.run(Blu_sw.java:61)
...
1) what is the number 61?
2) what is in this line?
3) what is the name?
4) Have you instantiated it (or initialize) and yes then where?
Tell me what you've found and I'll tell you how to fix it.
 

Joe

Thành viên VIP
21/1/13
2,971
1,312
113
where are the answers for the question 3 and 4?