Unknown Exception/Error in JavaFX FXML 2.2 - java

I am using NetBeans 7.3.1 and JavaFX 2.2.
I am getting an error which says just '2' (not like NullPointerException, etc). When I change some 2-d Array values then error is some other number.
StackTrace :
Executing com.javafx.main.Main from (my project path).jar using platform D:\Java\jdk1.7.0_25/bin/java
2 this is my error. sometimes 6,5 ,etc
file:/(my project path).jar!/<project name>/SelectionWindow.fxml
at <project name>.SelectionWindowController.attachBrandImagesAndNamesToTiles(SelectionWindowController.java:92)
at <project name>.SelectionWindowController.initialize(SelectionWindowController.java:33)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2152)
at ..... all the trailing trace
Explain me this error and how to solve it.

Well, seeing the exception is thrown from this line:
brandNames[i][j] = new Label(DataInterface.getBrandName(i, j));
It may be reasonable to assume you have your array elements out of range, causing it to throw an OutOfBoundsException 2 if for example you tried to reach element 2 from the array but for example it only has 1 element.
Without your full stack trace it is hard to say for sure though.

This is difficult to say. The stack trace shows the error on line 92, which is this:
brandNames[i][j] = new Label(DataInterface.getBrandName(i, j));
The javafx.scene.control.Label constructor doesn't appear to throw any exceptions (see http://docs.oracle.com/javafx/2/api/javafx/scene/control/Label.html#Label%28java.lang.String%29). I would say
Put a try/catch block around the entire inner loop.
In the catch statement, use reflection to find out what exactly the class type of the exception is (post back if you don't know how, and I'll write the code for you)
And then also write out the message from the caught exception.

Related

How to generate exceptions when trying to matching input from list

Slightly odd question but I'm practicing exception handling with try catch blocks at the minute and I'm trying to generate an exception caused by a user entering values that are not on a predefined list. I am unsure of how to generate the errors needed to work on the exception handling. The simplest possible example to generate the error is fine, I'm just using months of the year. I have tried adding them to an arraylist and running a months.contains() line but it's not giving me any errors if I enter something that's not there.
I'm normally fantastic at generating exceptions, but somethings changed
You can use an enum for this.
Declare this outside you method.
enum MONTHES {
JAN, FEB, MAR;
};
Inside your method do this.
MONTHES.valueOf("FOO");
This will throw an java.lang.IllegalArgumentException

Finding xpath or cssSelector locations for all

I am relatively new to development so please forgive me if some of this seems rather amateurish. Part of my reason for posting the question is to help nudge me to the answer, part is to make sure I'm following good coding practice.
The challenge -
I'm using Java & Selenium to check a very large, dynamically populated table. I need to find a specific list of elements where the text matches a case-sensitive String -
List<WebElement> AllPaths = getCurrentDriver().findElements(By.xpath("//*[text()[contains(.,'" + fixedString + "')]]"));
The table I'm checking is basically a large calendar-style grid. If I don't find evidence of fixedString, I then want to iterate back one month at a time until I DO find the fixedString.
The problem -
The code above returns an exception if it cannot find an element. My first thought was to setup a while loop, trying/catching the exception and then repeating until exceptions stopped. However this feels wrong to me - I don't think I should be essentially "swallowing" exceptions. That said, I'm not sure what the correct way of trying to find this element is that doesn't lead to an exception if it fails to locate it.
Am I right in thinking it's a bad idea to write code that you know causes an exception and then simply swallow it and move on?
Hope this makes sense, as I say I'm a beginner so please be gentle :)
You can try something like -
if(AllPaths.size()>0){
//logic when elements found with fixed string
}else{
//logic to iterate over another month
}
Also, your statement seems wrong to me. It should be -
List<WebElement> AllPaths = getCurrentDriver().findElements(By.xpath("//*[contains(text(),'" + fixedString + "')]"));
findElements doesn't throw exception this way. It will return empty if no elements located. The exception seems due to incorrect statement that you are using to find elements.
The code above returns an exception if it cannot find an element.
The documentation says findElements returns an empty list when no elements are found. It should not throw any exception in this case. Is it possible that you by mistake used findElement instead of findElements? What type of exception is thrown and what is the message?
You shouldn't need to catch exceptions here. You are right though that control flow using exceptions should be avoided and that swallowing exceptions is bad. On the other hand frameworks don't always let you write code the way you want so sometimes exceptions have to be made.

Java - java.lang.ArrayIndexOutOfBoundsException

I'm trying to run my code and it says something about the exception:
java.lang.ArrayIndexOutOfBoundsException
I googled it, from what I understand it happens when I try to access an index that's negative or greater than my array length.
But I can't seem to find the problem, here's my code: http://pastebin.com/sXsBbYfh
Thanks for any helpers.
EDIT: the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Calculator.addOne(Calculator.java:127)
at Calculator.add(Calculator.java:88)
at Program.main(Program.java:8)
About the relevant part of the code, I have no idea, that's why i'm coming to you.
The issue would appear to be with line 86
arrResult = this.addOne(arrResult.length);
Array indexes are 0 based, so 0 - length-1 and you are passing length in and then using that to access your array on line 127
switch(arrResult[arrayIndex])
This part of code :
public int[] addOne(int arrayIndex)
124.
{
125.
switch(arrResult[arrayIndex])
126.
{
127.
case 0:
128.
arrResult[arrayIndex] = 1;
Is source of error.
Note that.
In java, arrays index range from 0 to length-1
In your code above, when method addon() is called , you are passing array length as parameter, and in code above you are trying to access array[length] which does not exist and hence the exception. Thus you may want to keep that length-1
In the following code line #86
arrResult = this.addOne(arrResult.length);
There are lot of logical errors in your code. This is just the one that throws the exception you mentioned

ANTLR Error Recovery Causing a NullPointerException in Java

I have an ANTLR Grammar that is in the form:
A - B: more,things
However sometimes, either A or B can be missing such as:
- B: more, things //Skip
A - : some, other, things //Skip
C - D: yet, more, things //Read this one and following
E - F: things
I want ANTLR to skip over those lines (where either side of - is missing) and continue processing the rest.
Basically, something like that
- B: more, things {if (!hasBothParts()) { continueAtNextLine();} };
From the book, I provided a #rulecatch and this catch after my appropriate Parser block:
catch[RecognitionException re]{
reportError(re);
consumeUntil(input, NEWLINE);
input.consume();
}
EDIT 2 - I tried doing this instead:
while (input.LA(1) != 6){
input.consume();
}
This worked as expected. 6 is the token identifier for "NEWLINE" for me. I don't know how to do a comparison like input.LA(1) != "\n" or something similar. I know it's not correct to do it this way, I am just experimenting. If you know the right way, please tell me! :)
But this works, unlike the first loop. I suspect consumeUntil is perhaps not seeing the NEWLINE on channel Hidden.
The NullPointer seems to be caused by the input fast-forwarding to EOF, and hence, the tree grammar is hitting a Null when it's doing input.LT(1).
However, in doing so, I get a NullPointerException in my tree grammar:
Exception in thread "main" java.lang.NullPointerException
at org.antlr.runtime.tree.BaseTreeAdaptor.isNil(BaseTreeAdaptor.java:70)
at org.antlr.runtime.tree.CommonTreeNodeStream.nextElement(CommonTreeNodeStream.java:93)
at org.antlr.runtime.misc.LookaheadStream.fill(LookaheadStream.java:94)
at org.antlr.runtime.misc.LookaheadStream.sync(LookaheadStream.java:88)
at org.antlr.runtime.misc.LookaheadStream.LT(LookaheadStream.java:119)
....
The behavior I want is for the parser to skip over the lines missing components and proceed with the remaining lines. The tree parser should not be a problem, I assume?
The ANTLR book does not mention anything regarding this issue.
Also, I think the ANTLR Error Recovery mentions something along those lines but the solution provided is fairly complex/ugly and dates back to 2004. So, is there a better way of doing this relatively simple thing in ANTLR?
Thank you.
EDIT If this helps, the error was caused by this line in the generated tree grammar:
retval.start = input.LT(1);
Which is, I assume, being called with nothing. I.e. LT(1) is returning Null, since it skipped over.

Multi-Dimensional Array and ArrayIndexOutOfBoundsException

I have a strange problem which I can't fix:
A field:
private boolean[][][] gaps;
Constructor (1st line):
gaps = new boolean[NOBARRICADES][WIDTH][HEIGHT];
Constructor (2nd line):
for (int i = 0; i < NOBARRICADES; i++) {
Java throws an error for the 2nd line, saying:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Does it have anything to do with Java syntax (the mistake is in these lines of code) or I should look for the problem somewhere else?
You're probably misreading the error output. Your second line does not even access the array - make sure that it's not the first line of the body of the for-loop that throws the exception. Also, make sure that you use i only to index the first dimension of your array.
sometimes the java compiler is off by a line or two. You may check the lines of code around the line that it says the error is on and see if you see anything.
Sorry, but you really don't want to do that.
Multidimensional arrays are never worth the confusion they cause--they have no positive value at all (with the POSSIBLE exception of a clear, obvious x,y array).
I suggest you try starting with either a list of two-dimensional arrays or a two-dimensional array of objects where each object contains a list.

Categories