简体   繁体   中英

List items = new ArrayList() : it does not work

Is all in the title, I do not understand the problem this time is a bit different, I used the same Object(List) for two different programs and it does not work in the second time, see :

private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {                                            
init_creer_client();
List  items  = new ArrayList();
items.add("mawren");
items.add("blabla");
items.add("Bonjour");

CL.show(cartes,"creer_client");       
}   

screenshot about the error : 在此输入图像描述

by cons here its work smoothly :

 import java.awt.Dimension;
 import java.awt.HeadlessException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

 public class Test_swingx extends JFrame {

public Test_swingx(String title) throws HeadlessException {

this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items  = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items,false);
pan.add(jtf);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280, 150, 500, 200);

}


 public static void main(String[] args) {

Test_swingx tsx=new Test_swingx("helloo swingx");

}
}

can anyone explain to me ?

你有一个java.awt.List导入应该是java.util.List

It's because the List on the left-hand side is a java.awt.List instead of a java.util.List .

Try changing the line to:

java.util.List items = new ArrayList();

This is probably happening because you're importing java.awt.* and java.util.List . If you can change how you import these classes, you can avoid namespacing the type inline.

Nope, compiles fine:

package cruft;

import java.util.ArrayList;
import java.util.List;

/**
 * ListExample description here
 * @author Michael
 * @link
 * @since 2/11/12 7:27 PM
 */
public class ListExample {

    public static void main(String[] args) {
        List items = new ArrayList();
        for (String arg : args) {
            items.add(arg);
        }
        System.out.println(items);
    }
}

Runs fine:

"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 111.255\bin" -Dfile.encoding=UTF-8 -classpath . com.intellij.rt.execution.application.AppMain cruft.ListExample foo bar baz bat
[foo, bar, baz, bat]

Process finished with exit code 0

完整性检查:您是否导入了import java.util.Listimport java.util.ArrayList

检查导入,因为java.awt.Listjava.util.List

I think the confusion comes from having two List types in different packages, as the error message says. You don't give all the code that generates the error, but I think a reasonable start to a fix would be to change the highlighted line to:

java.util.List items  = new ArrayList();

and make sure you have imported java.util.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM