简体   繁体   English

为什么会收到“ java.util.NoSuchElementException”?

[英]Why am I getting a “java.util.NoSuchElementException”?

Here the error i am getting... 这是我得到的错误...

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Maze.Map.readFile(Map.java:60)
at Maze.Map.<init>(Map.java:23)
at Maze.Board.<init>(Board.java:16)
at Maze.Maze.<init>(Maze.java:15)
at Maze.Maze.main(Maze.java:9)

Below is my code! 下面是我的代码!

package Maze;

import javax.swing.*;

public class Maze 
{

    public static void main(String[] args)

    {
        new Maze();
    }

    public Maze()

    {

        JFrame f = new JFrame();

        f.setTitle("Maze Game");

        f.add(new Board());
        f.setSize(464, 485);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

package Maze;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener
{

    private Timer timer;

    private Map m;

    public Board()
    {
        m = new Map();

        timer = new Timer(25, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        repaint();
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        for(int y = 0; y < 14; y++)
        {
            for(int x = 0; x < 14; x++)
            {
                if(m.getMap(x, y).equals("g"))
                {
                    g.drawImage(m.getFloor(), x * 32, y * 32, null);
                }
                if(m.getMap(x, y).equals("w"))
                {
                    g.drawImage(m.getWall(), x * 32, y * 32, null);
                }
            }
        }

    }

}

package Maze;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;

public class Map 
{
    private Scanner m;
    private String Map[] = new String[14];
    private Image floor,
                  wall;    

    public Map()
    {
        ImageIcon img = new ImageIcon("C://Test//MazeGame//floor.jpg");
        floor = img.getImage();
        img = new ImageIcon("C://Test//MazeGame//wall.jpg");
        wall = img.getImage();

        openFile();
        readFile();
        closeFile();
    }

    public Image getFloor()
    {
        return floor;
    }

    public Image getWall()
    {
        return wall;
    }

    public String getMap(int x, int y)
    {
        String index = Map[y].substring(x, x + 1);
        return index;
    }

    public void openFile()
    {
        try
        {
            m = new Scanner(new File("C://Test//MazeGame//Map.txt"));
        }catch(Exception e)
            {
                System.out.print("Error Loading Map!");
            }
    }

    public void readFile()
    {
        while(m.hasNext())
        {
            for(int i = 0; i < 14; i++)
            {
                Map[i] = m.next();
            }
        }
    }



    public void closeFile()
    {
        m.close();
    }

}

I don't know if this is your error (you haven't told us which line is line 60 of your Map class, the line that is causing your exception), but this is dangerous code: 我不知道这是否是您的错误(您没有告诉我们哪一行是Map类的60行,这是导致您异常的行),但这是危险的代码:

while(m.hasNext())
{
    for(int i = 0; i < 14; i++)
    {
        Map[i] = m.next();
    }
}

You're calling hasNext() once per loop iteration, but calling next() 14 times! 您每个循环迭代调用一次hasNext() ,但是调用next() 14次! There should be a strict 1 to 1 correlation in that each next() should match with a preceding hasNext() . 应该存在严格的一对一关联,因为每个next()应该与前面的hasNext()匹配。


Also, there's no reason for the nested for loop since the while loop will handle all you need. 另外,没有必要嵌套for循环,因为while循环将处理您需要的所有内容。 You probably would get by with something like: 您可能会遇到类似这样的情况:

int i = 0;
while(m.hasNext()) {
   Map[i] = m.next();
   i++;
}

But it would be safer to use an ArrayList rather than an array. 但是使用ArrayList而不是数组会更安全。


As an aside, please learn and follow Java coding conventions. 另外,请学习并遵循Java编码约定。 Methods and fields/variables/parameters should all start with a lower-case letter, so Map[i] is not allowed, but rather should be map[i]. 方法和字段/变量/参数都应以小写字母开头,因此不允许Map [i],而应该是map [i]。 Doing this will help us to better understand and follow your code and thus help you. 这样做将有助于我们更好地理解和遵循您的代码,从而为您提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我从下面的代码中收到错误 java.util.NoSuchElementException? - Why am I getting the error java.util.NoSuchElementException from my code below? 不知道为什么我收到java.util.NoSuchElementException错误 - Not sure why i'm getting an java.util.NoSuchElementException error 获取 java.util.NoSuchElementException - Getting java.util.NoSuchElementException 为什么我在在线编码 class 代码检查器上收到“线程中的异常”主“java.util.NoSuchElementException”错误? - Why am I getting a “Exception in thread ”main“ java.util.NoSuchElementException” error on a online coding class code checker? 我在Java中遇到此运行时错误:java.util.NoSuchElementException,并且我不确定如何解决它 - I am getting this runtime error in java: java.util.NoSuchElementException, and I'm not sure how to fix it Java扫描程序:获取“java.util.NoSuchElementException”错误,不确定原因 - Java scanner: Getting “java.util.NoSuchElementException” error, not sure why 在代码中获取 java.util.NoSuchElementException。 为什么? - Getting java.util.NoSuchElementException in the code. Why? 我一直得到“线程中的异常”主“java.util.NoSuchElementException” - I keep getting “Exception in thread ”main“ java.util.NoSuchElementException” 为什么会有这个java.util.NoSuchElementException? - Why do I have this java.util.NoSuchElementException? 继续在此程序上获取java.util.NoSuchElementException - Keep getting the java.util.NoSuchElementException on this program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM