I have an array of JPanels and I'm trying to add a mouseadapter to each one so it will be possible to identify which one was clicked and then change its background colour. Everything seems to work fine except when I run it from eclipse when a page will appear that says EventDispatchThread.run() line: not available, Source not found, and in the debug window it says:
Thread [AWT-Shutdown] (Running)
Daemon Thread [AWT-Windows] (Running)
Thread [AWT-EventQueue-0] (Suspended (exception ArrayIndexOutOfBoundsException))
EventDispatchThread.run() line: not available
Thread [DestroyJavaVM] (Running)
Thread [AWT-EventQueue-0] (Running)
This is the code:
private void drawBoard() {
LayoutManager layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLS);
boardPanel.setLayout(layout);
boardPanel.setPreferredSize(new Dimension(200, 400));
chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
MoveArrays move = new MoveArrays();
move.initialisePieceMoves();
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {
int index = i * 4 + j;
chessBoard[i][j] = new JPanel();
chessBoard[i][j].addMouseListener(clickSquare(j, i, index, move));
chessBoard[i][j].setBackground(getColor(i,j));
if (!(boardArray.chessBoard[index].square.isEmpty())) {
Piece piece = (Piece) boardArray.chessBoard[index].square.firstElement();
JLabel pieceString = new JLabel(piece.toString());
chessBoard[i][j].add(pieceString);
}
boardPanel.add(chessBoard[i][j]);
}
}
} // drawBoard()
private MouseAdapter clickSquare(final int xCo, final int yCo, final int index, final MoveArrays move) {
return new MouseAdapter() {
public void mousePressed(MouseEvent me) {
resetColors();
JPanel selectedSquare = (JPanel) me.getSource();
selectedSquare.setBackground(selectedColor());
System.out.println("xCo: " + xCo + " yCo: " + yCo);
Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];
if (!validMoves.isEmpty()) {
for (int n = 0; n <= totalMoves; n++) {
String stringMove = validMoves.elementAt(n).toString();
int intMove = Integer.parseInt(stringMove);
System.out.println("intMove: " + intMove);
}
}
}
};
}
I think it might be the fact that I cast the me.getSource to a JPanel but shouldn't it be one anyway? If I don't put the cast it says that it cannot bind an 'Object' to a JPanel, when I do System.out.print(me.getSource()) it prints a line saying that it's a JPanel so I don't get what the problem is. Any help would be much appreciated!
It appears the problem is not to do with the source of the mouse event. Indeed I think there's two different things being called "source" here, and you're mixing them together. I think the message:
EventDispatchThread.run() line: not available, Source not found
Is Eclipse telling you that the library does not have the source code attached to it, so it cannot find line numbers or show you the source code. The problem does not appear to be with me.getSource().
The problem is that you are trying to reference an index within an array that is outwith the bounds of the array (hence the ArrayIndexOutOfBoundsException in the stack trace).
Since the stack trace is on the AWT Event Queue thread, it is likely the exception is stemming from within the mousePressed() method of your MouseAdapter. Since you're already using Eclipse, I suggest you get to know the Debugger, which is extremely useful. Put a breakpoint at these lines:
Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];
And check that both the array fields of move are large enough to reference an element at the index. If you don't want to use the debugger (I'd really recommend that way), then you could wrap those two lines with an exception catch, like this:
try {
Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception thrown: index = " + index +
"Array lengths: " + move.DroneMovesNorth.length + ", " +
move.DroneTotalMovesNorth.length);
}
When/if the exception is caught, you want to find out why the index is larger than the size of each of these arrays. That's left as an exercise ;-)
Edit: there's some other code in there which looks suspicious.
First you declare the array for the chessboard:
chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
Then you begin to iterate for every square in the board:
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
for (int j = 0; j < NUMBER_OF_COLS; j++) {
Note that i will run up to the value NUMBER_OF_ROWS, which is the length of the chessboard array. Then for some reason I don't understand, you change the value of index to something that could be 4x as large as the length of the array:
int index = i * 4 + j;
And later try to reference that position in the chessboard array:
if (!(boardArray.chessBoard[index].square.isEmpty())) {
This means it's possible for index to be a value higher than NUMBER_OF_ROWS, and if you try to access the element at that index of the array, it will throw the ArrayIndexOutOfBoundsException.
So, my second suggestion would be to take another look at the logic involved in:
int index = i * 4 + j;
... as that may also be the problem.
P.S. the debugger in Eclipse is awesome, you should use it ;-)
Related
I have a homework question I need help with
We have been given a text file containing one word per line, of a story.
We need to read this file into an array, perform a sort on the array and then perform a binary search.
The task also says I'll need to use an overload method, but I'm unsure where
I have a bubble sort, that I've tested on a small array of characters which works
public static void bubbleV1String(String[]numbers)
{
for(int i = 0; i < numbers.length-1; i++)
{
for(int j = 0; j < numbers.length-1; j++)
{
if(numbers[j] .compareTo(numbers[j+1])>0)
{
String temp = numbers[j+1];
numbers[j+1] = numbers[j];
numbers[j] = temp;
}
}
}
}`
And my binary search which I've tested on the same small array
public static String binarySearch(int[] numbers, int wanted)
{
ArrayUtilities.bucketSort(numbers);
int left = 0;
int right = numbers.length-1;
while(left <= right)
{
int middle = (left+right)/2;
if (numbers[middle] == wanted)
{
return (wanted + " was found at position " + middle);
}
else if(numbers[middle] > wanted)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return wanted + " was not found";
}
Here is my code in an app class to read in a file and sort it
String[] myArray = new String[100000];
int index = 0;
File text = new File("threebears.txt");
try {
Scanner scan = new Scanner(text);
while(scan.hasNextLine() && index < 100000)
{
myArray[index] = scan.nextLine();
index++;
}
scan.close();
} catch (IOException e) {
System.out.println("Problem with file");
e.printStackTrace();
}
ArrayUtilities.bubbleV1String(myArray);
try {
FileWriter outFile = new FileWriter("sorted1.txt");
PrintWriter out = new PrintWriter(outFile);
for(String item : myArray)
{
out.println(item);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
When I go to run the code, I get a null pointer exception and the following message
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.compareTo(Unknown Source)
at parrayutilities.ArrayUtilities.bubbleV1String(ArrayUtilities.java:129)
at parrayutilities.binarySearchApp.main(binarySearchApp.java:32)
Line 129 refers to this line of code of my bubblesort
if(numbers[j] .compareTo(numbers[j+1])>0)
And line 32 refers to the piece of code where I call the bubblesort
ArrayUtilities.bubbleV1String(myArray);
Does anyone know why I'm getting a null pointer exception when I've tested the bubblesort on a small string array? I'm thinking possibly something to do with the overloaded method mentioned earlier but I'm not sure
Thanks
You are creating an array of length 100000 and fill the lines as they are read. Initially all elements will be null and after reading the file quite a number of them is likely to still be null. Thus when you sort the array numbers[j] will eventually be a null element and thus calling compareTo(...) on that will throw a NullPointerException.
To fix that you need to know where in the array the non-null part ends. You are already tracking the number of read lines in index so after reading the file that would be the index of the first null element.
Now you basically have 2 options:
Pass index to bubbleV1String() and do for(int i = 0; i < index-1; i++) etc.
Make a copy of the array after reading the lines and before sorting it:
String[] copy = new String[index];
StringSystem.arrayCopy(myArray,0,copy,0,index);
//optional but it can make the rest of the code easier to handle: replace myArray with copy
myArray = copy;
Finally you could also use a List<String> which would be better than using arrays but I assume that's covered by a future lesson.
It seems that you have some null values in your numbers array. Try to debug your code (or just print array's content) and verify what you have there. Hard to tell anything not knowing what is in your input file.
Method overloading is when multiple functions have the same name but different parameters.
e.g. (taken from wikipedia - function overloading)
// volume of a cube
int volume(const int s)
{
return s*s*s;
}
// volume of a cylinder
double volume(const double r, const int h)
{
return 3.1415926*r*r*static_cast<double>(h);
}
Regarding your null pointer exception, you've created an array of size 100000, but it's likely you haven't read in enough information to fill that size. Therefore some of the array is empty when you try to access it. There are multiple ways you can go about this, off the top of my head that includes array lists, dynamic arrays or even moving the contents of the array to another one, once you know the size of the contents (however this is inefficient).
I'm using a piece of code taken from the website The Nature of Code - https://natureofcode.com/book/chapter-7-cellular-automata/ - the portion I used was from example 7.1. I'm trying to create a one-dimensional cellular automaton using the Processing IDE, but I keep getting an error saying that brackets are missing from particular locations.
Errors:
-Missing curlry bracket "}", line 32
-Syntax error on "}", delete this, line 40
I've gone over it multiple times, but I can't see how this is wrong. Though I did try changing them as it says, only to get more errors. I thought maybe they were just in the wrong place, but I also can't see how that's the case. They seem to be correct as far as I can tell, but maybe I'm missing something. This is my first time using Processing, and it's been a long time since I last used Java. So maybe I'm mistaken.
class CA {
int[] cells;
int[] ruleset;
int w = 10;
// The CA should keep track of how
// many generations.
int generation = 0;
CA() {
cells = new int[width/w];
ruleset = new int[]{0,1,0,1,1,0,1,0};
cells[cells.length/2] = 1;
}
// Function to compute the next generation
void generate() {
int[] nextgen = new int[cells.length];
for (int i = 1; i < cells.length-1; i++) {
int left = cells[i-1];
int me = cells[i];
int right = cells[i+1];
nextgen[i] = rules(left, me, right);
}
cells = nextgen;
// Increment the generation counter.
generation++;
}
int rules(int a, int b, int c) {
String s = "" + a + b + c;
int index = Integer.parseInt(s,2);
return ruleset[index];
}
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) fill(0);
else fill(255);
// Set the y-location according to the generation.
rect(i*w, generation*w, w, w);
}
}
The program is supposed to print each generation of the one-dimensional CA on top of the next.
The for-loop in the end of the CA class is not supposed to be there. That is why you get the error: the parser expects either a method declaration or the end of the class, hence a curly bracket.
It looks like this loop is actually drawing the CA state, so you can try to wrap it in a void draw(){} method. Then it should be syntactically correct, not sure though whether it works as expected. Alternatively move the for-loop outside of the class and call fill based on the the cells state of an instance of the CA class.
In any case, will need some additional code that creates the CA instance and invokes the generate function.
I am using the MASON library to run a simple agent-based model.
As per specifications, I meant to access an agent's inspector by double-clicking on such agent in the Portrayal.
However, when I do so I get the following console error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sim.display.Display2D.createInspectors(Display2D.java:1737)
at sim.display.Display2D$8.mouseClicked(Display2D.java:1392)
I went to Display2D.java:1737 to find:
public void createInspectors( final Rectangle2D.Double rect, final GUIState simulation )
{
Bag inspectors = new Bag();
Bag names = new Bag();
Bag[] hitObjects = objectsHitBy(rect);
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}
simulation.controller.setInspectors(inspectors,names); //1737
}
However, this is a library file so I'm not familiar with it.
Any advice?
Library: cs.gmu.edu/~eclab/projects/mason/
Update:
Ok, it gets interesting...
I did an echo on the toString method of inspectors and names, returning:
insepectors sim.util.Bag#1b2202a names sim.util.Bag#16b334d
Ok so they're bags, a type of collection. Time to get their sizes...
insepectors 1 names 1
Good, they're not empty.
Let's follow the error stack
Next bit:
at sim.display.Display2D$8.mouseClicked(Display2D.java:1392)
// add mouse listener for the inspectors
insideDisplay.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (handleMouseEvent(e)) { repaint(); return; }
else
{
// we only care about mouse button 1. Perhaps in the future we may eliminate some key modifiers as well
int modifiers = e.getModifiers();
if ((modifiers & e.BUTTON1_MASK) == e.BUTTON1_MASK)
{
final Point point = e.getPoint();
if( e.getClickCount() == 2 )
1392-> createInspectors( new Rectangle2D.Double( point.x, point.y, 1, 1 ),
Display2D.this.simulation );
if (e.getClickCount() == 1 || e.getClickCount() == 2) // in both situations
performSelection( new Rectangle2D.Double( point.x, point.y, 1, 1 ));
repaint();
}
}
}
Ok, the error is clearly in the CreateInspectors method:
public void createInspectors( final Rectangle2D.Double rect, final GUIState simulation )
{
Bag inspectors = new Bag();
Bag names = new Bag();
Bag[] hitObjects = objectsHitBy(rect);
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}
System.out.println("insepectors " + inspectors.size() + " names " + names.size());
simulation.controller.setInspectors(inspectors,names);
}
First thing I do:
System.out.println(rect.getCenterX() + " " + rect.getCenterY());
Which gives me 646.5 659.5. Seem to make sense as coordinates.
So next thing I want to look at hitObjects:
System.out.println(hitObjects.length);
Returns 2. So I had two agents at that coordinates.
I assume the NPE is somewhere here:
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}
The outer loop looks fine, but I'm having issues with the inner one. Advice?
I'm actually going to write how to reason through the library code given as far as you've come since you sound capable.
You found the code. That's a good start. You've also found the line that it's on:
simulation.controller.setInspectors(inspectors,names); //1737
And given what you know about NPEs you can reason that simulation or controller is null.
Do you know which? Can you reason about your code which you may have failed to set, or passed incorrectly? Can you put a debugger breakpoint or simple println statements in your code to determine which?
If not and you're using an IDE (you probably are), then from the source you just opened up, put a breakpoint before line 1737. When that line is reached, use the debugger to inspect the variable simulation then simulation.controller to see which is null. Exactly how to do this will depend on your IDE but it shouldn't be hard to find using XKCD's tech support cheat sheet.
Then you will probably know the cause. If not, continue reading source code, e.g. look a level up to line 1737.
I am programming a game and almost have the save-file system complete. I have two Vectors (one holds the name of the savegame, one holds the sessionID).
At launch, the program will read in data from a file and add that information to the Vectors. Then another method is called to check if the files shown in the Vector acctualy exist. If not, they will be removed from the Vectors. At the end, the Vectors are printed to and rewrite the file.
The problem I'm having is the for loop isn't checking every item in the Vector, because Vector.size() is decreasing when items are removed.Is there a better way to form the for loop, or is there a workaround I can use?
private static void slistCleanup() throws IOException {
private static Vector<String> saveNames = new Vector<String>();
private static Vector<Integer> sessionIDs = new Vector<Integer>();
Scanner slistReader = new Scanner(new FileReader(sessionList));
File tempSave;
String path;
int run = 1;
String tempName = " ";
int tempID = 0;
for (int x = 0; x < saveNames.size(); x++) {
path = currentDir + "\\saves\\" + sessionIDs.elementAt(x) + ".sav";
tempSave = new File(path);
System.out.println("-----------------------"); //debug
System.out.println("current pass: " + run);
System.out.println("tempSave Path: " + tempSave.getAbsolutePath()); //debug
System.out.println("tempSave exists: " + tempSave.exists()); //debug
System.out.println("-----------------------"); //debug
run++; //debug
if (!tempSave.exists()) {
saveNames.remove(x);
sessionIDs.remove(x);
}
}
for (int x = 0; x < saveNames.size(); x++) {
System.out.println(saveNames.elementAt(x));
System.out.println(sessionIDs.elementAt(x));
}
slistReader.close();
}
If you need more code, let me know.
Loop backwards:
for (int x = saveNames.size()-1; x >= 0; x--)
One way that would require few changes to your existing code would be to traverse the vector in the reverse direction.
for (int x = saveNames.size() - 1; x >= 0; x--) {
...
}
As Fildor noted in the comments, you can do this with iterators
Iterator namesItr = saveNames.iterator();
Iterator sessionItr = sessionIDs.iterator();
while(namesItr.hasNext() && sessionItr.hasNext()) {
Object currentName = namesItr.next();
Object currentSession = sessionItr.next();
if (!tempSave.exists()) {
namesItr.remove();
sessionItr.remove();
}
}
Always loop backwards through an object when you are removing items from it, so:
for (int x = saveNames.size()-1; x >=0; x--) {
That way the removed items don't cause you a problem.
The reason for the problem is that you are starting at x=0; you delete x=0 (so x=1 is the new x=0, x=2 is the new x=1 etc) but you move on to x=1, skipping one.
On the other hand if you start at saveNames.size()-1:
You start at (for example) 9, delete it, 9 is now empty but we move on to 8 anyway. 8 is unaffected because its before 9
If you are having trouble because you're deleting items from the array as your looping over it, you can make a new array before the loop and put items you're keeping into it. After the loop is done, the new array will only contain items you're keeping, and you can set the original array to the new array you created.
You can just add the indexes you want to remove, and the going from last to first remove them. Or you can also use
x--;
inside your loop when you remove a element.
Best and simple way to remove items via loops in my opinion is:
scan the items using for loop and add the itemsToDelete list the items you want to delete.
list.removeAll(itemsToDelete)
The simplest thing to do is just take out x++.
Explanation: When you remove saveNames.remove(x) and x=0 then what was in index 1 moves to index 0. X is still 0 and it'll now remove the 2nd item which is now at index 0. Once all the items are removed saveNames.size() will be 0, x is no longer less than saveNames.size() so the loop will break.
I'm trying to make a tetris game for android to help learn game programming for android. My goLeft/Rights break right when the button is pressed, the code for going left is in a class separate of the fields int array, and the list parts array. The fields array is accessed by a referenced variable (TetrisWorld tetrisworld;). While part list array is public so accessed through a variable(part) code for which is in the goLeft() code. It breaks at: if(tetrisworld.fields[x][part.y] != 0) Code for left:
public void goLeft() {
int x = 0;
for(int i = 0; i < 4; i++) {
TetrisParts part = parts.get(i);
x = part.x - 1;
if(tetrisworld.fields[x][part.y] != 0) {
noleft = true;
break;
}
}
if(noleft == false) {
for(int i = 0; i < 4; i++) {
TetrisParts part = parts.get(i);
part.x--;
}
}
}
The code for the fields int array:
int fields[][] = new int[WORLD_WIDTH][WORLD_HEIGHT];
WORLD_WIDTH and WORLD_HEIGHT are both static final ints, width being 9 and height being 19
I've tried putting if(tetrisworld.fields[0][0] == 0) and it still crashes so I don't think it has to do with the variables. Also It doesn't go out of bound even if I haven't added the code to check for that yet because I have the teroid spawning around x = 5 and since I can't go left/right once there's not a chance of that happening
I've tried moving the goLeft/Right methods to the gamescreen class which has a "world = TetrisWorld();" and it still bugs out at the same spot
UPDATE:
Ok just adding:
tetrisworld != null
to the first if statement fixed it, my question now is, why did it fix it? Why can't I move without this check? It clearly isn't null cause as far as I know; it's fully responsive now.
But an easier way to have solved this which is SOOOO easy is changing fields to static... then access it lika so: TetrisWorld.fields so my updated code is:
public void goLeft()
{
noleft = false;
for (int i = 0; i < 4; i++)
{
part = parts.get(i);
if (part.x - 1 < 0 || TetrisWorld.fields[part.x - 1][part.y] != 0)
{
noleft = true;
break;
}
}
if (noleft == false)
{
for (int i = 0; i < 4; i++)
{
part = parts.get(i);
part.x--;
}
}
}
Looks like you are hitting IndexOutOfBoundsException.
When you are doing x = part.x - 1;, your x variable can become lesser tan zero, thus your code will act like if(tetrisworld.fields[-1][part.y] != 0
It looks like you're getting a java.lang.NullPointerException when trying to access the array in tetrisworld. In the line you mention there are several ways that this could occur:
if(tetrisworld.fields[x][part.y] != 0) {
tetrisworld could be null.
The fields member of tetrisworld could be null.
The second array that you're looking up by using tetrisworld.fields[x].
The value of part could be null.
Having a quick look through your source code it looks to me like you never initialise tetrisworld, either at declaration using:
TetrisWorld tetrisworld = new TetrisWorld();
Or at some other point which is certain to have happened before your goLeft() method is called.
Ok I believe I found the answer, referencing: http://en.wikipedia.org/wiki/Null_Object_pattern
Apparently java will throw an NPE if you don't check for it first if you have a null reference? Is there any way to initialize it without doing a TetrisWorld tetrisworld = new TetrisWorld(); because it's already created in a different class so i get a thousand errors, an actual stack overflow! lul... Still not 100% positive. Please comment to verify and possibly suggest a better way to go about this.