Dlaczego to nie działa?

0

Witam! Mam taki kod:

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Window.Type;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main implements MouseMotionListener, MouseListener{
        JFrame f; JLabel l; static JFileChooser jf; static String trueExtension;
    public static void main(String[] args) {
        new Main();
    }
    public Main() {
        f = new JFrame();
        f.setBounds(0,0,5,5);
        f.setLayout(null);
        f.setAlwaysOnTop(true);
        f.addMouseListener(this);
        f.addMouseMotionListener(this);
        f.setUndecorated(true);
        f.setBackground(new Color(1.0f,1.0f,1.0f,0.1f));
        f.setType(Type.UTILITY);
        l = new JLabel();
        l.setLayout(null);
        l.addMouseListener(this);
        l.setLayout(null);
        l.setBounds(0,0,30,30);
        f.add(l);
        f.setVisible(true);
    }   
    public static void setLanguage() {
        UIManager.put("FileChooser.lookInLabelText","Szukaj w");
        UIManager.put("FileChooser.lookInLabelMnemonic",""+KeyEvent.VK_W);
        UIManager.put("FileChooser.saveInLabelText","Zapisz w");
        UIManager.put("FileChooser.saveInLabelMnemonic",""+KeyEvent.VK_W);
        UIManager.put("FileChooser.fileNameLabelText","Nazwa pliku:");
        UIManager.put("FileChooser.fileNameLabelMnemonic",""+KeyEvent.VK_N);
        UIManager.put("FileChooser.folderNameLabelText","Nazwa katalogu:");
        UIManager.put("FileChooser.folderNameLabelMnemonic",""+KeyEvent.VK_N);      
        UIManager.put("FileChooser.filesOfTypeLabelText","Pliki typu:");
        UIManager.put("FileChooser.filesOfTypeLabelMnemonic",""+KeyEvent.VK_P);
        UIManager.put("FileChooser.upFolderToolTipText","Poziom wyżej");
        UIManager.put("FileChooser.homeFolderToolTipText","Pulpit");
        UIManager.put("FileChooser.newFolderToolTipText","Nowy katalog");
        UIManager.put("FileChooser.listViewButtonToolTipText","Lista");
        UIManager.put("FileChooser.detailsViewButtonToolTipText","Szczegóły");
        UIManager.put("FileChooser.fileNameHeaderText","Nazwa");UIManager.put("FileChooser.fileSizeHeaderText","Rozmiar");
        UIManager.put("FileChooser.fileTypeHeaderText","Typ");
        UIManager.put("FileChooser.fileDateHeaderText","Modyfikacja");
        UIManager.put("FileChooser.fileAttrHeaderText","Atrybuty");
        UIManager.put("FileChooser.newFolderErrorText","Blad podczas tworzenia katalogu");
        UIManager.put("FileChooser.saveButtonText","Zapisz");
        UIManager.put("FileChooser.saveButtonMnemonic",""+KeyEvent.VK_Z);
        UIManager.put("FileChooser.openButtonText","Otwórz");
        UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_O);
        UIManager.put("FileChooser.cancelButtonText","Anuluj");
        UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_R);
        UIManager.put("FileChooser.openDialogTitleText","Otwórz");
        UIManager.put("FileChooser.saveDialogTitleText","Zapisywanie");
        UIManager.put("FileChooser.saveButtonToolTipText","Zapisz");
        UIManager.put("FileChooser.openButtonToolTipText","Otwórz");
        UIManager.put("FileChooser.cancelButtonToolTipText","Anuluj");
        UIManager.put("FileChooser.acceptAllFileFilterText","Wszystkie pliki");
        }   

    public static String getExtension() {
        String ext = null;

        if(jf.getFileFilter().getDescription().equals("Obraz *.png"))
            ext="png";
        else if(jf.getFileFilter().getDescription().equals("Obraz *.jpg"))  
            ext="jpg";
        else if(jf.getFileFilter().getDescription().equals("Obraz *.jpeg"))
            ext="jpeg";
        else if(jf.getFileFilter().getDescription().equals("Obraz *.bmp"))
            ext="bmp";

        return ext;
    }

    public static File saveFileDialog() {
        File xyz = null;
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e) {
            e.printStackTrace();
        }
        setLanguage();
        JFrame frame = new JFrame();
        jf.setDialogTitle("Zapisz zrzut ekranu");
        jf.setAcceptAllFileFilterUsed(false);
        jf.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
        jf.setFileFilter(new FileNameExtensionFilter("Obraz *.png","png"));
        jf.setFileFilter(new FileNameExtensionFilter("Obraz *.jpg","jpg"));
        jf.setFileFilter(new FileNameExtensionFilter("Obraz *.jpeg","jpeg"));
        jf.setFileFilter(new FileNameExtensionFilter("Obraz *.bmp","bmp"));

           System.out.println(trueExtension+"savefiledialog");
        jf.setCurrentDirectory(new File(System.getProperty("user.home")+"\\desktop"));
        if (jf.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION){
            System.out.println(trueExtension+"savefiledialog OKKKKKK after");
            trueExtension = getExtension();
            System.out.println(trueExtension+"savefiledialog OKKKKKK before");
            xyz = jf.getSelectedFile();
        }

        return checkFileName(xyz);
    }       
    public static File checkFileName(File file) {
        Pattern pat = Pattern.compile(".*/.png");
        Matcher mat = pat.matcher(file.getName());

           System.out.println(trueExtension+"checkfilename");

        if(mat.matches()) {
            return file;
        }
        else{
            File f = new File(file.getAbsolutePath()+".png");
            return f;
        }
    }

    public static void saveScreenShot() throws AWTException, IOException{
           System.out.println(trueExtension+"savescreenshot");
           Robot robot = new Robot();
           BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
           ImageIO.write(image,getExtension(), saveFileDialog());
        }
    @Override
    public void mouseClicked(MouseEvent e) {
        try {
            saveScreenShot();
            System.out.println(trueExtension+"mouseclicked");
        } catch (AWTException | IOException | NullPointerException a) {
            JOptionPane.showMessageDialog(null, "Nie udało się zrobić screenshot'a!", "Błąd!", JOptionPane.ERROR_MESSAGE);
            a.printStackTrace();
        }
        catch(IllegalArgumentException o) {}
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        f.setBounds(0,0,30,30);
    }
    @Override
    public void mouseExited(MouseEvent e) {
        f.setBounds(0,0,5,5);
    }
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}
    public void mouseDragged(MouseEvent arg0) {}
    public void mouseMoved(MouseEvent arg0) {}
}

Iiiii... Chodzi o to, że chcę, aby gdy użytkownik zatwierdzi, pobrać wybrany filtr, żeby wiedzieć jakie rozszerzenie potrzebuję. Wywala mi takie błędy:

nullsavescreenshot
java.lang.NullPointerException
    at Main.getExtension(Main.java:85)
    at Main.saveScreenShot(Main.java:145)
    at Main.mouseClicked(Main.java:150)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Wiem jedynie, że chodzi o to, że po prostu tego okna już nie ma. To kiedy mam pobierać te filtry?

Z tego można zobaczyć, że dochodzi jedynie do savescreenshot i dalej nic ;-;. Próbowałem na samym początku nadać zmiennej trueExtension wartość ("null", a potem "png") ale to nie pomogło... Co mam zrobić?

0

Nigdzie w kodzie nie widzę żadnego jf = new JFileChooser() więc jf jest zawsze nullem i jakiekolwiek odwołanie do niego wywali NPE.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0