我有下面的代码应该在用户输入时找到重复的字符串,以便我可以从地图中删除密钥(我将来会这样做,现在,它只是重复的值在一个字符串)。但是,我在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);
}
}
}}
感谢您的帮助。
答案 0 :(得分: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);*/
}
}
}}
答案 1 :(得分:0)
在您的while循环中,将此语句System.out.println(map.get(inp_addr_val[i]));
更改为System.out.println(map.get(inp_addr_val[i-1]));
。
在您的代码中,您实际上是在尝试访问为您提供NPE的i+1
变量。
答案 2 :(得分: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]));
}
太简单地解释您的问题,您正在检查您读取的行是否为空并将下一行插入数组。所以它发生了,数组的最后一个值是空的。
Sarajog