Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在HashMap中放置新对象时陷入无休止的循环_Java_Hashmap - Fatal编程技术网

Java 在HashMap中放置新对象时陷入无休止的循环

Java 在HashMap中放置新对象时陷入无休止的循环,java,hashmap,Java,Hashmap,我写了一些代码,在for循环中逐行读取文本文件;每一行都是在JPanel上绘制新形状的命令。 我有一个用于存储名称和形状的HashMap Map<String, GeoShape> geoObj = new HashMap<String, GeoShape>(); 这是一个无止境的循环。我在调试模式下运行代码,类点没有问题 非常感谢您的帮助 编辑:这是类点 public class Point extends Segment { private Line2D p

我写了一些代码,在for循环中逐行读取文本文件;每一行都是在JPanel上绘制新形状的命令。 我有一个用于存储名称和形状的HashMap

Map<String, GeoShape> geoObj = new HashMap<String, GeoShape>();
这是一个无止境的循环。我在调试模式下运行代码,类点没有问题

非常感谢您的帮助

编辑:这是类点

public class Point extends Segment {
    private Line2D point;
    private Stroke thickness;
    private Color fColor;
    private double x;
    private double y;

    public Point(String[] cmd, Graphics2D graph) {
        System.out.println("point");
        x = Double.parseDouble(cmd[4]);
        y = Double.parseDouble(cmd[5]);
        try {
            fColor = setColor(cmd[3]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        thickness = new BasicStroke(Integer.parseInt(cmd[6]));

        paint(graph);
    }

    @Override
    public void paint(Graphics2D graph) {
        point = new Line2D.Double(x, y, x, y);

        graph.setStroke(thickness);
        graph.setColor(fColor);
        graph.draw(point);
    }
}
for循环:

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

int linesNum = Integer.parseInt(br.readLine());
String line = br.readLine();
for (int i = 1; i <= linesNum; i++) {
     while (line != null) {
        cmd = line.split(" ");
        if (cmd[0].equalsIgnoreCase("ADD")) {
        if (cmd[2].equalsIgnoreCase("Point")) 
        geoObj.put(cmd[1], new Point(cmd, graph));
            // some else if with the same structure for other shapes
//at the end of for loop I have this:  line = br.readLine();
FileReader fr=新的FileReader(文件);
BufferedReader br=新的BufferedReader(fr);
int linesNum=Integer.parseInt(br.readLine());
String line=br.readLine();

对于(int i=1;i你是说对
put
的单个调用会导致无休止的循环吗

这在理论上是可能的,但前提是另一个线程在没有正确同步的情况下更新了
HashMap
。(您需要对
HashMap
类进行详细分析,以确定这是否实际可能,但如果两个线程在没有适当同步的情况下同时读取和写入
HashMap
,则散列链可能会损坏,当
get
调用尝试to搜索损坏的链。)


如果这不是问题所在,那么问题出在您没有向我们展示的代码中。

这个问题没有任何帮助。您没有提供任何可以给任何人提供线索的数据。如果您在调试模式下运行,您应该拥有所需的所有信息。您编写的Point或其他类有问题。Map没有问题或者HashMap。我猜这是一个坏点构造函数。检查for循环,什么是中断条件?
readLine()
应该在流的末尾返回null。要么每次迭代都不调用readLine,要么流永远不会结束。
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

int linesNum = Integer.parseInt(br.readLine());
String line = br.readLine();
for (int i = 1; i <= linesNum; i++) {
     while (line != null) {
        cmd = line.split(" ");
        if (cmd[0].equalsIgnoreCase("ADD")) {
        if (cmd[2].equalsIgnoreCase("Point")) 
        geoObj.put(cmd[1], new Point(cmd, graph));
            // some else if with the same structure for other shapes
//at the end of for loop I have this:  line = br.readLine();