Java Main中的NullPointerException错误
当用户在控制台中输入文件名时,尝试读取文件。程序编译和运行时没有错误。输入文件名并按enter键后,将出现此错误。不知道为什么。任何帮助都将不胜感激Java Main中的NullPointerException错误,java,nullpointerexception,Java,Nullpointerexception,当用户在控制台中输入文件名时,尝试读取文件。程序编译和运行时没有错误。输入文件名并按enter键后,将出现此错误。不知道为什么。任何帮助都将不胜感激 Exception in thread "main" java.lang.NullPointerException at java.io.Writer.<init>(Writer.java:88) at java.io.PrintWriter.<init>(PrintWriter.java:113) at java.io.P
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.<init>(Writer.java:88)
at java.io.PrintWriter.<init>(PrintWriter.java:113)
at java.io.PrintWriter.<init>(PrintWriter.java:100)
at propertylistings.propertylistings.main(propertylistings.java:34)
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
public class propertylistings {
public static void main(String[] args)
throws FileNotFoundException
{
// Prompt for the input file name
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
BufferedWriter pwfo = null;
try {
pwfo = new BufferedWriter(new FileWriter("C:\\agentReport.txt",
true));
} catch (IOException e) {
}
//next line is LINE 34
PrintWriter pwo = new PrintWriter(pwfo);
// Construct property type treeSet
Set<String> propertyTypes = pTypes(inputFileName);
// Print property types from treeSet
for (String type : propertyTypes) {
System.out.println(type);
pwo.println(type);
}
// Construct agent ids and values treeSet
Set<String> agentRpt = agentValue(inputFileName);
// Print agent Ids and values from key set
for (String tail : agentRpt) {
{
System.out.println(tail);
pwo.println(tail);
}
}
pwo.flush();
pwo.close();
}
// Reads the input file.
// @return the alphabetized property types in uppercase.
public static Set<String> pTypes(String inputFileName)
throws FileNotFoundException
// Construct a tree set to return property types
{
Set<String> type = new TreeSet<String>();
Scanner in = new Scanner(new File(inputFileName));
// Use delimiters to select specific chars for set
in.useDelimiter("[1234567890. ]");
while (in.hasNext()) {
type.add(in.next().toUpperCase());
}
in.close();
return type;
}
// Reads the input file.
// @returns the Agent id's and corresponding property values.
public static Set<String> agentValue(String inputFileName)
throws FileNotFoundException {
TreeSet<String> tail = new TreeSet<String>();
SortedMap<String, Number> agentValues = new TreeMap<String, Number>();
Scanner in = new Scanner(new File(inputFileName));
String line = inputFileName;
while (in.hasNextLine()) {
try {
line = in.nextLine();
String[] fields = line.split("[\\s}]");
String agentId = (fields[3]);
Double pValue = Double.parseDouble(fields[2]);
if (agentValues.containsKey(agentId)) {
pValue += agentValues.get(agentId).doubleValue();
}
agentValues.put(agentId, pValue);
} catch (Exception e) {
}
// Create keyMap with all keys and values
Set<String> keySet = agentValues.keySet();
for (String key : keySet) {
Number value = agentValues.get(key);
// System.out.println(key + ":" + value);
tail.add(key + ":" + value);
}
}
return tail;
}
}
线程“main”java.lang.NullPointerException中的异常
在java.io.Writer(Writer.java:88)
位于java.io.PrintWriter(PrintWriter.java:113)
在java.io.PrintWriter。(PrintWriter.java:100)
位于propertylistings.propertylistings.main(propertylistings.java:34)
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.util.Scanner;
导入java.util.Set;
导入java.util.SortedMap;
导入java.util.TreeMap;
导入java.util.TreeSet;
公共类属性列表{
公共静态void main(字符串[]args)
抛出FileNotFoundException
{
//提示输入文件名
扫描仪控制台=新扫描仪(System.in);
System.out.print(“输入文件:”);
字符串inputFileName=console.next();
BufferedWriter pwfo=null;
试一试{
pwfo=new BufferedWriter(新文件写入程序(“C:\\agentReport.txt”),
是的);
}捕获(IOE异常){
}
//下一行是第34行
PrintWriter pwo=新的PrintWriter(pwfo);
//构造属性类型树集
设置propertyTypes=pTypes(inputFileName);
//从树集打印属性类型
for(字符串类型:propertyTypes){
系统输出打印项次(类型);
pwo.println(类型);
}
//构造代理ID和值树集
设置agentRpt=agentValue(inputFileName);
//打印密钥集中的代理ID和值
用于(字符串尾:agentRpt){
{
系统输出打印项次(尾部);
pwo.println(尾部);
}
}
pwo.flush();
pwo.close();
}
//读取输入文件。
//@返回按字母顺序排列的大写属性类型。
公共静态集类型(字符串输入文件名)
抛出FileNotFoundException
//构造树集以返回属性类型
{
设置类型=新树集();
扫描仪输入=新扫描仪(新文件(inputFileName));
//使用分隔符为集合选择特定字符
in.useDelimiter(“[1234567890.]”);
while(在.hasNext()中){
键入.add(in.next().toUpperCase());
}
in.close();
返回类型;
}
//读取输入文件。
//@返回代理id和相应的属性值。
公共静态设置代理值(字符串输入文件名)
抛出FileNotFoundException{
树集尾部=新树集();
SortedMap agentValues=新树映射();
扫描仪输入=新扫描仪(新文件(inputFileName));
字符串行=输入文件名;
while(在.hasNextLine()中){
试一试{
line=in.nextLine();
String[]fields=line.split(“[\\s}]”);
字符串agentId=(字段[3]);
Double pValue=Double.parseDouble(字段[2]);
if(agentValues.containsKey(agentId)){
pValue+=agentValues.get(agentId.doubleValue();
}
agentValues.put(agentId,pValue);
}捕获(例外e){
}
//使用所有键和值创建keyMap
Set keySet=agentValues.keySet();
用于(字符串键:键集){
Number value=agentValues.get(键);
//System.out.println(键+“:”+值);
tail.add(键+“:”+值);
}
}
返回尾;
}
}
在catch块中放置stacktrace,您将知道确切的错误
try {
pwfo = new BufferedWriter(new FileWriter("C:\\agentReport.txt", true));
} catch (IOException e) {
e.printstacktrace();
}
catch(IOE异常){}
不好。很可能您收到了一个IOException
并将其忽略,留下pwfo
null
。添加堆栈跟踪有效。它试图将文件写入需要管理员权限的文件夹。谢谢你的帮助!!