NullPointerException:
[code]package testeserial;
import javax.comm.;
import java.io.;
import java.awt.TextArea;
import java.awt.event.*;
import java.util.TooManyListenersException;
/**
* A class to handle <code>KeyEvent</code>s generated by the messageAreaOut.
* When a <code>KeyEvent</code> occurs the <code>char</code> that is
* generated by the event is read, converted to an <code>int</code> and
* writen to the <code>OutputStream</code> for the port.
*/
class KeyHandler extends KeyAdapter {
private static KeyHandler INSTANCE;
private static Entrada_Saida es;
/**
* Creates the KeyHandler.
* @param os The OutputStream for the port.
*/
private KeyHandler() {}
public void setES(Entrada_Saida es) {
this.getInstance().es = es.getInstance();
}
public Entrada_Saida getES() {
return this.getInstance().es;
}
public void setValor(int valor) {
String strValor = Integer.toString(valor);
byte[] bytes = strValor.getBytes();
try {
this.getInstance().getES().getInstance().getOS().write(bytes); // NESTA LINHA!
} catch (IOException e) {}
}
public void keyTyped(KeyEvent evt) {
char newCharacter = evt.getKeyChar();
if (es == null) {
es = es.getInstance();
}
try {
es.getInstance().getOS().write((int) newCharacter);
} catch (IOException e) {
System.err.println("OutputStream write error: " + e);
}
}
public static KeyHandler getInstance() {
if (INSTANCE == null) {
INSTANCE = new KeyHandler();
}
return INSTANCE;
}
}
[/code]
[color=“darkblue”]
O problema é que o valor de OS é considerado null, não sei o motivo…
[/color]
/*
* Entrada_Saida.java
*
* Created on 31 de Maio de 2007, 17:55
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testeserial;
import javax.comm.*;
import java.io.*;
import java.awt.TextArea;
import java.awt.event.*;
import java.util.TooManyListenersException;
public class Entrada_Saida {
private static Entrada_Saida INSTANCE;
private OutputStream os;
private InputStream is;
/** Creates a new instance of Entrada_Saida */
private Entrada_Saida() {}
public void setOS(OutputStream os) {
this.getInstance().os = os;
}
public OutputStream getOS() {
return this.getInstance().os;
}
public void setIS(InputStream is) {
this.getInstance().is = is;
}
public InputStream getIS() {
return is;
}
public static Entrada_Saida getInstance() {
if (INSTANCE == null) {
INSTANCE = new Entrada_Saida();
}
return INSTANCE;
}
}
muito obrigada,
Iúna.
[color=“red”]*Editado: use BBCode em suas mensagens no fórum. Jair Elton. :joia:[/color]