My project is supposed to do basic operations on polynomials. Each polynomial si an ArrayList of objects from class Monom.
I created an interface with Swing. I made a method that transforms a String received into a Polinom object. If I use basic commands, like when I press a button, get text and show it, everything works fine. But when I call the method mentioned before (toPolinom) , the buttons do not work anymore and red lines are written in the console.
This is how the button code looks like :
mybutton = new JButton("Suma");
mybutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = new Polinom(p1);
polinom2 = new Polinom(p2);
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(polinom1.suma(polinom1, polinom2)
.toString());
}
});
mybutton2 = new JButton("Diferenta");
mybutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(rezultat.diferenta(polinom1, polinom2)
.toString());
}
});
resultField and numberField1 are type JTextField;
This is my toPolinom() method from class Polinom.java:
public Polinom toPolinom(String p1) {
List<Monom> prez = new ArrayList<Monom>();
int grad, coef;
int i = 0;
for(i=0; i<p1.length()-1; i=i+5){
grad=0; coef=0;
if(p1.charAt(i)=='+') coef=Character.getNumericValue(p1.charAt(i+1));
else if (p1.charAt(i)=='-') coef=-Character.getNumericValue(p1.charAt(i+1));
grad=Character.getNumericValue(p1.charAt(i+4));
prez.add(new MonomZ(grad, coef));
}
return new Polinom(prez);
}
I tested this methode, and it works.
Finally, this is what I receive when I click my buttons :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at tema1.Polinom.toString(Polinom.java:208)
at tema1.Fatza$1.actionPerformed(Fatza.java:61)
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$400(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 warning methode added :
public String toString() {
String Rezultat = "";
for (int i = 0; i < polinom.size() - 1; i++) {
Rezultat = Rezultat + verificare(polinom.get(i), polinom, i);
}
return Rezultat
+ verificare(polinom.get(polinom.size() - 1), polinom, //line 208
polinom.size() - 1);
}
private String verificare(Monom monom, List<Monom> polinom, int n) {
// functia verifica daca trebuie adaugat semn sau nu
String text;
boolean ok = true;
for (int i = 0; i < n; i++) {
if (polinom.get(i).getCoef().doubleValue() != 0) {
ok = false;
break;
}
}
double numar = monom.getCoef().doubleValue();
if (ok == true && numar > 0) {
text = monom.toString();
} else {
text = "+" + monom.toString();
}
if (numar < 0) {// numarul are inclus semn negativ
text = monom.toString();
}
return text;
}
If I forgot any information, please ask me.
ArrayIndexOutOfBoundsException, what the incorrect value is,-1and the line number where it occurs:at tema1.Polinom.toString(Polinom.java:208). So what code is on line 208 of the Polinom class?