I'm playing with things I don't really understand at the moment for a JAVA project on a Robot exploring an unknown territory (a grid of valid positions and blocked positions). Anyway, I tried to improve the basic GUI we were given as an example of how to proceed, it had been done with a text field and various unicode characters to represent the map and the robot. I tried to do it by overriding the paint method and drawing coloured rectangles to the screen to represent the map. When the program starts I'm presented with the runtime exception below but the window loads up (although for some reason the JButtons at the top are all invisible until i roll my mouse over them) and functions fine from then on.
I've got this far by trial and error and playing with examples I've found online but I guess there's something fundamental I haven't grasped here.
Thanks for any advice you can give me here.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controller.paint(Controller.java:156)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(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.NullPointerException
at Controller.paint(Controller.java:156)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(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 suggest you start by reading the Swing tutorial. It contains a section that explains how painting is done. For one thing you should be overriding the paintComponent() method. From the above error it appears your code is in the paint() method.
To learn something you should start small, then when you have problems you can post a SSCCE showing the problem.
Related
I currently have two halves of a video game; a storyboard system that reads in a scene from an XML file then displays a description of the scene and a list of choices that the player interacts with, and a combat system that works in a similar way only with the players prompts coming directly from the java system and not the XML. I am having trouble putting these two things together. I have combined the code of both but, in combining two working programs I have ended up with two NullPointerExceptions coming off of a DefaultListModel that in every instance has at least one value added. I can't figure out why it's still being thrown as NULL.
I cant enter my code here because it exceeds 30000 characters so here is a link to a version control repository:
https://github.com/jpinaz92/Itc303-textAdventure/commit/59a24a677b85ec237d9270c75592e09fd43cb9e6
If some one could figure out what is causing all of these problems it would rally help me, and if there is a way to post my code here even though it is longer than 30000 characters let me know and I will do it.
Here is the full output:
java.lang.NullPointerException
at textadventure.TextAdventureGUI2.initComponents(TextAdventureGUI2.java:199)
at textadventure.TextAdventureGUI2.<init>(TextAdventureGUI2.java:150)
at textadventure.TextAdventureGUI2$1.run(TextAdventureGUI2.java:107)
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$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 "main" java.lang.NullPointerException
at textadventure.TextAdventureGUI2.initComponents(TextAdventureGUI2.java:199)
at textadventure.TextAdventureGUI2.<init>(TextAdventureGUI2.java:150)
at textadventure.TextAdventureGUI2.main(TextAdventureGUI2.java:146)
I fixed it the issue was in the initialization the list model needed arguments.
Are there any smart ways to reduce logging of "equals" exceptions?
For example:
java.lang.IllegalArgumentException: Wrong parameter, should be a float from 0 to 100
at com.test.Foo.setAmount(Foo.java:93)
at com.test.Bar.setAmounts(Bar.java:39)
at com.test.Bar2.init(Bar2.java:152)
at java.awt.event.InvocationEvent.dispatch(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.access$200(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue$3.run(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue$3.run(Unknown Source) [na:1.7.0_65]
at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_65]
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) [na:1.7.0_65]
at java.awt.EventQueue.dispatchEvent(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) [na:1.7.0_65]
at java.awt.EventDispatchThread.run(Unknown Source) [na:1.7.0_65]
such exception can be thrown 90 times per second under specific circumstances. We use AWT handler to log exceptions, and it can "freeze" all Swing's application.
The first workaround which came to mind was to log exceptions in a different thread, but such approch may be a real hell in later debug.
The second thought was to put exceptions in a WeakHashMap and log exception only for first occurance. Im not sure how to perform equals for exception and whether it will be fast enough at all.
If you want to use Logback, there is a Filter called DuplicateMessageFilter that drops messages after a certain repetition.
What if you write an ExceptionWrapper class and implement equals() method which compare message and stack trace. Then logger keeps LinkedHashSet of the wrappers where newly happened exceptions are added. The logger logs the Set e.g. each second (or any different time interval). Thus newly added "equal" exceptions replace older.
I am making a calculator for an AP Computer Science Final. I built the GUI in Eclipse using Jigloo, and I quickly tried to learn about Action Listeners so you can hit the buttons to make numbers appear. The problems started occurring when I started enter the actual code to make calculations. I keep getting the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewJFrame.<init>(NewJFrame.java:82)
at NewJFrame$1.run(NewJFrame.java:73)
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$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.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'm pretty new to Java, and I've never attempted a project like this before. I would love if you guys can help me fix this problem. Here is a link to my actual code, it won't fit in the code box for some reason: Link to code
You button variables aren't assigned until you call initGUI(), which is at the bottom of your constructor. So, when you do this:
jButton3.addActionListener(new ListenToOne());
... Java sees this:
null.addActionListener(new ListenToOne());
... which is obviously a problem.
Hello I am currently attempting to create a minesweeper game, i have my gui and am using a 2d array in order to store whether or not a location has a mine, however when i attempt to have the game end for clicking on a mine using this code:
if (board[row][col] == 1) {
return GameStatus.Lost; }
else {
return GameStatus.Continue;
}
I get error as
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
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$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)
Check the bounds that you call in row and col. If, for example, you have 25 rows and columns and you're referring to board[25][25], this is past the bounds of the array. Although the overall size of the number of rows is 25, in the array the indices will go from 0 to 25-1.
Array Index Out Of Bounds means that your array has (say) 10 elements in it, but you've tried to access (say) the 11th element - it just doesn't exist.
Sanity check - arrays are indexed starting from 0, are your values in row and col indexed from 1?
When it occurs?
Out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1)
for example:
String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.
If you don't know the size or length of an array, you can know it from stringArray.length.
How to handle it?
You should make sure that your program doesn't access an array with index bigger than length - 1.
example:
for(int i=0;i<stringArray.lenght;i++) {
//write your code here
}
the above code will guarantee that stringArray will never be accessed beyond its maximum index.
Your Case
In your case, you must have defined the array limit and trying to access the data of array which is out of range of defined limit.
Also read this for more info...
Example of 2d array having out of bound exception
I have created a jar file which throws the below error, it's a simple swing app which inserts a row when I press a button, not sure where I am going wrong please advise.
private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setatmid(jTextField2.getText());
setaa10(Integer.parseInt(jTextField3.getText()));
setaa20(Integer.parseInt(jTextField4.getText()));
setaa50(Integer.parseInt(jTextField5.getText()));
setaa100(Integer.parseInt(jTextField6.getText()));
try{
System.err.println("Inserting values in Mysql database table!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "agents";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","");
Statement st = con.createStatement();
String query="INSERT INTO schedule_data (`s_ID`, `schedule_date`, `atmID`, `notification`) VALUES ('"+System.currentTimeMillis()+"','2010-09-15','"+getatmid()+"','null')";
st.executeUpdate(query);
System.err.println("1 row affected");
} catch(Exception e) {
e.printStackTrace();
}
}
Error:
java.lang.IllegalStateException: zip file closed
at java.util.zip.ZipFile.ensureOpen(Unknown Source)
at java.util.zip.ZipFile.getEntry(Unknown Source)
at java.util.jar.JarFile.getEntry(Unknown Source)
at java.util.jar.JarFile.getJarEntry(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at atmguis.atm.jButton20ActionPerformed(atm.java:588)
at atmguis.atm.access$1600(atm.java:25)
at atmguis.atm$17.actionPerformed(atm.java:226)
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.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 method involved is being called from the Event Dispatch Thread. I'm sure this is a part of the problem. You are trying to access the JAR file containing the com.mysql.jdbc.Driver class from this thread. This is where the error is being thrown. I have to wonder if there is some sort of concurrency issue here. Here are a couple of general notes, things that should be addressed. Once you have addressed these issues, see if you are still having a problem.
You should not be doing a database query from inside the EDT. You should collect the information you need from the swing components and then use a Runnable object to execute the SQL query on a different thread. Do a search on SO for executing code on or off the EDT to find examples of how to do this. This will ensure that your UI doesn't lock up while you wait for your SQL results.
Opening and closing a database connection every time you need one is something better left to the SQL driver and its built-in connection pooling abilities. This method should be declared on some sort of controller object which already has a reference to the SQL connection. Then, when this method is called, you call your thread as in the last step, and that thread uses the reference to the SQL connection that it already has.
This will take the line that's throwing the exception and move it out of the EDT into some sort of setup phase, presumably where you will have better luck accessing the class file. Certainly it will be a much more controlled environment than within the EDT. If there continues to be a problem accessing it, it will be easier to debug in the more controlled environment.
As an added bonus, you will also be designing your application in much more robust way.