Opening a file produces a java.lang.NullPointerException - java

I have a JFrame and on the frame I have JButton, what I want is when that file is clicked that the user can load a file using the java JFileChooser.
I declare the FileChooser like this.
JFileChooser fc;
And then here is my code in the action listener for the button.
JButton btnLoad = new JButton("Load .txt");
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(OpenFile.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("Opening: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user.");
}
}
});
The error that it is producing me is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at maple.Netflix$2.actionPerformed(Netflix.java:73)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
Here is line 73.
int returnVal = fc.showOpenDialog(Netflix.this);

Just declaring the JFileChooser variable is not enough, as you need to initialize the reference variable, fc, to a valid object first before using it. This is the same as for any other reference variable.
JFileChooser fc = new JFileChooser();

The value of fc is null. It needs to be set to that of an appropriate object before you call methods on it.

Related

NullPointerException when calling getSelectedItem() on an editable JComboBox

I have an editable combo box with a key listener. I want to write some text in it and with every letter, it has to make a string with the text I have inserted (or deleted).
But when I want to get the string I get a NullPointerException.
combobox.getEditor().getEditorComponent()
.addKeyListener(new KeyListener()
{
String value="";
#Override
public void keyTyped(KeyEvent e)
{
value = combobox.getSelectedItem().toString();
System.out.println(value);
}
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyPressed(KeyEvent e)
{
}
});
Stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at auftrag_paket.Neuer_Auftrag_ohneMwSt$25.keyTyped(Neuer_Auftrag_ohneMwSt.java:2037)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$400(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at auftrag_paket.Auftragsverwaltung$14.actionPerformed(Auftragsverwaltung.java:1865)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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$400(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
Has anyone any ideas?
Don't use a KeyListener that is old code when using AWT. Swing has newer and better API's.
Instead you should be using a DocumentListener. Read the section from the Swing tutorial on How to Write a Document Listener for more information.
To capture any character typed in combobox using KeyListener, you should do it inside keyReleased method, try the following code:
combobox.getEditor().getEditorComponent()
.addKeyListener(new KeyListener()
{
String value="";
#Override
public void keyTyped(KeyEvent e)
{
}
#Override
public void keyReleased(KeyEvent e)
{
value = combo.getSelectedItem().toString();
value = ((JTextField)combo.getEditor().getEditorComponent()).getText();
System.out.println(value);
}
#Override
public void keyPressed(KeyEvent e)
{
}
});

File copy access denied

Here I am trying to copy a file one location to another. I am having the source address as
F:\C#\Studies\OS\ch4.ppt
where I want to copy ch3.ppt to another location where user chooses .ie., fetching the destination address from the JFilechooser(Where ever the user wants to copy). Here I am getting access denied error
{
//Getting the value of fileLocationSourceDrive from table.
//fileLocationSourceDrive contains F:\C#\Studies\OS\ch4.ppt
JFileChooser location = new JFileChooser();
location.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
location.showSaveDialog(null);
String fileLocationDestination = location.getSelectedFile().getAbsolutePath().toString();
copyFile(fileLocationSourceDrive, fileLocationDestination);
}
public static void copyFile(String sourceFileName, String destionFileName) {
try {
System.out.println("Reading..." + sourceFileName);
File sourceFile = new File(sourceFileName);
File destinationFile = new File(destionFileName);
InputStream in = new FileInputStream(sourceFile);
JOptionPane.showMessageDialog(null, destinationFile);
OutputStream out = new FileOutputStream(destinationFile);
//OutputStream out = new FileOutputStream("C:\\Users\\Sagar Ch\\Documents\\New folder");
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("Copied: " + sourceFileName);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Here the error block(Getting access denied)
Reading...F:\C#\Studies\OS\ch4.ppt
java.io.FileNotFoundException: C:\Users\Sagar Ch\Documents\New folder (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at main.copyFile(main.java:1277)
at main$22.actionPerformed(main.java:1127)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
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$400(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
I believe that the problem is that you are trying to write the file (copy it, rather) onto the path of C:\Users\Sagar Ch\Documents\New folder, and that is not a "file", that's an existing folder. You should copy ch4.ppt to C:\Users\Sagar Ch\Documents\New folder\ch4.ppt.

How to choose file and then submit in Java swing?

The UI has two buttons, browse and submit. I want the user to hit browse to find a file, and then submit to copy it to a different location. However whenever I attempt it, I get this stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at me.trevor1134.modinjector.ModInjector$3.actionPerformed(ModInjector.java:154)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
"Submit" button actionPerformed :
#Override
public void actionPerformed(ActionEvent arg0) {
if (mod.exists()) { //line 154 mod is a file object
OS = System.getProperty("os.name").toLowerCase();
detectOS();
modLocation = new File(fullPath);
if (modLocation.exists() && modLocation.isDirectory()) {
Path newP = modLocation.toPath();
Path oldP = mod.toPath();
try {
Files.copy(oldP, newP);
} catch (IOException e) {
e.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "Mod successfully copied to: "
+ fullPath);
System.out.println("Mod successfully copied to: " + fullPath);
}
}
}
Browse button code:
#Override
public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser(homePath + "\\Downloads");
FileNameExtensionFilter filter = new FileNameExtensionFilter("ZIP & JAR Files",
"zip", "jar");
fc.setFileFilter(filter);
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File mod = fc.getSelectedFile();
textField.setText(mod.getAbsolutePath());
System.out.println("File: " + mod.getName());
} else {
System.out.println("Open command cancelled by user.");
}
System.out.println(returnVal);
}
I basically want the variable mod to carry on to the Submit area, but because of where it is set, I am unable to do so.
In your "browse" button code you are initializing a local variable mod like this:
final File mod = fc.getSelectedFile();
In the "Submit" button code you are using a class variable mod. Same name, different variables with different scopes.
Try changing:
final File mod = fc.getSelectedFile();
to
mod = fc.getSelectedFile();
with mod being a private class variable.
Also add null check in the "Submit" button code.

XSLT-Transformation with Saxon9he throws java.net.URISyntaxException: Illegal character in path at index

I'm trying an xsl transformation with saxon and I'm having trouble with the file names:
package com.fop;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class XMLtoFO {
private static StreamResult out;
private static StreamSource xml;
private static StreamSource xsl;
private static Transformer transformer;
// TODO
protected static void xmlToFO(String inputXSL, String[] inputxml,
String[] fofilname) throws Exception {
if (true) {
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
}
for (int i = 0; i < inputxml.length; i++) {
try {
out = new StreamResult( fofilname[i] );
xml = new StreamSource( inputxml[i] );
xsl = new StreamSource( inputXSL );
transformer = TransformerFactory.newInstance().newTransformer( xsl );
transformer.transform( xml, out );
System.err.println("\n--------------------------------------------------------------------------------------------\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Error on line 12 of root-template.xsl:
java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung,Methode 2_result.fo.xml
; SystemID: file:/C:/Users/z003a5bp/Desktop/root-template.xsl; Line#: 12; Column#: -1
net.sf.saxon.trans.XPathException: java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung, Methode 2_result.fo.xml
at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:200)
at net.sf.saxon.serialize.Emitter.makeWriter(Emitter.java:160)
at net.sf.saxon.serialize.XMLEmitter.openDocument(XMLEmitter.java:145)
at net.sf.saxon.serialize.XMLEmitter.startElement(XMLEmitter.java:309)
at net.sf.saxon.event.NamespaceReducer.startElement(NamespaceReducer.java:73)
at net.sf.saxon.event.ComplexContentOutputter.startContent(ComplexContentOutputter.java:558)
at net.sf.saxon.event.ComplexContentOutputter.startElement(ComplexContentOutputter.java:183)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:424)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
at net.sf.saxon.expr.instruct.Block.processLeavingTail(Block.java:660)
at net.sf.saxon.expr.instruct.Instruction.process(Instruction.java:138)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:431)
at net.sf.saxon.expr.instruct.ElementCreator.processLeavingTail(ElementCreator.java:373)
at net.sf.saxon.expr.instruct.Template.applyLeavingTail(Template.java:239)
at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:1057)
at net.sf.saxon.Controller.transformDocument(Controller.java:2080)
at net.sf.saxon.Controller.transform(Controller.java:1903)
at com.fop.XMLtoFO.xmlToFO(XMLtoFO.java:29)
at com.fop.Layout.output(Layout.java:151)
at com.fop.Layout.actionPerformed(Layout.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
Caused by: java.net.URISyntaxException: Illegal character in path at index 19: 3-Leiter-Steuerung, Methode 2_result.fo.xml
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at net.sf.saxon.serialize.Emitter.makeOutputStream(Emitter.java:172)
... 55 more
The problem is that many filenames have spaces or commas or things like that. What would be the best way to handle that? Have all input files pass through a character-escaping method?
I was looking at this and guess my problem is something similar:
http://sourceforge.net/p/saxon/discussion/94027/thread/b43bb749/
But that discussion is a little over my head.
UPDATE: I changed the constructors accordingly. Now I get this:
Error
I/O error reported by XML parser processing
file:/C:/Users/z003a5bp/Desktop/Workspace/FOP/file:C:/Users/z003a5bp/Desktop/3-Leiter-Steuerung,%20Methode%202.xml: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
net.sf.saxon.trans.XPathException: I/O error reported by XML parser processing file:/C:/Users/z003a5bp/Desktop/Workspace/FOP/file:C:/Users/z003a5bp/Desktop/3-Leiter-Steuerung,%20Methode%202.xml: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:427)
at net.sf.saxon.event.Sender.send(Sender.java:169)
at net.sf.saxon.Controller.transform(Controller.java:1890)
at com.fop.XMLtoFO.xmlToFO(XMLtoFO.java:29)
at com.fop.Layout.output(Layout.java:151)
at com.fop.Layout.actionPerformed(Layout.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
Caused by: java.io.FileNotFoundException: C:\Users\z003a5bp\Desktop\Workspace\FOP\file:C:\Users\z003a5bp\Desktop\3-Leiter-Steuerung, Methode 2.xml (the filename, directory name or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:396)
... 41 more
This is how I read the files:
protected static void openXMLChooser() {
setUIManager();
FileFilter filter = new FileNameExtensionFilter("XML files", "xml");
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.addChoosableFileFilter(filter);
chooser.showOpenDialog(null);
if (chooser.getSelectedFile() != null
&& chooser.getSelectedFiles() != null)
infoarea.append("No file(s)\n");
}
And this is how I change the filename:
protected String[] fileName(String exten) {
file = chooser.getSelectedFiles();
pdffilename = new String[file .length];
for (int i = 0; i < file .length; i++) {
pdffilename[i] = file [i].getName();
b = new StringBuilder(pdffilename[i]);
b.replace(pdffilename[i].lastIndexOf("."),
pdffilename[i].lastIndexOf("l") + 1, "_result"+exten+"");
pdffilename[i] = b.toString();
}
return pdffilename;
}
And here I call the function:
XMLtoFO.xmlToFO(inputxsl, inputxml, this.fileName("fo.xml"));
Filenames are not URIs; you are using a filename where a URI is expected.
The simplest answer is to use the constructors for StreamSource that expect a File rather than a URI. So change
new StreamSource(X)
to
new StreamSource(new File(X))
because the "new File()" constructor works with file names. (Internally, the file name will be automatically converted to a URI, but you don't need to worry about that).

NullPointerException when try to capture imagem from webcam

I got an error in my line 508 where I have this code
buf = fgc.grabFrame();
CODE:
startC.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Grab a frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
BufferToImage btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
//imgpanel.setImage(img);
// save image
try {
saveJPG(img,"c:\\test.jpg");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}):
STACK:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at com.colorfulwolf.webcamapplet.WebcamApplet$6.actionPerformed(WebcamApplet.java:508)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
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.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)
What's going wrong with this line ?
You need to assign player to an instance:
public static Player player = new Player();

Categories