Why could be a possible reason for:
JTextPane p = new JTextPane();
p.setText("hello");
The exact code is rather long. So I'm not sure what parts I must show here.
It's something like:
Tab t = new Tab(jp1);
t.editortxt.setText("hello");
Tab extends JPanel and has in its constructor:
editortxt = new JTextPane();
i.e., editortxt is a property of Tab.
It throws this:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:371)
at java.util.ArrayList.get(ArrayList.java:384)
I mean why would something so abstract throw when I use its own API?
Verify are you passing correct index to ArrayList.get() method. It looks you are accessing the element from ArrayList that have size is zero.
I try to create a generic array but I'm taking the error of the title.
ByteConverter<Product> byteconverter = new ByteConverter<Product>();
//into an inner class I have to declare a final field
final ByteConverter<Product>[] byteconverter2 = {byteconverter};
So, I searched at the Stackoverflow for a possible solution. I found something similar here: Cannot create an array of LinkedLists in Java...? , so I canged my code to the following:
final ByteConverter<Product>[] byteconverter2 = {(ByteConverter<Product>[])byteconverter};
but I still take the same error. I can't understand why..Any help please?
final ByteConverter<Product>[] byteconverter2 =
new ByteConverter[]
{
byteconverter
};
this works well
This compiles, though with a warning
ByteConverter<Product> byteconverter = new ByteConverter<Product>();
ByteConverter<Product>[] byteconverter2 = new ByteConverter[] { byteconverter };
Read here http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html about restrictions for generics
I've been looking all over, and i cant find anyone who can solve this problem. I'm making a game, and in that game, i have editable controls. the controls window is a seperate JFrame, and when i click the confirm button, it is supposed to write the items in the JTextFields (holding the controls) to a file. but that wasnt working, so instead i have it print the arraylist that holds the values. here is the code:
public void writeControls() {
ArrayList<String> al = new ArrayList<String>();
al.add(up.getText());
al.add(down.getText());
al.add(left.getText());
al.add(right.getText());
al.add(jump.getText());
al.add(duck.getText());
al.add(attack.getText());
for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
System.exit(0);
}
the problem is this: if i change the final JTextField attack or any other one for that matter, and click submit, the system prints out the default controls. for example, if the JTextFields have the values w,a,s,d,r,t,q and i change the value q to i, it prints out q. what am i doing wrong? thanks in advance!
EDIT 1:
code for the textfields, and the FILES.... is simply a string stored in a different class. the class setText() is below the textfields.
up = new JTextField(setText(FILES.controlsFileFinalDir, 1));
down = new JTextField(setText(FILES.controlsFileFinalDir, 2));
left = new JTextField(setText(FILES.controlsFileFinalDir, 3));
right = new JTextField(setText(FILES.controlsFileFinalDir, 4));
jump = new JTextField(setText(FILES.controlsFileFinalDir, 5));
duck = new JTextField(setText(FILES.controlsFileFinalDir, 6));
attack = new JTextField(setText(FILES.controlsFileFinalDir, 7));
public String setText(String fileDir, int lineNum) {
String txt = "";
txt = io.readSpecificLine(fileDir, lineNum);
txt = switchCase(txt);
return txt;
}
switchcase() is only taking what you have written in the text file that these are getting the values from, and translating them. so if the value is 0, it is turned into Space, etc. io.readSpecificLine(); is only to get the line of text from the file. does this help?
EDIT 2:
i just was dinking around and found out that if i set the JTextField text by using setText(""); then use getText(); it works. so the problem is that when i change it manually, and use getText(); it wont work. Why?
To update the text to a currently existing JTextField, I would establish the JTextField as a class variable, and create a setter/getter method to adjust it (which I'm assuming you're doing).
According to your methods, you would use something like:
up.setText(setText(FILES.controlsFileFinalDir, 7));
Edit: **The first setText is the JTextField.setText, the second setText is your public method you posted. I'm assuming your second getText() isn't working because you're probably not setting the text correctly.
Without seeing more code, I can't really give a better guess.
The main possibilities:
(1) The text fields have their editable property set to false.
(2) You are creating multiple copies of the JTextFields, then editing a new one on the screen, but referring to the old one when you get the value.
(3) You have a ValueChanged or LostFocus event handler that is resetting the text fields to their defaults
(4) It is actually JFormattedTextField not a JTextField
If I was you, I would try to debug the programm. You will probably do some Mistake in your code, you won't be able to see, by just checking the code.
For example in which order do you call the functions and so on, maybe you have a fault here, or maybe you have several threads, so you try to read the Textfields without even set them and so on ... It's hard to say without reviewing the whole Code.
So if you use eclipse you can follow this link for an explanation on how to debug: http://www.vogella.com/articles/EclipseDebugging/article.html
Netbeans or any other IDE should support debugging as well.
This may seem like a strange thing to suggest, but I think this is an issue with pointers. If you create a new string before passing it in, JTextField will be able to change it internally and return what you expect when asked for the modified value.
down = new JTextField("" + setText(FILES.controlsFileFinalDir, 2));
// or
down = new JTextField(new String(setText(FILES.controlsFileFinalDir, 2)));
You might want to try the following:
create a class Test.java
import java.util.ArrayList;
import javax.swing.JTextField;
public class Test implements Runnable {
private ArrayList<JTextField> textFields = null;
private ArrayList<String> stringList = null;
public Test(ArrayList<JTextField> textFields, ArrayList<String> stringList) {
this.textFields = textFields;
this.stringList = stringList;
}
#Override
public void run() {
for ( JTextField textField : this.textFields )
this.stringList.add( textField.getText() );
}
}
and then, at the place where you use the "getText() method .. "
do the following...
ArrayList<JTextField> textFields = new ArrayList<JTextField>();
// add all the JTextField to textFields
ArrayList<String> stringList = new ArrayList<String>();
Test test = new Test( textFields, stringList );
SwingUtilities.invokeLater( test );
// check if the stringList is populated.
If this work, then what I believe is that, for some reason, the JTextField hasn't finished
"setting" the text, and before it finishes your getText() was called. I've had similar problems before, and this solved my problem that time, but still, this might not be the perfect solution.
First, you should change your "setText()" method name to something like "getTextFromFile()" it would be more readable
Then, if you are setting and reading the new text in different threads, my bet is that the setText() is taking long to return, because it is accessing the file system, while the method that read the values run instantly
I would try to do run a little test:
public void test(){ // must be run after the JTextFields be initialized
up.setText("TEST")
System.out.println(up.getText());
up.setText(setText(FILES.controlsFileFinalDir, 1));
System.out.println(up.getText());
}
If the test() prints the correct values, then we can assume that if you set and read the new value in the same thread it works fine
The other test I would do is:
public void testThread(){
new Thread(){
public void run(){
while(true){
if(up!=null){
System.out.println(up.getText());
}
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}
}.start();
}
It will print the value of up each 1 second, so that you can see if after some time you get the new value. If it does, then the answer is: Your setText() is taking long to run and you are reading the value before the new value is set
SOLUTION
none of the above answers were working for me, so i finally decided to just start over with that class. the few things i changed were the way i made the JTextFields. I made them as an array instead of individual objects. Second is the way i put what they say. When i initialized them, i was unable to get them to create WITH the text in the parameters. so i had to do that seperately. i changed some of the method names so as to reduce future confusion, and it worked! so im not sure what was up with that, maybe it was the way i did it, maybe just a fluke. it happens sometimes, so im sorry for the delay and waste of your time! thanks for all the answers anyway!
Try this:
textbox.setText(setFile(args)); // your function for set file
I am getting the following exception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Java.CompileFile.doCompilation(CompileFile.java:48)
at GUI.CompilerForm.compileBtnActionPerformed(CompilerForm.java:225)
at GUI.CompilerForm.access$400(CompilerForm.java:23)
............
I no the error is at line 48 in CompileFile.java, it is saying that the array in NULL and i dont know why because that is where i am adding strings to it!
String[] compile;
int numberOfErrors = 0;
.
.
.
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
String errors = diagnostic.getKind().toString()+" on line "+ diagnostic.getLineNumber() +"\nIn file: \n"+ diagnostic.toString();
compile[numberOfErrors] = errors;
numberOfErrors++;
}
I have tried System.out.println(errors); straight after i set it and it is working fine so i really dont know what is going on!
Any suggestions?
You've declared a variable called compile, but you haven't shown anywhere that it's given a value. Assuming it's an instance variable, its value will default to null. You need to initialize it with:
compile = new String[someSize];
where someSize is "big enough".
Alternatively, and preferrably, you could use a list:
// TODO: Rename variable to something more sensible
private final List<String> compile = new ArrayList<String>();
then...
compile.add(errors);
Then you can probably get rid of numberOfErrors too, as that would just be compile.size() presumably.
From the code snap you are showing, it seems you did not initialize compile, so it is initialized to null as default.
You should explicitly create a String[] and assign it to compile:
compile = new String[MY_SIZE];
If you are trying to append errors, you might want to consider using a dynamic array - which is an ArrayList<String> in java for it, and append elements, using ArrayList.add(element)
I guess you haven't initialized the array (properly)
String[] compiled = new String[size];
or you haven't set a proper size of the array
If you are unable to predict how many items there will be in the array. Use lists (eg ArrayList) instead
List<String> compiled = new ArrayList<String>();
Arraylists have no size limit.
To add items
compiled.add(item);
It looks like you haven't initialized your array.
Try something like this :
compile = new String[numberOfErrors];
And then store the errors in the array.
Maybe this just isn't possible but what I'm trying to do is change the visibility of multiple elements in a GUI at the same time using an ArrayList to reference to them sort of dynamically. The objects are created by themselves in another method.
Both oldScreen.setVisible(false); and oldScreen<1>.setVisible(false); statements cause errors. I had a hunch my idea wouldn't work out so well.
Here is basically what i have, any way i can achieve this?
private void initScreens() {
// I create some ArrayLists as "screensets" of sorts and put some GUI elements in there
ArrayList startScreen = new ArrayList();
ArrayList lostScreen = new ArrayList();
ArrayList playScreen = new ArrayList();
startScreen.add(startB);
startScreen.add(exitB);
lostScreen.add(yl1);
lostScreen.add(yl2);
lostScreen.add(yl3);
lostScreen.add(yl4);
lostScreen.add(yl5);
}
private void changeScreen(ArrayList oldScreen,ArrayList newScreen) {
// now i try to create a handy method to handle the length of the arrays itself, so if
i need to make changes to screens I just add them to there array. They are then easily
displayed, and hidden when told.
int os = oldScreen.size();
int ns = newScreen.size();
for (int i = os; i > 0; i--){
oldScreen<i>.setVisible(false);
oldScreen<1>.setVisible(false);
}
That's invalid syntax.
You're trying to write
oldScreen.get(i)
You should also use generics (ArrayList<Screen>) to avoid casting.