How to load modifications made in native java classes? - java

Hi,
I'm trying to execute a jar file, overwriting some java originals interfaces to connect with the database, changing the method setBoolean to receive an object like parameter.
That's the original interface method:
void setBoolean(int parameterIndex, boolean x) throws SQLException;
That's my change:
void setBoolean(int parameterIndex, Boolean x) throws SQLException;
My changes were made at interface java.sql.PreparedStatement. For my changes to be called, when I start the application I'm using the command:
java -verbose:class -Xbootclasspath/p:C:/Users/user/Desktop/myproject/src/main/java -jar safepdv.jar
With the command "-verbose:class" I can verify that the class with my changes are being loaded.
However, when the method setBoolean is called by the application, he isn't founded, and my return is:
Exception in thread "AWT-EventQueue-0" [Loaded java.lang.Throwable$WrappedPrintStream from C:\Program Files\Java\jre1.8.0_91\lib\rt.jar]
java.lang.NoSuchMethodError: java.sql.PreparedStatement.setBoolean(IZ)V
at com.t2tierp.pafecf.controller.ConfigurationController.updateCS(ConfigurationController.java:613)
at com.t2tierp.pafecf.view.OpenMoviment.validaCamposLogin(OpenMoviment.java:448)
at com.t2tierp.pafecf.view.OpenMoviment.passwordTextSenhaUsuarioActionPerformed(OpenMoviment.java:403)
at com.t2tierp.pafecf.view.OpenMoviment.access$200(OpenMoviment.java:22)
at com.t2tierp.pafecf.view.OpenMoviment$4.actionPerformed(OpenMoviment.java:240)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(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.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 com.t2tierp.pafecf.view.Caixa.verificarMovimentoExistente(Caixa.java:3392)
at com.t2tierp.pafecf.view.Caixa.<init>(Caixa.java:344)
at com.t2tierp.pafecf.view.Caixa$41.run(Caixa.java:2393)
at java.awt.event.InvocationEvent.dispatch(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.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)
How can I make my method was really called instead of the original method?

The calling code is still trying to pass a null value to the old method's signature (which expects a boolean, and not a Boolean). You would need to recompile the calling code so that it binds to the new method signature: the method signatures to be called are fixed at compile-time.
Also, note that PreparedStatement is an interface - changing its definition is likely to break all classes implementing said interface. Most notably, each JDBC driver will include its own implementation; you would have to recompile the drivers from source to fix this.
It is possible, but ugly and unmaintainable, to decompile the calling code and simply fix it at the point where the null is being passed, and then recompile it. You can automate this to a certain point (think bytecode-editing libraries), but the gist is that not having access to the source code making the bad calls is a severe drawback.

Related

Event errors, not sure what they mean [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm currently getting these errors, I've tried doing a few things to see if that would help. I even used the debugger to find out why these errors are occurring, but i'm still not sure.
If anyone would be able to help it would be really appreciated.
I tried looking at the suggested post and i made the fixes suggested, however this didn't help, the errors still remain the same.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at c3537779.ByteReader.getBytes(ByteReader.java:14)
at c3537779.BytePattern.patternParse(BytePattern.java:37)
at c3537779.FileChooser.PatternOpen(FileChooser.java:95)
at c3537779.GUI$OpenPatternAction.actionPerformed(GUI.java:74)
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$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)
NullPointerException throws when the object where you try to call some method does not exists. So we have it at c3537779.ByteReader.getBytes(ByteReader.java:14) and you need to debug the process on 14 row of class ByteReader and look what it calls. If You will see something like myName.myMethod(); you should check creation process of myName object because it null.

Code not working after exporting, but befor it did

I need your help.
I'm currently developing a java application that uses MySQL
(com.mysql.cj.jdbc.MysqlDataSource to be exact), and if I run it inside Intellij IDEA, everything works fine, but after exporting, I get an error message:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/MysqlDataSource
at ger.mrSoUndso.Main$1.actionPerformed(Main.java:77)
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.ClassNotFoundException: com.mysql.cj.jdbc.MysqlDataSource
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 37 more
How can i fix this error?
Thanks in advance.
Mr_SoUndso
PS: I know a similar question was asked before, but without stack trace. There also was no answer given.
A friend showed me how to fix it:
You need to make your library available in your jar.
Then everything is working fine
Assuming you are exporting the app as a jar file, you need to provide the path of that driver jar while running the app, e.g.:
java -cp '<path_to_driver_jar>' yourApp.jar
This would make sure the library is available to your app and it will work fine.

Creating files in Roaming Directory

I've been having lots of trouble trying to create a file ANYWHERE in any directories and I've had no luck.
#SuppressWarnings("resource") //Install the modpack and create files
public void installModpack(){
File f = new File("\\ultima");
f.mkdirs();
}
Here's the stack trace:
C:\Users\Drew\AppData\Roaming
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at ultima.launcher.Ultima.installModpack(Ultima.java:299)
at ultima.launcher.Ultima$6.mouseClicked(Ultima.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(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)
The stacktrace does not match the code in your question. The stacktrace says that installModpack is calling File.createNewFile, but in your code there is no such call.
The exception message seems to be saying that it is trying to create "C:\Users\Drew\AppData\Roaming", or create a file in that directory. That doesn't match the pathname "\ultima" that you are apparently trying to use.
Without seeing the actual code, we can't tell you what the real solution is, but I would:
Check to see if that "C:\Users\Drew\AppData\Roaming" exists using some other tool.
Examine the sourcecode and/or run your code using a debugger to figure out which installModpack method you are really using.
I answered this myself. Some code I had later in the code messed with this :p
The correct usage for anybody who is looking here is:
File f = new File(System.getenv("APPDATA") + "/.ultima");
f.mkdirs();

NullPointerException when trying to copy/paste from/into clipboard

I have an specific issue with copy/paste from my Java application. I made IT utility that we use in our IT Department for Evidence of our IT assessments.
When I try to copy from anything (jTextArea, jTextField, jTable cell, etc..) I receive bellow shown NullPointerException.
This issue appears randomly, I can copy In/Out all I want, but then, let say hour after, I want to copy some information out and this appears, after that I cannot copy anything in or out and only thing that helps is restarting the application. After Restart, I can try to copy anything anywhere, even use Java Toolkit.getDefaultToolkit().getSystemClipboard() function with no error, but again, after some time (usually after one or more hours) it appears again. For this time, there is only one active scheduled method that keeps refreshing the main table from SQL. I don't think that my code will help here is this application have more that 65K rows ant this issue appears randomly anywhere.
But my question is - Is there any special way to treat system Clipboard, or are there any common mistakes that could make such behavior? I found nothing that could help me understand my problem.
I'll appreciate any help or leads whatsoever, thank you:)
Error message:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.Win32FileSystem.normalize(Unknown Source)
at java.io.Win32FileSystem.getUserPath(Unknown Source)
at java.io.Win32FileSystem.resolve(Unknown Source)
at java.io.File.getAbsolutePath(Unknown Source)
at java.io.File.getAbsoluteFile(Unknown Source)
at java.io.File.toURI(Unknown Source)
at java.awt.datatransfer.SystemFlavorMap$1.run(Unknown Source)
at java.awt.datatransfer.SystemFlavorMap$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.datatransfer.SystemFlavorMap.initSystemFlavorMap(Unknown Source)
at java.awt.datatransfer.SystemFlavorMap.getFlavorToNative(Unknown Source)
at java.awt.datatransfer.SystemFlavorMap.getNativesForFlavor(Unknown Source)
at sun.awt.datatransfer.DataTransferer.getFormatsForFlavors(Unknown Source)
at sun.awt.windows.WDataTransferer.getFormatsForFlavors(Unknown Source)
at sun.awt.datatransfer.DataTransferer.getFormatsForTransferable(Unknown Source)
at sun.awt.windows.WClipboard.setContentsNative(Unknown Source)
at sun.awt.datatransfer.SunClipboard.setContents(Unknown Source)
at GUI.Evidence.EvidenceMain.SaveToClip(EvidenceMain.java:711)
at GUI.Evidence.EvidenceMain.access$800(EvidenceMain.java:65)
at GUI.Evidence.EvidenceMain$4.mouseReleased(EvidenceMain.java:401)
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)
java.io.IOException: cannot transfer non-text data as Reader
at sun.awt.datatransfer.DataTransferer.translateTransferable(Unknown Source)
at sun.awt.windows.WDataTransferer.translateTransferable(Unknown Source)
at sun.awt.windows.WClipboard.setContentsNative(Unknown Source)
at sun.awt.datatransfer.SunClipboard.setContents(Unknown Source)
at javax.swing.TransferHandler.exportToClipboard(Unknown Source)
at javax.swing.TransferHandler$TransferAction.actionPerformedImpl(Unknown Source)
at javax.swing.TransferHandler$TransferAction.access$700(Unknown Source)
at javax.swing.TransferHandler$TransferAction$1.run(Unknown Source)
at javax.swing.TransferHandler$TransferAction$1.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 javax.swing.TransferHandler$TransferAction$2.run(Unknown Source)
at javax.swing.TransferHandler$TransferAction$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.TransferHandler$TransferAction.actionPerformed(Unknown Source)
at javax.swing.text.JTextComponent.invokeAction(Unknown Source)
at javax.swing.text.JTextComponent.copy(Unknown Source)
at javax.swing.text.DefaultEditorKit$CopyAction.actionPerformed(UnknownSource)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(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$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)

Dynamic sprites with Arraylist - Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

GameCanvas
AppleEnity - http://pastebin.com/ZVyL5fRm
Enity - paste bin.com/r4cqtr8B (cant post more than 2 links yet)
I have problem with adding new, dynamic sprites to my scene. Pressing E (with should spawn randomly an apple) gives me this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at start.GameCanvas.dodaj(GameCanvas.java:65)
at start.GameCanvas.keyPressed(GameCanvas.java:149)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Component.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.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)
The problem is obviously in this line:
jablka.add(new AppleEnity("resources/apple.png", random.nextInt(400), random.nextInt(400)));
...but it's not about jablka variable, but with this expression:
random.nextInt(400)
you never initialize random variable:
private Random random;
Replace the line above with:
private final Random random = new Random();
And you'll be fine. Or just use Math object or RandomUtils.

Categories