求指导: Exception in thread "Thread-2" java.lang.NullPointerException

Elehum 2015-05-05 09:48:40
JAVA新人求指导:
我想用JSlider取值并通过串口将所获得的值发送出去。但目前遇到的问题是Exception in thread "Thread-2" java.lang.NullPointerException
at msproject.Connection.run(Connection.java:49)
at java.lang.Thread.run(Unknown Source)
这个问题不知道应该如何解决。求解答,谢谢!
代码如下:
Connection.java实现通信连接,并使用Thread发送获取数值。
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Connection implements Runnable{
/**
* 实现通信
*/
Test gui;
Value value;
CommPortIdentifier portName;
InputStream input;
OutputStream output;
SerialPort serialPort;
String str = "";

// string format "c+hue+s+sat+l+light+e"
//String dataForm = "c";

Connection(Test gui) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException{
this.gui = gui;
//this.value = value;
portName = CommPortIdentifier.getPortIdentifier("COM4");
serialPort = (SerialPort)portName.open("COMM", 100);
serialPort.setSerialPortParams(115200, 8, 1, 0);

}

// write data to the output stream
void Tx_string(String str) throws IOException{
for(int i=0; i<str.length();i++){
output.write((int)str.charAt(i));
}
}

@Override
public void run(){
while(true){
try{

input = new BufferedInputStream(serialPort.getInputStream());
output = serialPort.getOutputStream();

output.write(123); // handshake

Tx_string(value.formedData(value.getHue(), value.getSat(), value.getLight()));


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

}
}

Test.java实现GUI。里面使用了三个slider获取数值。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;

public class Test extends JFrame{
/**
* This class is used for build GUI
*/
private static final long serialVersionUID = 1L;
static Test gui;
Value val;
HSLValue hsl = new HSLValue();

JButton start;
JPanel sPanel;

public Test(){

this.setTitle("GUI");
this.setLayout(new BorderLayout());
//this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
super.windowClosing(e);
System.exit(0);
}
});
/**
* start.addActionListener()实现点击按钮启动thread。
*/
start = new JButton("START");
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Connection co;
try{
co = new Connection(gui);
Thread thread_com = new Thread(co);
thread_com.start();
}catch(NoSuchPortException e1){
e1.printStackTrace();
}catch(PortInUseException e1){
e1.printStackTrace();
}catch(UnsupportedCommOperationException e1){
e1.printStackTrace();
}
}
});

sPanel = new JPanel();
sPanel.setLayout(new FlowLayout());
sPanel.add(start);

this.add(sPanel, BorderLayout.NORTH);
this.add(hsl, BorderLayout.CENTER);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500, 600);
this.setVisible(true);

}

public static void main(String arg[]){
new Test().setVisible(true);

}
}

class HSLValue extends JPanel{
/**
* create a slider panel.
*/
private static final long serialVersionUID = 1L;
Value val = new Value();
JSlider hueSlider, satSlider, lightSlider;

JLabel hueLabel = new JLabel("HUE SLIDER");
JLabel satLabel = new JLabel("SATURATION SLIDER");
JLabel lightLabel = new JLabel("LIGHT SLIDER");
JLabel hueValue = new JLabel("");
JLabel satValue = new JLabel("");
JLabel lightValue = new JLabel("");

JPanel sliderPanel;

public HSLValue(){

hueSlider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
hueSlider.addChangeListener(new SliderListener());
satSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
satSlider.addChangeListener(new SliderListener());
lightSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
lightSlider.addChangeListener(new SliderListener());

sliderPanel = new JPanel();
sliderPanel.setLayout(new GridLayout(9, 1, 30, 0));
sliderPanel.setSize(500,500);



sliderPanel.add(hueLabel);
sliderPanel.add(hueSlider);
sliderPanel.add(hueValue);
sliderPanel.add(satLabel);
sliderPanel.add(satSlider);
sliderPanel.add(satValue);
sliderPanel.add(lightLabel);
sliderPanel.add(lightSlider);
sliderPanel.add(lightValue);

this.add(sliderPanel);
this.setVisible(true);
}

class SliderListener implements ChangeListener{
public void stateChanged(ChangeEvent e){
JSlider slider = (JSlider)e.getSource();

if(slider == hueSlider){
hueValue.setText("Selected Hue : " + slider.getValue()); //GUI 显示数值
val.setHue(slider.getValue());

}else if(slider == satSlider){

val.setSat(slider.getValue());
satValue.setText("Selected Saturation : " + val.getSat());
}else if(slider == lightSlider){
lightValue.setText("Selected Light : " + slider.getValue());
val.setLight(slider.getValue());
}

}
}

}

Value.java 我只是想通过这个类方便其他两个类传递数值。

public class Value {
/**
* this class is used for get HSL values.
*/
int hue = 0;
int sat = 0;
int light = 0;
String dataForm = "";

public int getHue(){
return hue;
}
public void setHue(int hue){
this.hue = hue;
}

public int getSat(){
return sat;
}
public void setSat(int sat){
this.sat = sat;
}

public int getLight(){
return light;
}
public void setLight(int light){
this.light = light;
}

/*public String toString(int i){
return i+"";
}*/
// build output data format.
public String formedData(int hue, int sat, int light){
String dataForm = "c";
dataForm += hue;
dataForm += "s";
dataForm += sat;
dataForm += "l";
dataForm += light;
dataForm += "e";
System.out.println(dataForm);

return dataForm; // c+hue+s+sat+l+light+e
}
}

...全文
1823 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
三仙半 2015-05-05
  • 打赏
  • 举报
回复
我QQ是597882752,有兴趣可以加一下,聊着方便
Elehum 2015-05-05
  • 打赏
  • 举报
回复
麻烦你了。我刚接触线程,好多都不是很懂
三仙半 2015-05-05
  • 打赏
  • 举报
回复
我看到的49行是提示中指的那个49行吧?
三仙半 2015-05-05
  • 打赏
  • 举报
回复
哦,是啊,如果serialPort 是null了,那么第二句代码就过不去了。

serialPort = (SerialPort)portName.open("COMM", 100);
serialPort.setSerialPortParams(115200, 8, 1, 0);
我再看看啊,怎么会空指针了呢。我机器没串口,调试不了,愁人。
Elehum 2015-05-05
  • 打赏
  • 举报
回复
不应该是serial port 的问题吧, 因为如果再开一个GUI启动串口会报错: PortInUse。 说明已经与COM4连接了。
三仙半 2015-05-05
  • 打赏
  • 举报
回复
看你的代码,第49行是
output = serialPort.getOutputStream();
错误提示是空指针,我分析,是不是serialPort获取失败啊?你debug一下看看

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧