Pessoal, estou tentando fazer um KeyEvent, para cada vez que ele aperta ENTER, ele so imprimir uma linha no meu programa, mas ele fica dando esse erro: NULLPOINTEREXCEPTION
Aqui o codigo:
public class Janela extends javax.swing.JFrame {
KeyEvent ev;
public Janela() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
areaTexto = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
menuArquivo = new javax.swing.JMenu();
itemSalvar = new javax.swing.JMenuItem();
itemAbrir = new javax.swing.JMenuItem();
menuEditar = new javax.swing.JMenu();
itemFundo = new javax.swing.JMenuItem();
itemCor = new javax.swing.JMenuItem();
itemFonte = new javax.swing.JMenuItem();
itemFudeu = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("NotePad - -");
areaTexto.setColumns(20);
areaTexto.setRows(5);
jScrollPane2.setViewportView(areaTexto);
menuArquivo.setText("Arquivo");
itemSalvar.setText("Salvar");
itemSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemSalvarActionPerformed(evt);
}
});
menuArquivo.add(itemSalvar);
itemAbrir.setText("Abrir");
itemAbrir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemAbrirActionPerformed(evt);
}
});
menuArquivo.add(itemAbrir);
jMenuBar1.add(menuArquivo);
menuEditar.setText("Editar");
itemFundo.setText("Fundo");
itemFundo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemFundoActionPerformed(evt);
}
});
menuEditar.add(itemFundo);
itemCor.setText("Cor");
itemCor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemCorActionPerformed(evt);
}
});
menuEditar.add(itemCor);
itemFonte.setText("Fonte");
itemFonte.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemFonteActionPerformed(evt);
}
});
menuEditar.add(itemFonte);
itemFudeu.setText("HackerTurbo");
itemFudeu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
itemFudeuActionPerformed(evt);
}
});
menuEditar.add(itemFudeu);
jMenuBar1.add(menuEditar);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void itemSalvarActionPerformed(java.awt.event.ActionEvent evt) {
salvarTexto();
}
private void itemCorActionPerformed(java.awt.event.ActionEvent evt) {
editCor();
}
private void itemAbrirActionPerformed(java.awt.event.ActionEvent evt) {
abrirTexto();
}
private void itemFundoActionPerformed(java.awt.event.ActionEvent evt) {
editFundo();
}
private void itemFonteActionPerformed(java.awt.event.ActionEvent evt) {
editFonte();
}
private void itemFudeuActionPerformed(java.awt.event.ActionEvent evt) {
AcionarEvento(ev);
}
public void AcionarEvento(KeyEvent ev){
System.out.println("Selecionado");
JFileChooser diretorio = new JFileChooser();
try{
int r = diretorio.showSaveDialog(null);
if(r==JFileChooser.APPROVE_OPTION){
String x="",y="";
File file = new File(diretorio.getSelectedFile().getAbsolutePath());
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
y = br.readLine();
while((x = br.readLine()) !=null){
if (ev.getKeyCode()==ev.VK_ENTER){
y = y+"\n"+x;
areaTexto.setText(y);
}
}}}
catch(Exception a){
JOptionPane.showMessageDialog(null, a);
}
}
public void salvarTexto(){
try{
JFileChooser diretorio = new JFileChooser();
int r = diretorio.showSaveDialog(null);
if (r==JFileChooser.APPROVE_OPTION){
File file = new File(diretorio.getSelectedFile().getAbsolutePath());
FileWriter fw = new FileWriter (file+".txt",false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(areaTexto.getText());
bw.flush();
bw.close();
}
else{
JOptionPane.showMessageDialog(null,"Operação cancelada");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void abrirTexto(){
JFileChooser diretorio;
try{
diretorio = new JFileChooser();
int r = diretorio.showSaveDialog(null);
if(r==JFileChooser.APPROVE_OPTION){
String x="",y="";
File file = new File(diretorio.getSelectedFile().getAbsolutePath());
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
y = br.readLine();
while((x=br.readLine())!=null){
y = y +"\n"+x;
}
areaTexto.setText(y);
}else{
JOptionPane.showMessageDialog(null,"Operação cancelada");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void editFundo(){
JColorChooser editCor = new JColorChooser();
Color color = JColorChooser.showDialog(null,"Selecione uma cor",areaTexto.getBackground());
areaTexto.setBackground(color);
}
public void editCor(){
JColorChooser editCor = new JColorChooser();
Color color = JColorChooser.showDialog(null,"Selecione uma cor",areaTexto.getForeground());
areaTexto.setForeground(color);
}
public void editFonte(){
JComboBox combo = new JComboBox();
JComboBox combo2 = new JComboBox();
String [] font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (String fonte : font){
combo.addItem(fonte);
}
for (int i = 8;i<=72;i+=2){
combo2.addItem(i);
}
JOptionPane.showMessageDialog(null,combo,"Selecione a fonte",JOptionPane.DEFAULT_OPTION);
JOptionPane.showMessageDialog(null,combo2,"Selecione o tamanho",JOptionPane.DEFAULT_OPTION);
String fonteSelecionada = (String)combo.getSelectedItem();
int tamanhoSelecionado = (int) combo2.getSelectedItem();
areaTexto.setFont(new Font(fonteSelecionada,Font.CENTER_BASELINE,tamanhoSelecionado));
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Janela.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Janela().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JTextArea areaTexto;
private javax.swing.JMenuItem itemAbrir;
private javax.swing.JMenuItem itemCor;
private javax.swing.JMenuItem itemFonte;
private javax.swing.JMenuItem itemFudeu;
private javax.swing.JMenuItem itemFundo;
private javax.swing.JMenuItem itemSalvar;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JMenu menuArquivo;
private javax.swing.JMenu menuEditar;
// End of variables declaration
}