Say for example, if in a program you get a null-pointer error for adding a piece of code to your which program makes the program run fine but without that piece of code the program doesn't work as expected, would it be a good idea to allow the null-pointer error to happen, if so, is there any way of catching it before it displays onto the console. 1 way I am aware of is, using try and catch but in my past experience this hasn't worked, my attempt at using this might be wrong, but this is how I tried it.
try {
// line / s of code to catch the error from
} catch (java.lang.NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
edited: The list of error i am getting:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at playAGame.endGameResult(playAGame.java:204)
at playAGame.checkingWinner(playAGame.java:159)
at playAGame.callCheckWinner(playAGame.java:179)
at playAGame.moveSetup(playAGame.java:66)
at playAGame$1.actionPerformed(playAGame.java:52)
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)
this is because of this line: button = new JButton[button.length][button.length];
I am creating a TicTacToe game and if I remove that line from my code, the game won't work properly.
edited: I believe this is one of the lines providing the null pointer, correct me if I am wrong. Basically, I have created a method checking if three given buttons have the value X, if it does then trigger the win variable to true. This is how I am checking if someone has won the TicTacToe game.
public void checkingWinner(JButton button1, JButton button2, JButton button3) {
if(buttonA.getText().equals(buttonB.getText()) && buttonB.getText().equals(buttonC.getText()) && buttonA.getText().equals("X"))
{
win = true;
System.out.pl("winner is X");
}
It's hard to tell without seeing the whole code, but it might be the case that the initialization you're doing of the array is throwing an exception since it's the first time it's being initialized, during which time it is being referenced.
If that's the case, you should solve this by using a constant for the width and height instead of self reference:
public static final int HEIGHT = ...;
public static final int WIDTH = ...;
...
button = new JButton[HEIGHT][WIDTH];
Are you sure that the line/s of code are throwing a NullPointerException? Because it works for me.
public class Main {
static String a;
public static void main(String args[]) {
try {
a.charAt(0);
} catch (java.lang.NullPointerException e) {
System.out.println("Thrown");
e.printStackTrace();
}
}
}
Related
i got many errors about js... so i changed to chromedriver headless, and it work better for screenshot a specific element, but i got an errors in a other screenshot(s) code
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387)
on port 44903
Only local connections are allowed.
janv. 31, 2019 7:02:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at CarteEtdInfo.photoProfile(CarteEtdInfo.java:57)
at Accueil.<init>(Accueil.java:99)
at Login$2.actionPerformed(Login.java:287)
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.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(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$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)
this is the the code that have errors from class CarteEtdInfo
public void photoProfile() throws IOException {
String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));
Login webpage = new Login();
WebDriver pagee = webpage.driver;
pagee.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?Idsession="+cookie+"&action1=toCarteEtd");
// Get entire page screenshot
WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[4]/td[1]/div[2]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/img[1]"));
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = taswira.getLocation();
// Get width and height of the element
int eleWidth = taswira.getSize().getWidth();
int eleHeight = taswira.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); // line 57
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\avatar.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this is the line 57
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
the preivious code of screenshot an element in Login class works fine, so why this one have a problem ?
also the chromedriver not headless works fine but the headless not
Headless chrome default window size might be smaller than expected and this might cause your element to be outside of the window, hence not visible. You can either set the widndow size yourself or maximize window by using:
chromeOptions.addArguments(""--start-maximized")
i fixed the problem by adding
chromeOptions.addArguments("window-size=1980,960");
I am using Sikuli with Java to create a small automation tool. I am having trouble with this Unhandled Exception error. I am trying to pass a method I created to the GUI actionPerformed method.
package mission;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import tools.ChooseMission;
public class Story {
public static void runStoryMissions(int chapter, int stage) throws FindFailed, InterruptedException {
Screen screen = new Screen();
ChooseMission.ChooseChapter(screen,chapter);
ChooseMission.ChooseStage(screen, stage);
int dailyBiometricCount = ChooseMission.dailyBiometricCount(screen);
Pattern start = new Pattern("img/chapters/start.png");
Pattern replay = new Pattern("img/chapters/replay.png");
Pattern next = new Pattern("img/chapters/next.png");
Pattern mission_finish_bar = new Pattern("img/chapters/mission_finish_bar.png");
screen.click(start);
System.out.println("The mission has started.");
Thread.sleep(2000);
while (find(screen, mission_finish_bar) == false) {
System.out.println("Still playing the mission...");
}
if (screen.exists(mission_finish_bar) != null){
System.out.println("The mission has finished.");
}
System.out.println("Wait for 5 Seconds");
Thread.sleep(5000);
System.out.println("Click repeat button");
screen.click(replay);
}
Here is the code for my actionPerformed listener button:
btnStartMissions.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int chapter = Integer.parseInt(txtFldChapter.getText());
int stage = Integer.parseInt(txtFldStage.getText());
System.out.println("Chapter #: " + chapter);
System.out.println("Stage #: " + stage);
try {
Story.runStoryMissions(chapter, stage);
} catch (FindFailed e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
There is a error at Story. runStoryMissions(chapter,stage) it says: Unhandled Exception type FindFailed and InterruptedException
Stack Trace:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at mission.Story.runStoryMissions(Story.java:12)
at GuiFrame1$2.actionPerformed(GuiFrame1.java:94)
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)
Caused by: java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
at java.awt.Robot.checkNotDispatchThread(Unknown Source)
at java.awt.Robot.waitForIdle(Unknown Source)
at org.sikuli.script.Mouse.move(Mouse.java:345)
at org.sikuli.script.Mouse.move(Mouse.java:318)
at org.sikuli.script.Mouse.init(Mouse.java:59)
at org.sikuli.script.Screen.initScreens(Screen.java:89)
at org.sikuli.script.Screen.<clinit>(Screen.java:58)
... 38 more
Okay, I found the answer to my problem if anyone is interested.
Sikuli uses the java.awt features so scripts cannot implement and use Swing elements.
Java AWT Robot actions cannot be called froma Java swing container.
Solution is to create a new Thread:
new Thread(() -> {
try {
Story.runStoryMissions(chapter, stage);
} catch (FindFailed | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}).start();
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.
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.
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 }