我有一个java类(Main.java),它有一个从DTMF串行输入的字符串。 字符串是InputLine在以下类中声明: - Main.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.File;
import java.io.FileInputStream;
import java.lang.String;
import java.util.Enumeration;
import javax.swing.JOptionPane;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Main implements SerialPortEventListener
{
SerialPort serialPort;
private static final String PORT_NAMES[] = {
"COM30", // Windows
};
public BufferedReader input;
public OutputStream output;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public static String inputLine;
static String complete = "";
static boolean eng=false;
static boolean hin=false;
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
inputLine=input.readLine();
System.out.println(inputLine);
complete= complete + inputLine;
int last= Integer.parseInt(complete);
int converted= last%10;
if (last<10)
{
if (converted==1)
{
BGMusicmaine();
eng= true;
}
else if (converted==1)
{
BGMusicmainh();
hin=true;
}
else
BGMusicmain9();
}
else if (last>10) {
if (eng==true)
{
if (converted==1)
{
BGMusicmainh();
}
else
BGMusicmain9();
}
}
}
catch (Exception e) {
// System.err.println(e.toString());
}
}
}
public void BGMusicmain9() { //Plays the background music
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"invalid.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmaine() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"hmenu2.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmainh() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"hmenu1.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public static void main(String[] args) throws Exception {
Main main = new Main();
main.initialize();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}
当我尝试在另一个java类中调用此inputLine时,它会给我Nullpointer异常。 我在函数void操作中调用它。 附加快餐店http://oi50.tinypic.com/6pb9qc.jpg IVRSNew.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JOptionPane;
import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import java.io.IOException;
public final class IVRSNew extends javax.swing.JFrame {
public IVRSNew() {
initComponents();
//BGMusic();
operation();
BGMusiceng();
}
public void BGMusic() { //Plays the background music
//make a new AudioPlayer.
/*
Thread.sleep(1000);
*/
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"alarma.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error)
{JOptionPane.showMessageDialog(null, "Invalid file!");}
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusiceng() { //Plays the background music
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
Thread.sleep(1000);
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"startingpoint.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error)
{JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Disconnect");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jButton1KeyPressed(evt);
}
});
jButton2.setText("Hold");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Call Summary");
jButton4.setText("1");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton14.setText("0");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
jButton15.setText("#");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.addComponent(jButton13, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
.addComponent(jButton14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
.addGap(7, 7, 7)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8)
.addComponent(jButton9))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11)
.addComponent(jButton12))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton14)
.addComponent(jButton15)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2))
.addContainerGap(66, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//BGMusicmaindc();
System.exit(WIDTH);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusic();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain1();
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain2();
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain3();
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain4();
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain5();
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
BGMusicmain6();
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
public void operation()
{
try{
System.out.println(Main.inputLine);
if (Main.inputLine.equals(""));
{
BGMusicmain();
}
}
catch (Exception e)
{
System.out.println(e);
}}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IVRSNew().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel1;
// End of variables declaration
public void BGMusicmain() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"welcome.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain1() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"hmenu2.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain2() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"hmenu1.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain3() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"invalid.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain8() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"invalid.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain9() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"invalid.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
// play background music.
mynewBackgroundPlayer.start(myLooop);
}
public void BGMusicmain0() { //Plays the background music
//make a new AudioPlayer.
AudioPlayer mynewBackgroundPlayer = AudioPlayer.player;
AudioDataStream myLooop = null;
//use a try block in case the file doesn't exist.
try {
AudioStream mynewBackgroundMusic = new AudioStream(new FileInputStream(new File(getClass().getResource(
"welcome.wav").toURI())));
AudioData mynewData = mynewBackgroundMusic.getData();
myLooop = new AudioDataStream(mynewData);
}
catch(Exception error) {JOptionPane.showMessageDialog(null, "Invalid file!");}
mynewBackgroundPlayer.start(myLooop);
}
}