I´m trying to build an application which can read information from Excel files and place the data into a document. The document is a form of a template with columns in. Everything works just fine until the saving part.
It work, almost, like it should when I run the program directly in IntelliJ. However, when I install the application to an runnable JAR with Maven - the JAR won't work.
The file is saved as desired... But the new file contains nothing if I run the application from JAR. When i run directly in IntelliJ, the new file is been created and opened, but of 3 columns / row, only 2 has data in it.
What can I do?
Link to template document
http://www.labelmedia.de/englisch/doc/70%20x%2032%20mm%20-%20Art.%2088%2010%2027%2070%2032.doc
Thank you in advanced
package utils;
import model.Customers;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class WriteToDocument {
String TARGET_FILE = "src\\main\\java\\utils\\template\\template.doc";
private int postInList = 0;
public WriteToDocument() {}
public WriteToDocument(ArrayList<Customers> list) throws IOException {
list.remove(0);
HWPFDocument doc = null;
try {
doc = openDocument(TARGET_FILE);
Range range = doc.getRange();
TableIterator itr = new TableIterator(range);
while (itr.hasNext()) {
Table table = itr.next();
for (int rowIndex = 0; rowIndex < table.numRows(); rowIndex++) {
TableRow row = table.getRow(rowIndex);
for (int colIndex = 0; colIndex < row.numCells(); colIndex++) {
TableCell cell = row.getCell(colIndex);
//WRITE IN TABLE //
if (postInList < list.size()) {
cell.getParagraph(0).replaceText(list.get(postInList).getName() + "\n\r" +
"\n\r" + list.get(postInList).getAddress() + "\n\r" +
list.get(postInList).getPostcode() + " " + list.get(postInList).getCity(), false);
postInList++;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
saveDocument(doc);
}
}
private HWPFDocument openDocument(String file) throws Exception {
System.out.println("OPEN");
return new HWPFDocument(new POIFSFileSystem(new FileInputStream(file)));
}
private static void saveDocument(HWPFDocument doc) throws IOException {
System.out.println("SAVE");
try (FileOutputStream out = new FileOutputStream(new File("test.doc"))) {
doc.write(out);
out.flush();
System.out.println("File saved");
doc.close();
out.close();
Desktop dt = Desktop.getDesktop();
dt.open(new File("test.doc"));
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
Stacktrace
java.io.FileNotFoundException: src\main\java\utils\template\template.doc (Det går inte att hitta sökvägen)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at utils.WriteToDocument.openDocument(WriteToDocument.java:56)
at utils.WriteToDocument.<init>(WriteToDocument.java:25)
at utils.ReadExcel.writeToDocument(ReadExcel.java:64)
at utils.ReadExcel.<init>(ReadExcel.java:57)
at MainFrameController$1.actionPerformed(MainFrameController.java:31)
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$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)
SAVE
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at utils.WriteToDocument.saveDocument(WriteToDocument.java:62)
at utils.WriteToDocument.<init>(WriteToDocument.java:49)
at utils.ReadExcel.writeToDocument(ReadExcel.java:64)
at utils.ReadExcel.<init>(ReadExcel.java:57)
at MainFrameController$1.actionPerformed(MainFrameController.java:31)
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$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)
Most probably you are missing libraries when it is exported as a JAR. You are using Apache POI as external libraries. Inside the IDE all the libraries were applied but when exported, they seems to be missing. Just run the jar file from command prompt to be 100% clear about the issue. In IDEs like NetBeans the libraries are exported into a separate folder called lib have a look for similar related to your IDE as well.
I'm writing java code for a game and when creating the health bars I saw an exception that confused me greatly. The code and stack is below:
Code:
package com.teamanubiz.pixelhero;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.teamanubiz.gameapi.gfx.Sprite;
public class GUIRenderLayer {
public void renderStatBar(GUIPosition pos, Graphics g, int health, int maxHealth, int mana, int maxMana) {
Sprite healthBar = null;
try {
healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
healthBar.crop(0, 0, 128, 32);
healthBar.scale(256, 32);
Sprite manaBar = null;
try {
manaBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
manaBar.crop(0, 32, 128, 32);
manaBar.scale(265, 16);
Sprite temp = null;
try {
temp = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
temp.crop(0, 64, 128, 32);
temp.scale(256, 32);
Sprite emptyHealth = new Sprite(temp.getCurrent());
temp.scale(256, 16);
Sprite emptyMana = new Sprite(temp.getCurrent());
if (pos == GUIPosition.BOTTOM) {
double percent_h = health / maxHealth;
double percent_m = mana / maxMana;
healthBar.crop(0, 0, (int) ((int) 256 * percent_h), 32);
manaBar.crop(0, 0, (int) ((int) 256 * percent_m), 16);
g.drawImage(emptyMana.getCurrent(), 100, 464, null);
g.drawImage(emptyHealth.getCurrent(), 100, 432, null);
g.drawImage(healthBar.getCurrent(), 100, 432, null);
g.drawImage(manaBar.getCurrent(), 100, 464, null);
}
}
}
This class is referencing a custom library containing the class Sprite. It for some reason says that I am trying to cast a ToolkitImage to a BufferedImage in the below method of Sprite.java.
public void crop(int xOffset, int yOffset, int width, int height) {
BufferedImage temp = (BufferedImage) source;
temp = temp.getSubimage(xOffset, yOffset, width, height);
source = temp;
}
The variable source is an instance of an Image that is a field in Sprite.java
The stack below claims I am creating a ToolkitImage despite the fact new ImageIcon("res\\gui\\bar.png").getImage() returns only an Image. I do not convert the Image to a ToolkitImage ever in the code. This makes it extremely confusing.
Stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.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)
BufferedImage is a type of Image, but Image is not a type BufferedImage, you could, for example, cast BufferedImage to an Image.
Instead of using ImageIcon to load the image, use ImageIO.read, which returns a BufferedImage
So instead of...
Sprite healthBar = new Sprite((Image)new ImageIcon("res\\gui\\bar.png").getImage()); // I Never Instatiated a ToolkitImage!!!!!!!!
Nb: ImageIcon delegates the actual loading of the image to Toolkit and wraps it in a Icon interface
Use something like...
Sprite healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
No you don't "convert" the Image to a ToolKitImage, but that's what the JVM is giving you when you call new ImageIcon("res\\gui\\bar.png").getImage(), and not a BufferedImage. The ImageIcon API states that getImage() returns an object of Image type, but does not specify what type of Image. Since Image is an interface, you know that there must be some concrete type underlying the returned Image object. If you need a BufferedImage you could one, and draw this Image into it.
Myself, I wouldn't even use an ImageIcon here if I wanted a BufferedImage, but rather would use ImageIO.read(...) which returns a BufferedImage.
So....
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("res/gui/bar.png"));
Sprite healthBar = new Sprite(img);
My below code works fine on running from NetBeans IDE
public Selector() {
try {
if (count == 0) {
initComponents();
count++;
ims.CPool.configureConnPool();
fillTable();
setResizable(false);
Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(new Double((Size.getWidth() / 2) - (getWidth() / 2)).intValue(), new Double((Size.getHeight() / 2) - (getHeight() / 2)).intValue());
jTable1.addMouseListener(this);
img = ImageIO.read(Selector.class.getResource("/ims/Icons/login.png"));
sci = img.getScaledInstance(jLabel3.getWidth(), jLabel3.getHeight(), java.awt.Image.SCALE_SMOOTH);
ImageIcon newIconImage = new javax.swing.ImageIcon(sci);
jLabel3.setIcon(newIconImage);
} else {
throw new myExc("Only one instance of the application is allowed to run at a time !");
}
} catch (HeadlessException | IOException | myExc x) {
x.printStackTrace();
JOptionPane.showMessageDialog(null,x.getMessage());
}
}
Whenever i try to launch the class having the above constructor i get an exception shown below
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input
== null!
at javax.imageio.ImageIO.read(Unknown Source)
at ims.init.Selector.<init>(Selector.java:60)
at ims.init.Selector$8.run(Selector.java:502)
at java.awt.event.InvocationEvent.dispatch(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 Sour
ce)
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)
pls help
Check what's returned by the getResource() call
img = ImageIO.read(Selector.class.getResource("/ims/Icons/login.png"));
Could be that path is not correct "ims" but could be "imgs"
I'm trying to remove the selection at my JList, after an item has been clicked, because it then can not be clicked again without moving the selection.
I'vet tried this, but keep getting an error, and i don't see what is going on.
ListSelectionModel mode = new DefaultListSelectionModel();
mode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mode.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent evt)
{
// Åben url'en i en browser
if( !evt.getValueIsAdjusting() )
{
if(isDownloaded(data_alle.get(list_alle.getSelectedIndex()),data_downloaded))
{
try {
Desktop.getDesktop().open(new File(data_downloaded.get(list_alle.getSelectedIndex()).getPath()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
OpenURI.open(data_alle.get(list_alle.getSelectedIndex()).getUrl(), url);
}
list_alle.clearSelection();
}
}
});
list_alle.setSelectionModel(mode);
And the error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at gui.Kursus$2.valueChanged(Kursus.java:114)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(Unknown Source)
at javax.swing.DefaultListSelectionModel.clearSelection(Unknown Source)
at javax.swing.JList.clearSelection(Unknown Source)
at gui.Kursus$2.valueChanged(Kursus.java:127)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(Unknown Source)
at javax.swing.JList.setValueIsAdjusting(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)
In your selection handler you are not making any out of bounds check for the selected item
if( !evt.getValueIsAdjusting() && list_alle.getSelectedIndex() >= 0) {...}
What's happening is
The selection changes and enters you handlers code, you process the change and clear the selection
Your handler is notified of the change to the selection (again), you process the change and hit an ArrayOutOfBoundsException because nothing's selected
Since relevant code / line #s are missing from your question, my best guess is as follows:
I had a similar problem with selection manipulation for JList. When you are determining the index of what is being selected, the reported index from getMinSelectionIndex() and related calls will sometimes report 0 as negative numbers (may have something to do with deltas, but I am admitted unsure). Place a manual check to ensure that the index you are working with isn't negative:
89 class SharedListSelectionHandler implements ListSelectionListener {
90 public void valueChanged(ListSelectionEvent e) {
91 ListSelectionModel lsm = (ListSelectionModel)e.getSource();
92
93 if (!lsm.getValueIsAdjusting()){
94 int index = lsm.getMinSelectionIndex();
95 if (index < 0) {
96 index = 0;
97 }
98 handleListSelectionEvent(index);
99 }
100
101 }
102 }
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();