0

I am trying to write a simple program to listen to clipboard copys and save them. I wrote the following code:

package CopyPaste;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args){
        JFrame frame = new JFrame("Copy Paste");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setLocationRelativeTo(null);

        MyPanel pane = new MyPanel();
        frame.add(pane);

        frame.setVisible(true);
    }
}

package CopyPaste;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.lang.Object;

import javax.swing.JButton;
import javax.swing.JPanel;

public class MyPanel extends JPanel implements ClipboardOwner{

    private final static Font f = new Font("david", Font.BOLD,22);
    private static TextArea text;
    private JButton btnGet;
    private Clipboard c;

    public MyPanel(){
        this.setLayout(new BorderLayout());

        text = new TextArea();
        text.setFont(f);
        add(BorderLayout.CENTER,text);
        add(BorderLayout.SOUTH,this.getSouthButton());

        c.addFlavorListener(new FlavorListener() {

            @Override
            public void flavorsChanged(FlavorEvent e) {
                proccessClipboard(c);

            }
        });
    }

    private static void proccessClipboard(Clipboard c){
        String s = null;
        c = Toolkit.getDefaultToolkit().getSystemClipboard();
        try {
            s = (String) c.getContents(null).getTransferData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException | IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        text.setText(s);
    }

    //return the south panel button
    private JPanel getSouthButton(){
        JPanel south = new JPanel();
        btnGet = new JButton("Get");
        ButtonLis lis = new ButtonLis();
        btnGet.addActionListener(lis);
        south.add(btnGet);
        return south;
    }

    private class ButtonLis implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            String s = null;
            if (e.getSource()==btnGet){
                c = Toolkit.getDefaultToolkit().getSystemClipboard();
                try {
                    s = (String) c.getContents(null).getTransferData(DataFlavor.stringFlavor);
                } catch (UnsupportedFlavorException | IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                text.setText(s);
            }


        }

    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        // TODO Auto-generated method stub

    }
}

error:
Exception in thread "main" java.lang.NullPointerException
    at CopyPaste.MyPanel.<init>(MyPanel.java:39)
    at CopyPaste.Main.main(Main.java:13)

for some reason the eclipse don't show any errors but when i hit the "run" button i get some compile error. someone know what can cause this?

Yuval Levy
  • 2,397
  • 10
  • 44
  • 73
  • 3
    Can you show us the text of the errors that you're seeing? And I'm not sure how you can be getting "compile errors" in code that appears to be compiling fine. – Hovercraft Full Of Eels Jun 15 '14 at 20:18
  • 2
    FYI: [`Panel`](http://docs.oracle.com/javase/7/docs/api/java/awt/Panel.html) is an existing Java class. – PM 77-1 Jun 15 '14 at 20:23
  • Chit, that's not a "compile error", it's a NullPointerException -- and there's a **huge** difference between the two! Find the line that throws the exception and figure out which variable on that line is null. – Hovercraft Full Of Eels Jun 15 '14 at 20:31

2 Answers2

1

This is an old post, but I am going to answer it for other people who search same thing. The right way is to initialize the Clipboard before define the FlavourListener. So "MyPanel" constructor has to be updated right this:

public MyPanel(){
        this.setLayout(new BorderLayout());

        text = new TextArea();
        text.setFont(f);
        add(BorderLayout.CENTER,text);
        add(BorderLayout.SOUTH,this.getSouthButton());

        c = Toolkit.getDefaultToolkit().getSystemClipboard();
        c.addFlavorListener(new FlavorListener() {

            @Override
            public void flavorsChanged(FlavorEvent e) {
                proccessClipboard(c);

            }
        });
    }
Nebuchanazer
  • 121
  • 1
  • 1
  • 11
0

Your error is occurring at line 39 of your Panel class as this line from your error message is telling you:

at CopyPaste.MyPanel.<init>(MyPanel.java:39)

Which is this line:

c.addFlavorListener(new FlavorListener() {

So, your clipboard variable, c, is null when you try to add a FlavorListener to it, and it must be initialized before you try to use it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373