-3

我有下面的代码,它应该在用户输入它们时找到重复的字符串,以便我可以从地图中删除键(我将来会这样做,现在,它只是字符串中的重复值)。但是,我在 if 语句中收到 NullPointerException 错误。为什么会这样?完整代码如下;

import java.io.*;
import java.util.*;

public class Testing
{
    public static void main (String [] args) throws FileNotFoundException
    {
        int i=0,j,k,map_addr;
        String [] inp_value = new String[100];
        String [] inp_addr_val = new String[100];
        String [] inp_addr = new String[100];
        String addr_path,sCurrentLine;
        BufferedReader reader = null;
        Scanner inp = new Scanner(System.in);
        Map<String, String> map = new HashMap<>();
        Iterator map_iterator = map.keySet().iterator();


        System.out.println("Welcome! Please enter the addresses");

        while(i!=100)
        {
            System.out.println("Enter address:");
            inp_addr[i] = inp.nextLine();
            try 
            {
                reader = new BufferedReader(new FileReader(inp_addr[i]));

                while ((sCurrentLine = reader.readLine()) != null) 
                {
                      inp_value[i]=reader.readLine();
                      inp_addr_val[i]=Integer.toString(i);
                      map.put(inp_addr_val[i],inp_value[i]);
                      i++;
                      System.out.println(map.get(inp_addr_val[i]));


                }
                         for(j=0;j<=i;j++)
                          {
                              if(inp_value[j].equals(inp_value[i]))
                              {
                              System.out.println("File already exists, skipping this file.");
                              i--;

                              }
                          }

            }catch (IOException ex) {
                System.out.println("Something bad happened. Program is exiting now...");
                System.exit(0);

            }





    }
    }}

谢谢您的帮助。

4

3 回答 3

1

检查这个

import java.io.*;
import java.util.*;

public class Testing
{
    public static void main (String [] args) throws FileNotFoundException
    {
        int i=0,j,k,map_addr;
        String [] inp_value = new String[100];
        String [] inp_addr_val = new String[100];
        String [] inp_addr = new String[100];
        String addr_path,sCurrentLine;
        BufferedReader reader = null;
        Scanner inp = new Scanner(System.in);
        Map<String, String> map = new HashMap<>();
        Iterator map_iterator = map.keySet().iterator();


        System.out.println("Welcome! Please enter the addresses");

        while(i!=100)
        {
            System.out.println("Enter address:");
            inp_addr[i] = inp.nextLine();
            try 
            {
                reader = new BufferedReader(new FileReader(inp_addr[i]));

                while ((sCurrentLine = reader.readLine()) != null) 
                {
                      inp_value[i]=reader.readLine();
                      inp_addr_val[i]=Integer.toString(i);
                      map.put(inp_addr_val[i],inp_value[i]);
                      i++;
                      System.out.println(map.get(inp_addr_val[i-1]));


                }
                         for(j=0;j<=i;j++)
                          {
                              if(inp_value[j].equals(inp_value[i-1]))
                              {
                              System.out.println("File already exists, skipping this file.");
                              i--;

                              }
                          }

            }catch (IOException ex) {
                ex.printStackTrace();
                /*System.out.println("Something bad happened. Program is exiting now...");
                System.exit(0);*/

            }





    }
    }}
于 2013-09-06T08:55:45.763 回答
0

在您的 while 循环中,将此语句更改System.out.println(map.get(inp_addr_val[i]));System.out.println(map.get(inp_addr_val[i-1]));.

实际上,在您的代码中,您正在尝试访问i+1为您提供 NPE 的变量。

于 2013-09-06T08:50:47.453 回答
0

代替:

while ((sCurrentLine = reader.readLine()) != null) 
                    {
                          inp_value[i]=reader.readLine();
                          inp_addr_val[i]=Integer.toString(i);
                          map.put(inp_addr_val[i],inp_value[i]);
                          i++;
                          System.out.println(map.get(inp_addr_val[i]));
                    }

和:

while ((sCurrentLine = reader.readLine()) != null) 
                    {
                          inp_value[i]=sCurrentLine ;
                          inp_addr_val[i]=Integer.toString(i);
                          map.put(inp_addr_val[i],inp_value[i]);
                          i++;
                          System.out.println(map.get(inp_addr_val[i]));
                    }

太简单地解释您的问题,您正在检查您读取的 Line 是否为空,并将下一行插入到数组中。所以它可能会发生,数组的最后一个值是空的。

萨拉约格

于 2013-09-06T08:58:12.547 回答